Advertisement
EWTD

lab2

Jan 8th, 2024
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 5.37 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2.  
  3. void main() => runApp(const MyApp());
  4.  
  5. class ExampleDestination {
  6.   const ExampleDestination(this.color, this.colorName, this.spline);
  7.   final Color color;
  8.   final String colorName;
  9.   final CatmullRomSpline spline;
  10. }
  11.  
  12. List<ExampleDestination> paths = <ExampleDestination>[
  13.   ExampleDestination( Colors.red, "Red", CatmullRomSpline(
  14.     const <Offset>[
  15.       Offset(0.05, 0.75),
  16.       Offset(0.18, 0.23),
  17.       Offset(0.32, 0.04),
  18.       Offset(0.73, 0.5),
  19.       Offset(0.42, 0.74),
  20.       Offset(0.73, 0.01),
  21.       Offset(0.93, 0.93),
  22.       Offset(0.05, 0.75),
  23.     ],
  24.     startHandle: const Offset(0.93, 0.93),
  25.     endHandle: const Offset(0.18, 0.23),
  26.   )),
  27.   ExampleDestination( Colors.green, "Green", CatmullRomSpline(
  28.     const <Offset>[
  29.       Offset(0.45, 0.52),
  30.       Offset(0.75, 0.48),
  31.       Offset(0.27, 0.89),
  32.       Offset(0.91, 0.45),
  33.       Offset(0.47, 0.74),
  34.       Offset(0.29, 0.29),
  35.       Offset(0.60, 0.32),
  36.       Offset(0.92, 0.84),
  37.     ],
  38.     startHandle: const Offset(0.92, 0.40),
  39.     endHandle: const Offset(0.60, 0.64),
  40.   )),
  41.   ExampleDestination( Colors.blue, "Blue", CatmullRomSpline(
  42.     const <Offset>[
  43.       Offset(0.03, 0.71),
  44.       Offset(0.96, 0.39),
  45.       Offset(0.16, 0.20),
  46.       Offset(0.23, 0.33),
  47.       Offset(0.94, 0.83),
  48.       Offset(0.78, 0.67),
  49.       Offset(0.79, 0.98),
  50.       Offset(0.22, 0.19),
  51.     ],
  52.     startHandle: const Offset(0.96, 0.92),
  53.     endHandle: const Offset(0.52, 0.63),
  54.   ))
  55. ];
  56.  
  57. class FollowCurve2D extends StatefulWidget {
  58.   const FollowCurve2D({
  59.     super.key,
  60.     required this.path,
  61.     this.curve = Curves.linear,
  62.     required this.child,
  63.     this.duration = const Duration(seconds: 1),
  64.   });
  65.  
  66.   final Curve2D path;
  67.   final Curve curve;
  68.   final Duration duration;
  69.   final Widget child;
  70.  
  71.   @override
  72.   State<FollowCurve2D> createState() => _FollowCurve2DState();
  73. }
  74.  
  75. class _FollowCurve2DState extends State<FollowCurve2D>
  76.     with TickerProviderStateMixin {
  77.   // The animation controller for this animation.
  78.   late AnimationController controller;
  79.   // The animation that will be used to apply the widget's animation curve.
  80.   late Animation<double> animation;
  81.  
  82.   @override
  83.   void initState() {
  84.     super.initState();
  85.     controller = AnimationController(duration: widget.duration, vsync: this);
  86.     animation = CurvedAnimation(parent: controller, curve: widget.curve);
  87.     // Have the controller repeat indefinitely.  If you want it to "bounce" back
  88.     // and forth, set the reverse parameter to true.
  89.     controller.repeat();
  90.     controller.addListener(() => setState(() {}));
  91.   }
  92.  
  93.   @override
  94.   void dispose() {
  95.     // Always have to dispose of animation controllers when done.
  96.     controller.dispose();
  97.     super.dispose();
  98.   }
  99.  
  100.   @override
  101.   Widget build(BuildContext context) {
  102.     // Scale the path values to match the -1.0 to 1.0 domain of the Alignment widget.
  103.     final Offset position =
  104.         widget.path.transform(animation.value) * 2.0 - const Offset(1.0, 1.0);
  105.     return Align(
  106.       alignment: Alignment(position.dx, position.dy),
  107.       child: widget.child,
  108.     );
  109.   }
  110. }
  111.  
  112. class MyApp extends StatelessWidget {
  113.   const MyApp({super.key});
  114.  
  115.   static const String _title = 'Flutter Code Sample';
  116.  
  117.   @override
  118.   Widget build(BuildContext context) {
  119.     return const MaterialApp(
  120.       title: _title,
  121.       home: MyStatefulWidget(),
  122.     );
  123.   }
  124. }
  125.  
  126. class MyStatefulWidget extends StatefulWidget {
  127.   const MyStatefulWidget({super.key});
  128.  
  129.   @override
  130.   State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
  131. }
  132.  
  133. class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  134.   int _selectedIndex = 0;
  135.   static const TextStyle optionStyle =
  136.   TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  137.   static const List<Widget> _widgetOptions = <Widget>[
  138.     Text(
  139.       'Index 0: Home',
  140.       style: optionStyle,
  141.     ),
  142.     Text(
  143.       'Index 1: Business',
  144.       style: optionStyle,
  145.     ),
  146.     Text(
  147.       'Index 2: School',
  148.       style: optionStyle,
  149.     ),
  150.   ];
  151.  
  152.   void _onItemTapped(int index) {
  153.     setState(() {
  154.       _selectedIndex = index;
  155.     });
  156.   }
  157.  
  158.   @override
  159.   Widget build(BuildContext context) {
  160.     return Scaffold(
  161.       appBar: AppBar(
  162.         title: const Text('Lab2.17'),
  163.       ),
  164.       drawer: Drawer(
  165.         child: ListView(
  166.           padding: EdgeInsets.zero,
  167.           children: <Widget>[
  168.             DrawerHeader(
  169.               decoration: BoxDecoration(
  170.                 color: Colors.blue,
  171.               ),
  172.               child: Text(
  173.                 'Options',
  174.                 style: TextStyle(
  175.                   color: Colors.white,
  176.                   fontSize: 24,
  177.                 ),
  178.               ),
  179.             ),
  180.             ...paths.asMap().entries.map((entry){
  181.               return ListTile(
  182.                 leading: Icon(Icons.opacity, color: entry.value.color,),
  183.                 title: Text(entry.value.colorName),
  184.                 onTap: (){
  185.                   _onItemTapped(entry.key);
  186.                   Navigator.pop(context);
  187.                 },
  188.               );
  189.             }),
  190.           ],
  191.         ),
  192.       ),
  193.       body:FollowCurve2D(
  194.         path: paths[_selectedIndex].spline,
  195.         duration: const Duration(seconds: 3),
  196.         child: CircleAvatar(
  197.           backgroundColor: paths[_selectedIndex].color,
  198.         ),
  199.       ),
  200.     );
  201.   }
  202. }
  203.  
  204.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement