Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:flutter/animation.dart';
- import 'package:scoped_model/scoped_model.dart';
- void main() {
- runApp(MaterialApp(
- title: 'Example',
- home: MyApp(),
- ));
- }
- class MyApp extends StatefulWidget {
- @override
- MyAppState createState() => MyAppState();
- }
- class MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
- Animation animation;
- AnimationController animationController;
- @override
- void initState() {
- animationController =
- AnimationController(duration: Duration(milliseconds: 100), vsync: this);
- animation = Tween(begin: 0.0, end: 50.0).animate(animationController);
- super.initState();
- }
- @override
- Widget build(BuildContext context) {
- final model = AppModel(animation, animationController);
- return Scaffold(
- body: ScopedModel<AppModel>(
- model: model,
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: <Widget>[
- ButtonAnimation(),
- Container(
- width: 100.0,
- height: 100.0,
- ),
- RaisedButton(
- child: Text('Stop Animation'),
- onPressed: () {
- animationController.stop();
- },
- )
- ])));
- }
- @override
- void dispose() {
- animationController.dispose();
- super.dispose();
- }
- }
- class ButtonAnimation extends StatefulWidget {
- @override
- ButtonState createState() => ButtonState();
- }
- class ButtonState extends State<ButtonAnimation> {
- @override
- Widget build(BuildContext context) {
- return ScopedModelDescendant<AppModel>(
- builder: (context, child, model) => Center(
- child: Container(
- margin: EdgeInsets.only(left: model.animation.value ?? 0.0),
- child: RaisedButton(
- child: Text("Push Me"),
- onPressed: () {
- model.animation.addListener(() => setState(() {}));
- model.onPressed();
- },
- ),
- ),
- ));
- }
- }
- class AppModel extends Model {
- final Animation animation;
- final AnimationController animationController;
- AppModel._({this.animation, this.animationController});
- factory AppModel(Animation anim, AnimationController controller) {
- return AppModel._(animation: anim, animationController: controller);
- }
- void onPressed() {
- animation.addStatusListener((status) {
- if (status == AnimationStatus.completed) {
- animationController.reverse();
- } else if (status == AnimationStatus.dismissed) {
- animationController.forward();
- }
- });
- animationController.forward();
- print('Pushed the Button');
- print(animation.value.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement