Created
August 8, 2018 05:09
-
-
Save gliheng/e2c5ad651469094fff01fb0f05a261cd to your computer and use it in GitHub Desktop.
AnimatedWidget demo
This file contains 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 'radial_logo.dart'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
title: 'Flutter Demo', | |
theme: new ThemeData( | |
primarySwatch: Colors.blue, | |
accentColor: Colors.red, | |
brightness: Brightness.light, | |
), | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text('Flutter Demo') | |
), | |
body: AnimationExample() | |
) | |
); | |
} | |
} | |
class AnimationExample extends StatefulWidget { | |
@override | |
_AnimationExampleState createState() => _AnimationExampleState(); | |
} | |
class _AnimationExampleState extends State<AnimationExample> with TickerProviderStateMixin { | |
AnimationController controller; | |
@override | |
void initState() { | |
super.initState(); | |
controller = AnimationController( | |
vsync: this, duration: Duration(seconds: 5) | |
); | |
controller.forward(); | |
} | |
_onTap() { | |
if (controller.status == AnimationStatus.completed || controller.status == AnimationStatus.forward) { | |
controller.reverse(); | |
} else { | |
controller.forward(); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Center( | |
child: GestureDetector( | |
onTap: _onTap, | |
child: AnimatedBox( | |
listenable: controller, | |
), | |
), | |
); | |
} | |
} | |
class AnimatedBox extends AnimatedWidget { | |
AnimatedBox({listenable}) : super(listenable: listenable); | |
Tween tween = Tween(begin: 100.0, end: 200.0); | |
@override | |
Widget build(BuildContext context) { | |
final Animation<double> animation = listenable; | |
return Container( | |
color: Colors.green, | |
width: tween.evaluate(animation), | |
height: tween.evaluate(animation), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment