網站首頁 編程語言 正文
本文實例為大家分享了Flutter Animation實現縮放和滑動動畫的具體代碼,供大家參考,具體內容如下
Animation對象是Flutter動畫庫中的一個核心類,它生成指導動畫的值。
Animation對象知道動畫的當前狀態(例如,它是開始、停止還是向前或向后移動),但它不知道屏幕上顯示的內容。
AnimationController管理Animation。
CurvedAnimation 將過程抽象為一個非線性曲線.
Tween在正在執行動畫的對象所使用的數據范圍之間生成值。例如,Tween可能會生成從紅到藍之間的色值,或者從0到255。
使用Listeners和StatusListeners監聽動畫狀態改變。
import 'package:flutter/animation.dart'; import 'package:flutter/material.dart'; void main() { ? runApp(new LogoApp()); } class LogoApp extends StatefulWidget { ? _LogoAppState createState() => new _LogoAppState(); } class _LogoAppState extends Statewith SingleTickerProviderStateMixin { ? AnimationController controller; ? Animation animation; ? initState() { ? ? super.initState(); ? ? controller = new AnimationController( ? ? ? ? duration: const Duration(milliseconds: 10000), vsync: this); ? ? animation = new Tween(begin: 0.0, end: 300.0).animate(controller); ? ? controller.forward(); ? } ?? ? Widget build(BuildContext context) { ? ? return new AnimatedLogo(animation: animation); ? } ? dispose() { ? ? controller.dispose(); ? ? super.dispose(); ? } } class AnimatedLogo extends AnimatedWidget { ? AnimatedLogo({Key key, Animation animation}) ? ? ? : super(key: key, listenable: animation); ? Widget build(BuildContext context) { ? ? final Animation animation = listenable; ? ? return new Center( ? ? ? child: new Container( ? ? ? ? margin: new EdgeInsets.symmetric(vertical: 10.0), ? ? ? ? height: animation.value, ? ? ? ? width: animation.value, ? ? ? ? child: new FlutterLogo(), ? ? ? ), ? ? ); ? } }
縮放功能
class ScaleAnimatedContent extends StatefulWidget { ? final Widget child; ? final bool show; ? const ScaleAnimatedContent({Key key, this.child, this.show = false}) ? ? ? : super(key: key); ? @override ? ScaleAnimatedContentState createState() => ScaleAnimatedContentState(); } class ScaleAnimatedContentState extends State? ? with TickerProviderStateMixin { ? AnimationController animationController; ? Animation animation; ? @override ? void initState() { ? ? animationController = new AnimationController( ? ? ? vsync: this, ? ? ? duration: new Duration(milliseconds: 600), ? ? ); ? ? // animationSlideUp = new Tween(begin: 0.0,end: 1.0).animate(animationController); ? ? animation = Tween(begin: 0.0, end: 1.0).animate(animationController); ? ? if (widget.show) { ? ? ? animationController.forward(); ? ? } ? ? super.initState(); ? } ? @override ? void didUpdateWidget(ScaleAnimatedContent oldWidget) { ? ? if (widget != oldWidget) { ? ? ? if (widget.show && !oldWidget.show) { ? ? ? ? animationController.forward(from: 0.0); ? ? ? } else if (!widget.show) { ? ? ? ? animationController.reverse(); ? ? ? } ? ? } ? ? super.didUpdateWidget(oldWidget); ? } ? @override ? Widget build(BuildContext context) { ? ? return ScaleTransition( ? ? ? scale: animation, ? ? ? child: widget.child, ? ? ); ? } ? @override ? void dispose() { ? ? animationController.dispose(); ? ? super.dispose(); ? } }
滑動效果
class SlideAnimatedContent extends StatefulWidget { ? final Widget child; ? final bool show; ? const SlideAnimatedContent({Key key, this.child, this.show = false}) ? ? ? : super(key: key); ? @override ? SlideAnimatedContentState createState() => SlideAnimatedContentState(); } class SlideAnimatedContentState extends State? ? with TickerProviderStateMixin { ? AnimationController animationController; ? Animation animationSlideUp; ? @override ? void initState() { ? ? animationController = new AnimationController( ? ? ? vsync: this, ? ? ? duration: new Duration(milliseconds: 600), ? ? ); ? ? animationSlideUp = new Tween( ? ? ? begin: Offset(0.0, 5.0), ? ? ? end: Offset(0.0, 0.0), ? ? ).animate(CurvedAnimation(parent: animationController, curve: Curves.ease)); ? ? if (widget.show) { ? ? ? animationController.forward(); ? ? } ? ? super.initState(); ? } ? @override ? void didUpdateWidget(SlideAnimatedContent oldWidget) { ? ? if (widget != oldWidget) { ? ? ? if (widget.show && !oldWidget.show) { ? ? ? ? animationController.forward(from: 0.0); ? ? ? } else if (!widget.show) { ? ? ? ? animationController.reverse(); ? ? ? } ? ? } ? ? super.didUpdateWidget(oldWidget); ? } ? @override ? Widget build(BuildContext context) { ? ? return SlideTransition( ? ? ? position: animationSlideUp, ? ? ? child: FadeTransition( ? ? ? ? opacity: animationController, ? ? ? ? child: widget.child, ? ? ? ), ? ? ); ? } ? @override ? void dispose() { ? ? animationController.dispose(); ? ? super.dispose(); ? } }
原文鏈接:https://blog.csdn.net/m0_38013946/article/details/121764930
相關推薦
- 2022-12-09 ReactQuery系列React?Query?實踐示例詳解_React
- 2022-04-15 python實現AES算法及AES-CFB8加解密源碼_python
- 2022-06-07 victoriaMetrics庫布隆過濾器初始化及使用詳解_Golang
- 2022-06-06 C#實現Excel轉PDF時設置內容適應頁面寬度_C#教程
- 2022-11-22 Golang自旋鎖的相關介紹_Golang
- 2022-12-29 Kotlin使用滾動控件RecyclerView實例教程_Android
- 2022-12-25 Qt開發之QString類的使用教程詳解_C 語言
- 2022-08-22 Pycharm報錯Non-zero?exit?code?(2)的完美解決方案_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支