Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- void main() => runApp(const MyApp());
- class ExampleDestination {
- const ExampleDestination(this.color, this.colorName, this.spline);
- final Color color;
- final String colorName;
- final CatmullRomSpline spline;
- }
- List<ExampleDestination> paths = <ExampleDestination>[
- ExampleDestination( Colors.red, "Red", CatmullRomSpline(
- const <Offset>[
- Offset(0.05, 0.75),
- Offset(0.18, 0.23),
- Offset(0.32, 0.04),
- Offset(0.73, 0.5),
- Offset(0.42, 0.74),
- Offset(0.73, 0.01),
- Offset(0.93, 0.93),
- Offset(0.05, 0.75),
- ],
- startHandle: const Offset(0.93, 0.93),
- endHandle: const Offset(0.18, 0.23),
- )),
- ExampleDestination( Colors.green, "Green", CatmullRomSpline(
- const <Offset>[
- Offset(0.45, 0.52),
- Offset(0.75, 0.48),
- Offset(0.27, 0.89),
- Offset(0.91, 0.45),
- Offset(0.47, 0.74),
- Offset(0.29, 0.29),
- Offset(0.60, 0.32),
- Offset(0.92, 0.84),
- ],
- startHandle: const Offset(0.92, 0.40),
- endHandle: const Offset(0.60, 0.64),
- )),
- ExampleDestination( Colors.blue, "Blue", CatmullRomSpline(
- const <Offset>[
- Offset(0.03, 0.71),
- Offset(0.96, 0.39),
- Offset(0.16, 0.20),
- Offset(0.23, 0.33),
- Offset(0.94, 0.83),
- Offset(0.78, 0.67),
- Offset(0.79, 0.98),
- Offset(0.22, 0.19),
- ],
- startHandle: const Offset(0.96, 0.92),
- endHandle: const Offset(0.52, 0.63),
- ))
- ];
- class FollowCurve2D extends StatefulWidget {
- const FollowCurve2D({
- super.key,
- required this.path,
- this.curve = Curves.linear,
- required this.child,
- this.duration = const Duration(seconds: 1),
- });
- final Curve2D path;
- final Curve curve;
- final Duration duration;
- final Widget child;
- @override
- State<FollowCurve2D> createState() => _FollowCurve2DState();
- }
- class _FollowCurve2DState extends State<FollowCurve2D>
- with TickerProviderStateMixin {
- // The animation controller for this animation.
- late AnimationController controller;
- // The animation that will be used to apply the widget's animation curve.
- late Animation<double> animation;
- @override
- void initState() {
- super.initState();
- controller = AnimationController(duration: widget.duration, vsync: this);
- animation = CurvedAnimation(parent: controller, curve: widget.curve);
- // Have the controller repeat indefinitely. If you want it to "bounce" back
- // and forth, set the reverse parameter to true.
- controller.repeat();
- controller.addListener(() => setState(() {}));
- }
- @override
- void dispose() {
- // Always have to dispose of animation controllers when done.
- controller.dispose();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- // Scale the path values to match the -1.0 to 1.0 domain of the Alignment widget.
- final Offset position =
- widget.path.transform(animation.value) * 2.0 - const Offset(1.0, 1.0);
- return Align(
- alignment: Alignment(position.dx, position.dy),
- child: widget.child,
- );
- }
- }
- class MyApp extends StatelessWidget {
- const MyApp({super.key});
- static const String _title = 'Flutter Code Sample';
- @override
- Widget build(BuildContext context) {
- return const MaterialApp(
- title: _title,
- home: MyStatefulWidget(),
- );
- }
- }
- class MyStatefulWidget extends StatefulWidget {
- const MyStatefulWidget({super.key});
- @override
- State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
- }
- class _MyStatefulWidgetState extends State<MyStatefulWidget> {
- int _selectedIndex = 0;
- static const TextStyle optionStyle =
- TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
- static const List<Widget> _widgetOptions = <Widget>[
- Text(
- 'Index 0: Home',
- style: optionStyle,
- ),
- Text(
- 'Index 1: Business',
- style: optionStyle,
- ),
- Text(
- 'Index 2: School',
- style: optionStyle,
- ),
- ];
- void _onItemTapped(int index) {
- setState(() {
- _selectedIndex = index;
- });
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: const Text('Lab2.17'),
- ),
- drawer: Drawer(
- child: ListView(
- padding: EdgeInsets.zero,
- children: <Widget>[
- DrawerHeader(
- decoration: BoxDecoration(
- color: Colors.blue,
- ),
- child: Text(
- 'Options',
- style: TextStyle(
- color: Colors.white,
- fontSize: 24,
- ),
- ),
- ),
- ...paths.asMap().entries.map((entry){
- return ListTile(
- leading: Icon(Icons.opacity, color: entry.value.color,),
- title: Text(entry.value.colorName),
- onTap: (){
- _onItemTapped(entry.key);
- Navigator.pop(context);
- },
- );
- }),
- ],
- ),
- ),
- body:FollowCurve2D(
- path: paths[_selectedIndex].spline,
- duration: const Duration(seconds: 3),
- child: CircleAvatar(
- backgroundColor: paths[_selectedIndex].color,
- ),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement