Created
July 24, 2024 18:11
-
-
Save slightfoot/6266c566608482ddcc9760e9e24a952e to your computer and use it in GitHub Desktop.
Circular List - by Simon Lightfoot - Humpday Q&A :: 26th June 2024 #Flutter #Dart https://www.youtube.com/watch?v=mMv9wRcKZrw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MIT License | |
// | |
// Copyright (c) 2024 Simon Lightfoot | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
// | |
import 'dart:async'; | |
import 'dart:math' as math; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/rendering.dart'; | |
void main() { | |
runApp(const App()); | |
} | |
class App extends StatelessWidget { | |
const App({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return const MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Home(), | |
); | |
} | |
} | |
class Home extends StatefulWidget { | |
const Home({super.key}); | |
@override | |
State<Home> createState() => _HomeState(); | |
} | |
class _HomeState extends State<Home> { | |
@override | |
Widget build(BuildContext context) { | |
return const Material( | |
child: SafeArea( | |
child: CircularList(), | |
), | |
); | |
} | |
} | |
class CircularList extends StatelessWidget { | |
const CircularList({ | |
super.key, | |
}); | |
@override | |
Widget build(BuildContext context) { | |
return Scrollable( | |
controller: PrimaryScrollController.maybeOf(context), | |
axisDirection: AxisDirection.right, | |
physics: const BouncingScrollPhysics(), | |
viewportBuilder: (BuildContext context, ViewportOffset position) { | |
return Flow( | |
delegate: CircularScrollDelegate( | |
position: position, | |
size: const Size.square(80.0), | |
), | |
children: [ | |
for (int i = 0; i < Colors.accents.length; i++) // | |
ColoredBox( | |
color: Colors.accents[i], | |
child: Center(child: Text('Item #$i')), | |
), | |
], | |
); | |
}, | |
); | |
} | |
} | |
class CircularScrollDelegate extends FlowDelegate { | |
CircularScrollDelegate({ | |
required this.position, | |
required this.size, | |
}) : super(repaint: position); | |
final ViewportOffset position; | |
final Size size; | |
@override | |
bool shouldRepaint(covariant CircularScrollDelegate oldDelegate) { | |
return position != oldDelegate.position; | |
} | |
@override | |
BoxConstraints getConstraintsForChild(int i, BoxConstraints constraints) { | |
return BoxConstraints.tight(size); | |
} | |
@override | |
void paintChildren(FlowPaintingContext context) { | |
scheduleMicrotask(() { | |
position.applyViewportDimension(context.size.width); | |
position.applyContentDimensions( | |
0.0, | |
(size.width * context.childCount) - context.size.width, | |
); | |
}); | |
final centerX = (context.size.width - size.width) / 2; | |
final centerY = (context.size.height - size.height) / 2; | |
final phase = position.pixels / size.width; | |
final rx = (context.size.width - size.width) / 2; | |
final ry = (context.size.height - size.height) / 2; | |
for (int i = 0; i < context.childCount; i++) { | |
final e = i / context.childCount; | |
final x = math.sin(e * math.pi * 2 - phase) * rx; | |
final y = math.cos(e * math.pi * 2 - phase) * ry; | |
// if (y < 0) { | |
// continue; | |
// } | |
context.paintChild( | |
i, | |
transform: Matrix4.translationValues( | |
centerX + x, | |
centerY + y, | |
0.0, | |
), | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment