Created
June 20, 2019 18:28
-
-
Save liyuqian/81def6f7d5b2126a7c21ae6b7c252c9c to your computer and use it in GitHub Desktop.
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'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
home: Scaffold( | |
body: Center(child: AnimatedRect()), | |
), | |
); | |
} | |
} | |
class AnimatedRect extends StatefulWidget { | |
@override | |
State<StatefulWidget> createState() => _AnimatedRectState(); | |
} | |
class _AnimatedRectState extends State<AnimatedRect> | |
with SingleTickerProviderStateMixin { | |
Animation<double> _opacity; | |
AnimationController _controller; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = | |
AnimationController(vsync: this, duration: const Duration(seconds: 2)); | |
_opacity = Tween<double>(begin: 0, end: 1.0).animate(_controller) | |
..addListener(() { | |
setState(() {}); | |
}) | |
..addStatusListener((status) { | |
if (status == AnimationStatus.completed) { | |
_controller.reverse(); | |
} else if (status == AnimationStatus.dismissed) { | |
_controller.forward(); | |
} | |
}); | |
_controller.forward(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
width: 200, | |
height: 200, | |
color: Colors.blue.withOpacity(_opacity.value)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment