Last active
July 25, 2019 19:55
-
-
Save creativecreatorormaybenot/d23bf64984dd55f2b134e0b5f77a7a34 to your computer and use it in GitHub Desktop.
NonStopTickerProviderMixin
This file contains hidden or 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
import 'package:flutter/material.dart'; | |
import 'package:flutter/scheduler.dart'; | |
mixin NonStopTickerProviderMixin implements TickerProvider { | |
@override | |
Ticker createTicker(TickerCallback onTick) => Ticker(onTick); | |
} | |
void main() { | |
runApp(MaterialApp(home: const Init())); | |
} | |
class Init extends StatelessWidget { | |
const Init({Key key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) => Scaffold( | |
body: Center( | |
child: FlatButton( | |
child: const Text('init'), | |
onPressed: () => Navigator.of(context).push( | |
MaterialPageRoute( | |
builder: (context) => const Page(), | |
), | |
), | |
), | |
), | |
); | |
} | |
class Page extends StatefulWidget { | |
const Page({Key key}) : super(key: key); | |
@override | |
_PageState createState() => _PageState(); | |
} | |
class _PageState extends State<Page> with NonStopTickerProviderMixin { | |
AnimationController animationController; | |
@override | |
void initState() { | |
animationController = AnimationController(duration: Duration(seconds: 1), vsync: this) | |
..forward() | |
..addStatusListener((animationStatus) { | |
if (animationStatus == AnimationStatus.completed) animationController.forward(from: 0); | |
}); | |
super.initState(); | |
} | |
@override | |
void dispose() { | |
animationController.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) => Scaffold( | |
body: AnimatedBuilder( | |
animation: animationController, | |
builder: (context, child) { | |
print('_PageState.build -> AnimatedBuilder.builder'); | |
return Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
FlatButton( | |
child: const Text('push'), | |
onPressed: () => Navigator.of(context).push(MaterialPageRoute(builder: (context) => Container())), | |
), | |
FlatButton( | |
child: const Text('pop'), | |
onPressed: Navigator.of(context).pop, | |
), | |
], | |
), | |
); | |
}, | |
), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment