Flutter AnimatedBuilder

创建动画的widget

Key key,

@required Listenable animation,

@required this.builder,

this.child,

animation:Animationcontroller //动画

child 动画作用的view

builder:每次controller值改变都会回到builder 重新生成view

  1. import 'package:flutter/material.dart';
  2.  
  3. main()=>runApp(MyApp());
  4.  
  5. class MyApp extends StatelessWidget {
  6. @override
  7. Widget build(BuildContext context) {
  8. return MaterialApp(
  9. initialRoute: '/buildAnimation',
  10. routes: {
  11. '/':(context)=>AnimationDemo(),
  12. '/test':(context)=>TestAnimation(),
  13. '/buildAnimation':(context)=>BuildAnimation(),
  14. },
  15. );
  16. }
  17. }
  18.  
  19. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  20.  
  21. class BuildAnimation extends StatefulWidget{
  22. @override
  23. State<StatefulWidget> createState() {
  24. // TODO: implement createState
  25. return BuildAnimationState();
  26. }
  27. }
  28.  
  29. class BuildAnimationState extends State<BuildAnimation> with TickerProviderStateMixin{
  30. AnimationController animationController;
  31. var animation;
  32.  
  33. @override
  34. void initState() {
  35. super.initState();
  36. animationController = AnimationController(
  37. duration: Duration(seconds: 2),
  38. vsync: this);
  39. animation = Tween(begin: 20, end: 200).animate(animationController);
  40. animationController.forward(); //放到某个其他地方去启动, 会唤起Builder
  41. }
  42.  
  43. @override
  44. Widget build(BuildContext context) {
  45. return Scaffold(
  46. appBar: AppBar(title: Text('builder'),),
  47. body: Container(
  48. width: double.infinity,
  49. height: double.infinity,
  50. child: Stack(
  51. children: <Widget>[
  52. AnimatedBuilder(
  53. animation: animation,
  54. builder: (context, Widget child){ //child 可以定义要动的东西....这里先暂时省略了.
  55. return Positioned(
  56. left: animation.value/1.0,
  57. top: animation.value/1.0,
  58. child: Container(child: RaisedButton(
  59. child: Text('abc'),onPressed: (){
  60. print(animation.value);
  61. }),),
  62. );
  63. },
  64. ),
  65. ],
  66. ),
  67. ),
  68. );
  69. }
  70. }
  71.  
  72. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  73. class TestAnimation extends StatefulWidget{
  74. @override
  75. State<StatefulWidget> createState() {
  76. return TestAnimationState();
  77. }
  78. }
  79.  
  80. class TestAnimationState extends State<TestAnimation> with TickerProviderStateMixin{
  81. AnimationController animationController;
  82. Tween positionTween = Tween(begin: 20, end: 200);
  83. var animation;
  84.  
  85. @override
  86. void initState() {
  87. super.initState();
  88. animationController = AnimationController(
  89. vsync: this,
  90. duration: Duration(seconds: 1),
  91. );
  92. animation = positionTween.animate(animationController);
  93.  
  94. animationController.addListener((){
  95. print(animationController.value);
  96. });
  97. animationController.addStatusListener((value){
  98. print('status: $value');
  99. });
  100. }
  101.  
  102. @override
  103. Widget build(BuildContext context) {
  104. return Scaffold(
  105. appBar: AppBar(title: Text('test'),),
  106. body: Container(
  107. width: double.infinity,
  108. height: double.infinity,
  109. color: Colors.black12,
  110. child: TestAnimationDemo(animationController: animationController,animation:animation),
  111. ),
  112. );
  113. }
  114. }
  115.  
  116. class TestAnimationDemo extends AnimatedWidget {
  117. AnimationController animationController;
  118. Animation animation;
  119. TestAnimationDemo({this.animationController, this.animation}):super(listenable:animationController);
  120.  
  121. @override
  122. Widget build(BuildContext context) {
  123. return Stack(
  124. children: <Widget>[
  125. Positioned(
  126. left: animation.value/1.0,
  127. top: animation.value/1.0,
  128. child: Container(
  129. child: IconButton(icon: Icon(Icons.forward), onPressed: (){
  130. animationController.forward();
  131. }),
  132. ),
  133. ),
  134. ],
  135. );
  136. }
  137. }
  138.  
  139. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  140. class AnimationDemo extends StatelessWidget {
  141. @override
  142. Widget build(BuildContext context) {
  143. return Scaffold(
  144. appBar: AppBar(
  145. title: Text('AnimationDemo'),
  146. elevation: 0.0,
  147. ),
  148. body: AnimationDemoHome());
  149. }
  150. }
  151. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  152. class AnimationDemoHome extends StatefulWidget {
  153. @override
  154. _AnimationDemoHomeState createState() => _AnimationDemoHomeState();
  155. }
  156.  
  157. class _AnimationDemoHomeState extends State<AnimationDemoHome>
  158. with TickerProviderStateMixin {
  159. AnimationController animationDemoController;
  160. Animation animation;
  161. Animation animationColor;
  162. CurvedAnimation curve;
  163.  
  164. @override
  165. void initState() {
  166. super.initState();
  167. animationDemoController = AnimationController(
  168. // value: 32.0,
  169. // lowerBound: 32.0,
  170. // upperBound: 100.0,
  171. duration: Duration(milliseconds: 1000),
  172. vsync: this,
  173. );
  174.  
  175. curve = CurvedAnimation(
  176. parent: animationDemoController, curve: Curves.bounceOut);
  177.  
  178. animation = Tween(begin: 32.0, end: 100.0).animate(curve);
  179. animationColor =
  180. ColorTween(begin: Colors.red, end: Colors.red[900]).animate(curve);
  181. // animationDemoController.addListener(() {
  182. // // print('${animationDemoController.value}');
  183. // setState(() {});
  184. // });
  185. animationDemoController.addStatusListener((AnimationStatus status) {
  186. print(status);
  187. });
  188. // animationDemoController.forward();
  189. }
  190.  
  191. @override
  192. void dispose() {
  193. super.dispose();
  194. animationDemoController.dispose();
  195. }
  196.  
  197. @override
  198. Widget build(BuildContext context) {
  199. return Center(
  200. child: AnimatedHeart(
  201. animations: [
  202. animation,
  203. animationColor,
  204. ],
  205. controller: animationDemoController,
  206. ),
  207. );
  208. }
  209. }
  210.  
  211. class AnimatedHeart extends AnimatedWidget {
  212. final List animations;
  213. final AnimationController controller;
  214.  
  215. AnimatedHeart({
  216. this.animations,
  217. this.controller,
  218. }) : super(listenable: controller);
  219.  
  220. @override
  221. Widget build(BuildContext context) {
  222. return IconButton(
  223. icon: Icon(Icons.favorite),
  224. iconSize: animations[0].value,
  225. color: animations[1].value,
  226. onPressed: () {
  227. switch (controller.status) {
  228. case AnimationStatus.completed:
  229. controller.reverse();
  230. break;
  231. default:
  232. controller.forward();
  233. }
  234. },
  235. );
  236. }
  237. }

  

 

