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. 使用IDEA查看变量调用链

    在开发中,我们有时需要查看某个变量是怎么来的,从哪个类的某个方法调用后进入另一个类的某个方法. 如果只有一两层的调用,那么还能直接通过方法跳转来观察. 但是,如果有七八层的调用链呢,在各个方法之间跳来 ...

  2. ssm框架中,项目启动过程以及web.xml配置详解

    原文:https://blog.csdn.net/qq_35571554/article/details/82385838 本篇主要在基于SSM的框架,深入讲解web.xml的配置 web.xml   ...

  3. linux crontab 定时使用方法

    1.文件目录 00 07 * * * root cd /home/op/saiyan_game_center && venv/bin/python statistics_data/od ...

  4. spark-submit --files 动态加载外部资源文件

    在做spark时,有些时候需要加载资源文件,需要在driver或者worker端访问.在client模式下可以使用IO流直接读取,但是在cluster模式下却不能直接读取,需要如下代码: val is ...

  5. VBA 宏文件源代码密码解除

    VBA Project密码解除第一种方法详细步骤参考:以下VBA代码是第二种方法 '使用本代码之前需要将需要解除密码保护的含有宏的Excel文件(如果是xlsm文件,需要先另存为97-03版的xls文 ...

  6. Spring cloud微服务安全实战-7-12整合链路追踪和日志监控

    调用链路的监控和统一日志的监控结合起来.比如说我在调用链监控上发现有一个调用订单的服务慢了.通过pinpoint可以看到 .用户发出来的请求,经过了网关,经过了order,经过了pagement.通过 ...

  7. 【WebSocket】WebSocket快速入门

    WebSocket介绍 WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议. WebSocket 使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动 ...

  8. Swift编码总结6

    1.UILabel的minimumScaleFactor: 需要UIlabel根据字数多少来减小字体大小,使得UIlabel能够显示全所有的文字.你需要做的就是设置minimumScaleFactor ...

  9. DB2执行计划分析

    多表连接的三种方式详解 hash join.merge join. nested loop 项目中的SQL执行效率太低,就用执行计划看一下执行SQL,看不懂,百度一下,纪录下来: 大多数人从来没有听说 ...

  10. 面试之哈希表leetcode

    1 案例1 leetcode-----242 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词. 示例 1: 输入: s = "anagram", t ...