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综合大作业
作业要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3339 一.Hadoop综合大作业 要求: 1.将爬虫大作业产生的csv ...
- JAVA:使用栈实现一个队列
使用栈实现一个队列,需要弄清楚栈和队列的区别: 栈:先进后出: 队列:先进先出. 实现思路: 1)通过两个栈(pushStack / popStack)对倒,确保 popStack 栈的出栈顺序与队列 ...
- ES6将两个数组合并成一个对象数组
需求 有这么两个数组 let metrodates = [ "2008-01", "2008-02", "2008-03",..ect ]; ...
- phpexcel中文手册(转)
首先到phpexcel官网上下载最新的phpexcel类,下周解压缩一个classes文件夹,里面包含了PHPExcel.php和PHPExcel的文件夹,这个类文件和文件夹是我们需要的,把class ...
- HttpUtils(2)
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; impo ...
- 【Tools】Myeclise-2018.12.0 最新破解文件
Myeclise-2018.12.0 最新破解文件. 最近在写android app登录块,需要用到这个工具,顺手就拿到了,发现资源太少.这里分享给大家. 有币高富帅打赏下载地址: https://d ...
- 【视频开发】OpenCV中Mat,图像二维指针和CxImage类的转换
在做图像处理中,常用的函数接口有OpenCV中的Mat图像类,有时候需要直接用二维指针开辟内存直接存储图像数据,有时候需要用到CxImage类存储图像.本文主要是总结下这三类存储方式之间的图像数据的转 ...
- 【电子电路技术】PoE供电技术的优缺点
转自http://www.mamicode.com/info-detail-1059108.html 1PoE供电稳定吗? 随着近几年网络监控的迅猛发展,技术门槛也是越来越高,厂商提供的技术支持也越来 ...
- CentOS 初始化脚本
#!/bin/bash # curl https://yiyingcanfeng.github.io/centos-init.sh | bash # 可选参数base kernel python ph ...
- 谈谈php里的IOC控制反转,DI依赖注入(转)
转自:http://www.cnblogs.com/qq120848369/p/6129483.html 发现问题 在深入细节之前,需要确保我们理解"IOC控制反转"和" ...