Flutter Animation AnimatedBuilder的更多相关文章

  1. 【Flutter学习】之动画实现原理浅析(三)

    一,概述 Flutter动画库的核心类是Animation对象,它生成指导动画的值,Animation对象指导动画的当前状态(例如,是开始.停止还是向前或者向后移动),但它不知道屏幕上显示的内容.动画 ...

  2. 《Flutter 动画系列一》25种动画组件超全总结

    动画运行的原理 任何程序的动画原理都是一样的,即:视觉暂留,视觉暂留又叫视觉暂停,人眼在观察景物时,光信号传入大脑神经,需经过一段短暂的时间,光的作用结束后,视觉形象并不立即消失,这种残留的视觉称&q ...

  3. flutter 自己整理

    2018-05 资料 常见问题解决处 https://flutter.io/flutter-for-android/ 起步 api widget https://flutter.io/docs/ 其他 ...

  4. Flutter仿网易云音乐:播放界面

    写在前头 本来是要做一个仿网易云音乐的flutter项目,但是因为最近事情比较多,项目周期跨度会比较长,因此分几个步骤来完成.这是仿网易云音乐项目系列文章的第一篇.没有完全照搬网易云音乐的UI,借鉴了 ...

  5. Flutter Vignettes

    Flutter Vignettes Flutter Animation https://flutter.gskinner.com/ https://github.com/gskinnerTeam/fl ...

  6. Flutter Widget API

    Flutter Widget API https://api.flutter.dev/ https://api.flutter.dev/flutter/material/material-librar ...

  7. dart系列之:创建Library package

    目录 简介 Library package的结构 导入library 条件导入和导出library 添加其他有效的文件 library的文档 发布到pub.dev 总结 简介 在dart系统中,有pu ...

  8. flutter 的Animation简单了解

    import 'package:flutter/material.dart'; class AnimationDemo extends StatelessWidget { @override Widg ...

  9. 【译】使用 Flutter 实现跨平台移动端开发

    作者: Mike Bluestein   | 原文地址:[https://www.smashingmagazine.com/2018/06/google-flutter-mobile-developm ...

随机推荐

  1. Hadoop版本升级(2.7.6 => 3.1.2)

    自己的主机上的Hadoop版本是2.7.6,是测试用的伪分布式Hadoop,在前段时间部署了Hive on Spark,但由于没有做好功课,导致了Hive无法正常启动,原因在于Hive 3.x版本不适 ...

  2. Systemback制作大于4G的Ubuntu系统镜像

    1 安装Systemback 依此执行如下命令. sudo apt-get update sudo add-apt-repository ppa:nemh/systemback sudo apt-ge ...

  3. Python script to package the information of tracking benchmarks like LaSOT and GOT-10k into json files for Siamese network based trackers

    ############################################################################################ #### Fo ...

  4. Python selenium PO By.XPATH定位元素报错

    Python selenium PO  By.XPATH定位元素报错 如下代码经常报错: # 首页的“新建投放计划”按钮 new_ads_plan = (By.XPATH, "//*[tex ...

  5. Xamarin图表开发基础教程(9)OxyPlot框架

    Xamarin图表开发基础教程(9)OxyPlot框架 OxyPlot组件构成 OxyPlot组件主要由两个类构成,分别为PlotView和PlotModel.这两个类我们在上文中也使用到了.本节将讲 ...

  6. Win10 cmd控制台程序会被鼠标单击暂停的解决办法

    右键单击顶部白框,选择属性或默认值,将快速编辑模式的勾取消就可以了,最后记得点击确定

  7. Python3基础 tuple list转为tuple

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  8. vue-qriously 生成二维码并下载、cliploard复制粘贴

    xxx.vue <template> <a-modal class="dialogRecharge" title="活动链接及二维码" v-m ...

  9. spring cloud Eureka server 问题 Spring Cloud java.lang.TypeNotPresentException

    版本: spring-cloud.version : Greenwich.SR2 pom配置: <project xmlns="http://maven.apache.org/POM/ ...

  10. Could not get JDBC Connection; nested exception is java.sql.SQLException: ${jdbc.driver}

    在一个SSM分布式项目中一个服务报错: ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnec ...