Skip to content

Instantly share code, notes, and snippets.

@creativecreatorormaybenot
Last active July 25, 2019 19:55
Show Gist options
  • Save creativecreatorormaybenot/d23bf64984dd55f2b134e0b5f77a7a34 to your computer and use it in GitHub Desktop.
Save creativecreatorormaybenot/d23bf64984dd55f2b134e0b5f77a7a34 to your computer and use it in GitHub Desktop.
NonStopTickerProviderMixin
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