Flutter Animation AnimatedBuilder
Flutter AnimatedBuilder
创建动画的widget
Key key,
@required Listenable animation,
@required this.builder,
this.child,
animation:Animationcontroller //动画
child 动画作用的view
builder:每次controller值改变都会回到builder 重新生成view
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | 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(); } }, ); } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步