当前位置:网站首页>[shutter] animation animation (basic process of shutter animation | create animation controller | create animation | set value listener | set state listener | use animation values in layout | animatio
[shutter] animation animation (basic process of shutter animation | create animation controller | create animation | set value listener | set state listener | use animation values in layout | animatio
2022-07-03 01:33:00 【Programmer community】
List of articles
- One 、 Create animation controllers
- Two 、 Create animation
- 3、 ... and 、 Set value listener
- Four 、 Set up state listeners
- 5、 ... and 、 Use animation values in layout
- 6、 ... and 、 Animation runs
- 7、 ... and 、 Complete code example
- 8、 ... and 、 Related resources
Flutter Basic animation process :
① Create animation controllers
② Create animation
③ Set value listener
④ Set up state listeners
⑤ Use animation values in layout
⑥ Animation runs
One 、 Create animation controllers
AnimationController Constructor parameter description :
AnimationController( {
double? value, /// The initial value of the animation Duration? duration, /// Animation forward playback duration Duration? reverseDuration, /// Animation playback duration in reverse order String? debugLabel, /// Flags that identify animation during debugging double lowerBound: 0.0, /// Animation minimum double upperBound: 1.0, /// Animation max AnimationBehavior animationBehavior: AnimationBehavior.normal, /// Contextual TickerProvider , Used to prevent off screen animation from consuming unnecessary resources , /// Generally will StatefulWidget As vsync value required TickerProvider vsync} )
Among the above parameters , Just set up required TickerProvider vsync
Parameters And Duration? duration
Parameters can be ;
Create an animation controller code example :
/// 1. Initialize the animation controller animationController = AnimationController( // When the animation is drawn outside the screen , Reduce consumption vsync: this, // Animation duration 2 second duration: Duration(seconds: 3), );
Two 、 Create animation
Here to create Tween Patch animation , Set initial values for animation
0
0
0 , End value
300
300
300 , The animation is executing
3
3
3 In seconds ( Animation duration specified in the animation controller ) , Automatically calculate the
0
0
0 ~
300
300
300 Animation values between ;
Create animation code examples :
/// 2 . structure Tween Patch animation , /// Set up animation controller AnimationController Animate the room /// The animation value is the width and height of the square component animation = Tween<double>( begin: 0, end: 300 ).animate(animationController)
3、 ... and 、 Set value listener
call Animation Of addListener Method , You can add value listeners to animations ;
Concise usage : The previous line of code expression must be animation, There can't be a semicolon at the end , Can be used later ..addListener
usage , Equivalent to this usage animation.addListener
;
setState Method : If the animation works , Must be called in the listener setState Method , In order to recall build Method to render layout , otherwise UI The interface doesn't refresh ;
" Set value listener " Code example :
/// 3 . Add an animation value listener /// This usage is similar to animation.addListener The effect is equivalent /// This kind of writing is simple /// Similar to chain call , The previous line of code expression must be animation, There can't be a semicolon at the end /// Particular attention : If the animation works , Must be called in the listener setState Method ..addListener(() {
/// call setState After the method , After updating the relevant status values , Automatically call build Method to refactor the component interface setState(() {
// Get the value during the animation execution animationValue = animation.value; }); })
Four 、 Set up state listeners
call Animation Of addStatusListener Method , You can add value listeners to animations ;
Concise usage : The previous line of code expression must be animation, There can't be a semicolon at the end , Can be used later ..addStatusListener
usage , Equivalent to this usage animation.addStatusListener
;
setState Method : If the animation works , Must be called in the listener setState Method , In order to recall build Method to render layout , otherwise UI The interface doesn't refresh ;
" Set up state listeners " Code example :
/// 4 . Add animation status listener /// Set the animation status listener ..addStatusListener((status) {
/// call setState After the method , After updating the relevant status values , Automatically call build Method to refactor the component interface setState(() {
/// Get animation status animationStatus = status; }); });
5、 ... and 、 Use animation values in layout
stay build Method , Use the animation value obtained from the above listener animationValue , The value is
0
0
0 ~
300
300
300 The floating point number between ;
Here, the animation value is used as the width and height of the square component ;
" Use animation values in layout " Code example :
// The main component of the animation // 6 . Use animated values in layout components , In order to achieve the animation effect Container( /// Set the distance from the top 20 Pixels margin: EdgeInsets.only(top: 50), height: animationValue, width: animationValue, decoration: BoxDecoration(color: Colors.red), ),
6、 ... and 、 Animation runs
monitor GestureDetector Of onTap Click event , After clicking this component , call animationController.forward()
Method , Run the animation ;
Code example :
GestureDetector( // 5 . Click the button to start the animation onTap: (){
/// Button click event /// First initialize the animation animationController.reset(); /// Executing animation forward , That is, from the initial value to the end value animationController.forward(); }, child: Container( alignment: Alignment.center, color: Colors.green, height: 50, child: Text( // Show text " Animation start ", /// Text direction : From left to right textDirection: TextDirection.ltr, ), ), ),
7、 ... and 、 Complete code example
Complete code example :
import 'package:flutter/material.dart';void main() {
runApp(AnimationApp());}/// Animation example main interface component /// This component is stateful , So we need to define StatefulWidget Components class AnimationApp extends StatefulWidget{
@override _AnimationAppState createState() => _AnimationAppState();}/// by StatefulWidget Component creation State class /// Every StatefulWidget Need a matching State class class _AnimationAppState extends State<AnimationApp> with SingleTickerProviderStateMixin{
/// Animation class Animation<double> animation; /// Animation controller AnimationController animationController; /// Animation state AnimationStatus animationStatus; /// Animation values /// While the animation is running , The value calculated by animation double animationValue; @override void initState() {
super.initState(); /// 1. Initialize the animation controller animationController = AnimationController( // When the animation is drawn outside the screen , Reduce consumption vsync: this, // Animation duration 2 second duration: Duration(seconds: 3), ); /// 2 . structure Tween Patch animation , /// Set up animation controller AnimationController Animate the room /// The animation value is the width and height of the square component animation = Tween<double>( begin: 0, end: 300 ).animate(animationController) /// 3 . Add an animation value listener /// This usage is similar to animation.addListener The effect is equivalent /// This kind of writing is simple /// Similar to chain call , The previous line of code expression must be animation, There can't be a semicolon at the end /// Particular attention : If the animation works , Must be called in the listener setState Method ..addListener(() {
/// call setState After the method , After updating the relevant status values , Automatically call build Method to refactor the component interface setState(() {
// Get the value during the animation execution animationValue = animation.value; }); }) /// 4 . Add animation status listener /// Set the animation status listener ..addStatusListener((status) {
/// call setState After the method , After updating the relevant status values , Automatically call build Method to refactor the component interface setState(() {
/// Get animation status animationStatus = status; }); }); } /// The method and initState Corresponding @override void dispose() {
/// Release the animation controller animationController.dispose(); super.dispose(); } @override Widget build(BuildContext context) {
return Container( /// Set the distance from the top 20 Pixels margin: EdgeInsets.only(top: 100), child: Column( children: [ GestureDetector( // 5 . Click the button to start the animation onTap: (){
/// Button click event /// First initialize the animation animationController.reset(); /// Executing animation forward , That is, from the initial value to the end value animationController.forward(); }, child: Container( alignment: Alignment.center, color: Colors.green, height: 50, child: Text( // Show text " Animation start ", /// Text direction : From left to right textDirection: TextDirection.ltr, ), ), ), Text(" Animation state : $animationStatus", textDirection: TextDirection.ltr,), Text(" Animation values : ${animationValue?.round()}", textDirection: TextDirection.ltr,), // The main component of the animation // 6 . Use animated values in layout components , In order to achieve the animation effect Container( /// Set the distance from the top 20 Pixels margin: EdgeInsets.only(top: 50), height: animationValue, width: animationValue, decoration: BoxDecoration(color: Colors.red), ), ], ), ); }}
Running effect :
8、 ... and 、 Related resources
Reference material :
- Flutter Official website : https://flutter.dev/
- Flutter Plug in download address : https://pub.dev/packages
- Flutter Developing documents : https://flutter.cn/docs ( Strongly recommend )
- official GitHub Address : https://github.com/flutter
- Flutter The Chinese community : https://flutter.cn/
- Flutter Practical tutorial : https://flutter.cn/docs/cookbook
- Flutter CodeLab : https://codelabs.flutter-io.cn/
- Dart Chinese document : https://dart.cn/
- Dart Developer website : https://api.dart.dev/
- Flutter Chinese net : https://flutterchina.club/ , http://flutter.axuer.com/docs/
- Flutter Related issues : https://flutterchina.club/faq/ ( It is recommended to watch it at the introductory stage )
- GitHub Upper Flutter Open source examples : https://download.csdn.net/download/han1202012/15989510
- Flutter Practical e-books : https://book.flutterchina.club/chapter1/
Important topics :
- Flutter Animation reference documentation : https://flutterchina.club/animations/
Blog source download :
GitHub Address : https://github.com/han1202012/flutter_animation ( Keep updating with the progress of the blog , There may not be the source code of this blog )
Blog source snapshot : https://download.csdn.net/download/han1202012/16184811 ( The source code snapshot of this blog , You can find the source code of this blog )
边栏推荐
- What is tone. Diao's story
- The difference between tail -f, tail -f and tail
- How is the mask effect achieved in the LPL ban/pick selection stage?
- What are the trading forms of spot gold and what are the profitable advantages?
- Basis of information entropy
- 數學知識:臺階-Nim遊戲—博弈論
- Qtablewidget lazy load remaining memory, no card!
- Main features of transport layer TCP and TCP connection
- C language course information management system
- kivy教程之在 Kivy App 中使用 matplotlib 的示例
猜你喜欢
MySQL foundation 04 MySQL architecture
SSL flood attack of DDoS attack
Androd Gradle 对其使用模块依赖的替换
Scheme and practice of cold and hot separation of massive data
leetcode刷题_两数之和 II - 输入有序数组
软考信息系统项目管理师_历年真题_2019下半年错题集_上午综合知识题---软考高级之信息系统项目管理师053
After reading this article, I will teach you to play with the penetration test target vulnhub - drivetingblues-9
JDBC courses
C#应用程序界面开发基础——窗体控制(1)——Form窗体
Daily topic: movement of haystack
随机推荐
一位苦逼程序员的找工作经历
Now that the teenager has returned, the world's fireworks are the most soothing and ordinary people return to work~
Tp6 fast installation uses mongodb to add, delete, modify and check
2022 coal mine gas drainage examination question bank and coal mine gas drainage examination questions and analysis
[self management] time, energy and habit management
一比特苦逼程序員的找工作經曆
MySQL - database query - basic query
Appuyez sur l'apprentissage de l'esprit de frappe - reconnaissance des coordonnées de fond multithreadées
[FPGA tutorial case 6] design and implementation of dual port RAM based on vivado core
wirehark数据分析与取证A.pacapng
MySQL foundation 04 MySQL architecture
LDC Build Shared Library
Is there anything in common between spot gold and spot silver
Key wizard hit strange learning - automatic path finding back to hit strange points
MySQL foundation 06 DDL
音程的知识的总结
Key wizard play strange learning - front desk and Intranet send background verification code
Mathematical knowledge: Nim game game theory
leetcode 6103 — 从树中删除边的最小分数
看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-9