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. udf也能用Python

    具体步骤见<fluent加载第三方(C++,Fortran等)动态链接库> 我们对导入的动态链接库进行改动 打开VS2013 完成了上述过程以后,还需要配置Python 首先需要安装Pyt ...

  2. Apache Flink - 架构和拓扑

    Flink结构: flink cli 解析本地环境配置,启动 ApplicationMaster 在 ApplicationMaster 中启动 JobManager 在 ApplicationMas ...

  3. var a = function(){}和var a = function(){}();的区别

    var a = function(){ ... ... ... } 声明方法. var a = function(){ ... ... ... }(); 声明方法并执行 demo: var u = f ...

  4. 刷题记录:[SUCTF 2019]CheckIn

    目录 刷题记录:[SUCTF 2019]CheckIn 一.涉及知识点 1.利用.user.ini上传\隐藏后门 2.绕过exif_imagetype()的奇技淫巧 二.解题方法 刷题记录:[SUCT ...

  5. 动态BGP与静态BGP

    在阿里云上选择ECS的时候,发现有动态BGP和静态BGP区分,静态的要便宜些,搜了下区别如下: 静态BGP路由是指由网络运营商手动配置的路由信息.当网络的拓扑结构或链路的状态发生变化时,运营商需要手动 ...

  6. 使用yarn代替npm作为node.js的模块管理器

    使用yarn代替npm作为node.js的模块管理器 转 https://www.jianshu.com/p/bfe96f89da0e     Fast, reliable, and secure d ...

  7. Word模板生成PDF文件目录出现“错误!未定义书签!”的解决办法

    通过程序读取Word文档模板生成PDF时,所有目录的页码全部变为“错误!未定义书签!”,后来经过仔细研究,发现是“域”的问题. 解决办法:全选(Crtl+A),按下Crtl+F11,再打印或者另存为P ...

  8. javascript中this、new、apply和call详解

    如果在javascript语言里没有通过new(包括对象字面量定义).call和apply改变函数的this指针,函数的this指针都是指向window的,重要的话要说三遍.... 讲解this指针的 ...

  9. [LeetCode] 412. Fizz Buzz 嘶嘶嗡嗡

    Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...

  10. LeetCode:接雨水【42】

    LeetCode:接雨水[42] 题目描述 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1, ...