Advertisement
Guest User

shaking-button-scoped-model-flutter-example-quick

a guest
Apr 25th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.01 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/animation.dart';
  3. import 'package:scoped_model/scoped_model.dart';
  4.  
  5. void main() {
  6.   runApp(MaterialApp(
  7.     title: 'Example',
  8.     home: MyApp(),
  9.   ));
  10. }
  11.  
  12. class MyApp extends StatefulWidget {
  13.   @override
  14.   MyAppState createState() => MyAppState();
  15. }
  16.  
  17. class MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
  18.   Animation animation;
  19.   AnimationController animationController;
  20.  
  21.   @override
  22.   void initState() {
  23.     animationController =
  24.         AnimationController(duration: Duration(milliseconds: 100), vsync: this);
  25.     animation = Tween(begin: 0.0, end: 50.0).animate(animationController);
  26.     super.initState();
  27.   }
  28.  
  29.   @override
  30.   Widget build(BuildContext context) {
  31.     final model = AppModel(animation, animationController);
  32.     return Scaffold(
  33.         body: ScopedModel<AppModel>(
  34.             model: model,
  35.             child: Column(
  36.                 mainAxisAlignment: MainAxisAlignment.center,
  37.                 crossAxisAlignment: CrossAxisAlignment.stretch,
  38.                 children: <Widget>[
  39.                   ButtonAnimation(),
  40.                   Container(
  41.                     width: 100.0,
  42.                     height: 100.0,
  43.                   ),
  44.                   RaisedButton(
  45.                     child: Text('Stop Animation'),
  46.                     onPressed: () {
  47.                       animationController.stop();
  48.                     },
  49.                   )
  50.                 ])));
  51.   }
  52.  
  53.   @override
  54.   void dispose() {
  55.     animationController.dispose();
  56.     super.dispose();
  57.   }
  58. }
  59.  
  60. class ButtonAnimation extends StatefulWidget {
  61.   @override
  62.   ButtonState createState() => ButtonState();
  63. }
  64.  
  65. class ButtonState extends State<ButtonAnimation> {
  66.   @override
  67.   Widget build(BuildContext context) {
  68.     return ScopedModelDescendant<AppModel>(
  69.         builder: (context, child, model) => Center(
  70.               child: Container(
  71.                 margin: EdgeInsets.only(left: model.animation.value ?? 0.0),
  72.                 child: RaisedButton(
  73.                   child: Text("Push Me"),
  74.                   onPressed: () {
  75.                     model.animation.addListener(() => setState(() {}));
  76.                     model.onPressed();
  77.                   },
  78.                 ),
  79.               ),
  80.             ));
  81.   }
  82. }
  83.  
  84. class AppModel extends Model {
  85.   final Animation animation;
  86.   final AnimationController animationController;
  87.  
  88.   AppModel._({this.animation, this.animationController});
  89.  
  90.   factory AppModel(Animation anim, AnimationController controller) {
  91.     return AppModel._(animation: anim, animationController: controller);
  92.   }
  93.  
  94.   void onPressed() {
  95.     animation.addStatusListener((status) {
  96.       if (status == AnimationStatus.completed) {
  97.         animationController.reverse();
  98.       } else if (status == AnimationStatus.dismissed) {
  99.         animationController.forward();
  100.       }
  101.     });
  102.     animationController.forward();
  103.     print('Pushed the Button');
  104.     print(animation.value.toString());
  105.   }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement