Flutter AnimatedBuilder

创建动画的widget

Key key,

@required Listenable animation,

@required this.builder,

this.child,

animation:Animationcontroller //动画

child 动画作用的view

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

import 'package:flutter/material.dart';

main()=>runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: '/buildAnimation',
routes: {
'/':(context)=>AnimationDemo(),
'/test':(context)=>TestAnimation(),
'/buildAnimation':(context)=>BuildAnimation(),
},
);
}
} /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class BuildAnimation extends StatefulWidget{
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return BuildAnimationState();
}
} class BuildAnimationState extends State<BuildAnimation> with TickerProviderStateMixin{
AnimationController animationController;
var animation; @override
void initState() {
super.initState();
animationController = AnimationController(
duration: Duration(seconds: 2),
vsync: this);
animation = Tween(begin: 20, end: 200).animate(animationController);
animationController.forward(); //放到某个其他地方去启动, 会唤起Builder
} @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('builder'),),
body: Container(
width: double.infinity,
height: double.infinity,
child: Stack(
children: <Widget>[
AnimatedBuilder(
animation: animation,
builder: (context, Widget child){ //child 可以定义要动的东西....这里先暂时省略了.
return Positioned(
left: animation.value/1.0,
top: animation.value/1.0,
child: Container(child: RaisedButton(
child: Text('abc'),onPressed: (){
print(animation.value);
}),),
);
},
),
],
),
),
);
}
} ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class TestAnimation extends StatefulWidget{
@override
State<StatefulWidget> createState() {
return TestAnimationState();
}
} class TestAnimationState extends State<TestAnimation> with TickerProviderStateMixin{
AnimationController animationController;
Tween positionTween = Tween(begin: 20, end: 200);
var animation; @override
void initState() {
super.initState();
animationController = AnimationController(
vsync: this,
duration: Duration(seconds: 1),
);
animation = positionTween.animate(animationController); animationController.addListener((){
print(animationController.value);
});
animationController.addStatusListener((value){
print('status: $value');
});
} @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('test'),),
body: Container(
width: double.infinity,
height: double.infinity,
color: Colors.black12,
child: TestAnimationDemo(animationController: animationController,animation:animation),
),
);
}
} class TestAnimationDemo extends AnimatedWidget {
AnimationController animationController;
Animation animation;
TestAnimationDemo({this.animationController, this.animation}):super(listenable:animationController); @override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
Positioned(
left: animation.value/1.0,
top: animation.value/1.0,
child: Container(
child: IconButton(icon: Icon(Icons.forward), onPressed: (){
animationController.forward();
}),
),
),
],
);
}
} /////////////////////////////////////////////////////////////////////////////////////////////////////////
class AnimationDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('AnimationDemo'),
elevation: 0.0,
),
body: AnimationDemoHome());
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
class AnimationDemoHome extends StatefulWidget {
@override
_AnimationDemoHomeState createState() => _AnimationDemoHomeState();
} class _AnimationDemoHomeState extends State<AnimationDemoHome>
with TickerProviderStateMixin {
AnimationController animationDemoController;
Animation animation;
Animation animationColor;
CurvedAnimation curve; @override
void initState() {
super.initState();
animationDemoController = AnimationController(
// value: 32.0,
// lowerBound: 32.0,
// upperBound: 100.0,
duration: Duration(milliseconds: 1000),
vsync: this,
); curve = CurvedAnimation(
parent: animationDemoController, curve: Curves.bounceOut); animation = Tween(begin: 32.0, end: 100.0).animate(curve);
animationColor =
ColorTween(begin: Colors.red, end: Colors.red[900]).animate(curve);
// animationDemoController.addListener(() {
// // print('${animationDemoController.value}');
// setState(() {});
// });
animationDemoController.addStatusListener((AnimationStatus status) {
print(status);
});
// animationDemoController.forward();
} @override
void dispose() {
super.dispose();
animationDemoController.dispose();
} @override
Widget build(BuildContext context) {
return Center(
child: AnimatedHeart(
animations: [
animation,
animationColor,
],
controller: animationDemoController,
),
);
}
} class AnimatedHeart extends AnimatedWidget {
final List animations;
final AnimationController controller; AnimatedHeart({
this.animations,
this.controller,
}) : super(listenable: controller); @override
Widget build(BuildContext context) {
return IconButton(
icon: Icon(Icons.favorite),
iconSize: animations[0].value,
color: animations[1].value,
onPressed: () {
switch (controller.status) {
case AnimationStatus.completed:
controller.reverse();
break;
default:
controller.forward();
}
},
);
}
}

  

 

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. 电商ERP系统——商品SKU与库存设计

    面试题经常问道,如何设计库存,哪些库存呢?分类属性的库存:不同颜色 不同尺码的属性的库存,这时候需要针对具体的SKU商品创建表. 总体思路 1.商品关联商品类别,商品类别关联多个商品属性,其中指定某几 ...

  2. Android A/B System OTA分析(一)概览【转】

    本文转载自:https://blog.csdn.net/guyongqiangx/article/details/71334889 Android从7.0开始引入新的OTA升级方式,A/B Syste ...

  3. python3.6安装 zipimport.ZipImportError: can't decompress data; zlib not available【转】

    python3.6.3 安装: .tgz cd Python- ./configure make make altinstall `make altinstall` , 报错: zipimport.Z ...

  4. vue elementUi tree 懒加载使用详情

    背景:vue下使用elementUI 文档:http://element-cn.eleme.io/#/zh-CN/component/tree#tree-shu-xing-kong-jian 需求:只 ...

  5. 搭建Bitcoin全节点

    节点搭建 1. 进入 bitcoin 选择 Choose your wallet 2. 选择 Bitcoin Core for Linux 下载 bitcoin-0.17.0.1-x86_64-lin ...

  6. Python3基础 yield send 获得生成器后,需要先启动一次

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

  7. FreeSWITCH命令大全

    FreeSWITCH启动.查看.及关闭 FreeSWITCH一般安装在路径 /usr/local/freeswitch ,可执行程序位于/usr/local/freeswitch/bin 下,配置文件 ...

  8. 一篇文章学会shell脚本

    一.Shell传递参数 #!/bin/bash # 假设在脚本运行时写了三个参数 ..,,则 "(传递了三个参数). echo "-- \$* 演示 --" for i ...

  9. 实现RTSP摄像机硬盘录像机NVR网站网页微信H5直播方案EasyNVR部署问题之:ERR_CONTENT_LENGTH_MISMATCH

    背景分析 接触到EasyNVR产品的开发者都知道,EasyNVR是一套功能齐全.简洁易用的流媒体解决方案,可作为能力曾前端接入摄像头,后端接入业务系统使用,也可以作为应用层,直接修改为属于企业用户自己 ...

  10. js 类型判断