SHARE
TWEET

Restore AnimationController State Upon returning to Page

algcod Nov 19th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Context: https://stackoverflow.com/questions/58775671/restore-animationcontroller-state-upon-returning-to-page
  2.  
  3. import 'package:flutter/material.dart';
  4. import 'package:provider/provider.dart';
  5.  
  6. void main() => runApp(MyApp());
  7.  
  8. class MyApp extends StatelessWidget {
  9.   @override
  10.   Widget build(BuildContext context) {
  11.     return Provider<CounterStateModel>(
  12.       builder: (context) => CounterStateModel(),
  13.       child: MaterialApp(
  14.         title: 'Flutter Demo',
  15.         theme: ThemeData(
  16.           primarySwatch: Colors.blue,
  17.         ),
  18.         home: MyHomePage(title: 'Flutter Demo Home Page'),
  19.       ),
  20.     );
  21.   }
  22. }
  23.  
  24. class MyHomePage extends StatefulWidget {
  25.   MyHomePage({Key key, this.title}) : super(key: key);
  26.  
  27.   final String title;
  28.  
  29.   @override
  30.   _MyHomePageState createState() => _MyHomePageState();
  31. }
  32.  
  33. class _MyHomePageState extends State<MyHomePage> {
  34.   @override
  35.   Widget build(BuildContext context) {
  36.     return Scaffold(
  37.       appBar: AppBar(
  38.         title: Text(widget.title),
  39.       ),
  40.       body: Center(
  41.         child: Column(
  42.           mainAxisAlignment: MainAxisAlignment.center,
  43.           children: <Widget>[
  44.             FlatButton(
  45.               color: Colors.blue,
  46.               child: Text("AnimatonController"),
  47.               onPressed: () {
  48.                 Navigator.push(
  49.                   context,
  50.                   MaterialPageRoute(builder: (context) => Counter()),
  51.                 );
  52.               },
  53.             ),
  54.           ],
  55.         ),
  56.       ),
  57.     );
  58.   }
  59. }
  60.  
  61. class CounterText extends AnimatedWidget {
  62.   CounterText({Key key, this.animation})
  63.       : super(key: key, listenable: animation);
  64.   Animation<int> animation;
  65.  
  66.   @override
  67.   build(BuildContext context) {
  68.     return new Text(
  69.       animation.value.toString(),
  70.       style: new TextStyle(fontSize: 200.0),
  71.     );
  72.   }
  73. }
  74.  
  75. class CounterStateModel {
  76.   AnimationController _controller;
  77.   int startCount = 10;
  78. }
  79.  
  80. class Counter extends StatefulWidget {
  81.   State createState() => new _CounterState();
  82. }
  83.  
  84. class _CounterState extends State<Counter> with TickerProviderStateMixin {
  85.   AnimationController _controller;
  86.  
  87.   @override
  88.   void initState() {
  89.     super.initState();
  90.   }
  91.  
  92.   @override
  93.   Widget build(BuildContext context) {
  94.     CounterStateModel _counterStateModel =
  95.         Provider.of<CounterStateModel>(context);
  96.     if (_counterStateModel._controller == null) {
  97.       _counterStateModel._controller = new AnimationController(
  98.         vsync: this,
  99.         duration: new Duration(seconds: _counterStateModel.startCount),
  100.       );
  101.       _counterStateModel._controller = _controller;
  102.     } else {
  103.       _controller = _counterStateModel._controller;
  104.     }
  105.     return new Scaffold(
  106.       floatingActionButton: new FloatingActionButton(
  107.           child: Icon(_controller.isAnimating ? Icons.pause : Icons.play_arrow),
  108.           onPressed: () {
  109.             setState(() {
  110.               _controller.isAnimating
  111.                   ? _controller.stop()
  112.                   : _controller.forward();
  113.             });
  114.           }),
  115.       body: new Container(
  116.         child: new Center(
  117.           child: new CounterText(
  118.             animation: new StepTween(
  119.               begin: _counterStateModel.startCount,
  120.               end: 0,
  121.             ).animate(_controller),
  122.           ),
  123.         ),
  124.       ),
  125.     );
  126.   }
  127. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top