Flutter Animation AnimatedBuilder
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的更多相关文章
- 【Flutter学习】之动画实现原理浅析(三)
一,概述 Flutter动画库的核心类是Animation对象,它生成指导动画的值,Animation对象指导动画的当前状态(例如,是开始.停止还是向前或者向后移动),但它不知道屏幕上显示的内容.动画 ...
- 《Flutter 动画系列一》25种动画组件超全总结
动画运行的原理 任何程序的动画原理都是一样的,即:视觉暂留,视觉暂留又叫视觉暂停,人眼在观察景物时,光信号传入大脑神经,需经过一段短暂的时间,光的作用结束后,视觉形象并不立即消失,这种残留的视觉称&q ...
- flutter 自己整理
2018-05 资料 常见问题解决处 https://flutter.io/flutter-for-android/ 起步 api widget https://flutter.io/docs/ 其他 ...
- Flutter仿网易云音乐:播放界面
写在前头 本来是要做一个仿网易云音乐的flutter项目,但是因为最近事情比较多,项目周期跨度会比较长,因此分几个步骤来完成.这是仿网易云音乐项目系列文章的第一篇.没有完全照搬网易云音乐的UI,借鉴了 ...
- Flutter Vignettes
Flutter Vignettes Flutter Animation https://flutter.gskinner.com/ https://github.com/gskinnerTeam/fl ...
- Flutter Widget API
Flutter Widget API https://api.flutter.dev/ https://api.flutter.dev/flutter/material/material-librar ...
- dart系列之:创建Library package
目录 简介 Library package的结构 导入library 条件导入和导出library 添加其他有效的文件 library的文档 发布到pub.dev 总结 简介 在dart系统中,有pu ...
- flutter 的Animation简单了解
import 'package:flutter/material.dart'; class AnimationDemo extends StatelessWidget { @override Widg ...
- 【译】使用 Flutter 实现跨平台移动端开发
作者: Mike Bluestein | 原文地址:[https://www.smashingmagazine.com/2018/06/google-flutter-mobile-developm ...
随机推荐
- Hadoop版本升级(2.7.6 => 3.1.2)
自己的主机上的Hadoop版本是2.7.6,是测试用的伪分布式Hadoop,在前段时间部署了Hive on Spark,但由于没有做好功课,导致了Hive无法正常启动,原因在于Hive 3.x版本不适 ...
- Systemback制作大于4G的Ubuntu系统镜像
1 安装Systemback 依此执行如下命令. sudo apt-get update sudo add-apt-repository ppa:nemh/systemback sudo apt-ge ...
- Python script to package the information of tracking benchmarks like LaSOT and GOT-10k into json files for Siamese network based trackers
############################################################################################ #### Fo ...
- Python selenium PO By.XPATH定位元素报错
Python selenium PO By.XPATH定位元素报错 如下代码经常报错: # 首页的“新建投放计划”按钮 new_ads_plan = (By.XPATH, "//*[tex ...
- Xamarin图表开发基础教程(9)OxyPlot框架
Xamarin图表开发基础教程(9)OxyPlot框架 OxyPlot组件构成 OxyPlot组件主要由两个类构成,分别为PlotView和PlotModel.这两个类我们在上文中也使用到了.本节将讲 ...
- Win10 cmd控制台程序会被鼠标单击暂停的解决办法
右键单击顶部白框,选择属性或默认值,将快速编辑模式的勾取消就可以了,最后记得点击确定
- Python3基础 tuple list转为tuple
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- vue-qriously 生成二维码并下载、cliploard复制粘贴
xxx.vue <template> <a-modal class="dialogRecharge" title="活动链接及二维码" v-m ...
- spring cloud Eureka server 问题 Spring Cloud java.lang.TypeNotPresentException
版本: spring-cloud.version : Greenwich.SR2 pom配置: <project xmlns="http://maven.apache.org/POM/ ...
- Could not get JDBC Connection; nested exception is java.sql.SQLException: ${jdbc.driver}
在一个SSM分布式项目中一个服务报错: ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnec ...