Flutter:AnimatedIcon图标动画,自定义Icon通过延时Interval,实现交错式动画

html 复制代码
配置vsync,需要实现一下with SingleTickerProviderStateMixin
js 复制代码
class _MyHomePageState extends State<MyHomePage>  with SingleTickerProviderStateMixin{
  // late延迟初始化  AnimationController
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();
    // 初始化 AnimationController
    _controller = AnimationController(
      duration: const Duration(milliseconds: 500),
      vsync:this, // 让程序和手机的刷新频率统一
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('标题'),
      ),
      body: Center(
        child: Column(
          children: [
            AnimatedIcon(icon: AnimatedIcons.menu_close, progress: _controller,size: 40,color: Colors.red,),
            ElevatedButton(onPressed: (){
              _controller.forward();
            }, child: const Text('播放')),
            ElevatedButton(onPressed: (){
              _controller.reverse();
            }, child: const Text('返回')),
          ],
        ),
      ),
    );
  }
}

AnimatedIcons参数选项包含:

复制代码
add_event
arrow_menu
close_menu
ellipsis_search
event_add
home_menu
list_view
menu_arrow
menu_close
menu_home
pause_play
play_pause
search_ellipsis
view_list

上边没有想要的图标,那么就需要自己实现2个图标间的交错式动画

js 复制代码
class _MyHomePageState extends State<MyHomePage>  with SingleTickerProviderStateMixin{
  // 定义 AnimationController
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();
    // 初始化 AnimationController
    _controller = AnimationController(
      duration: const Duration(milliseconds: 1000),
      vsync:this, // 让程序和手机的刷新频率统一
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('标题'),
      ),
      body: Column(
        children: [
          // 定义Stack,使2个图标重叠摆放
          Stack(
            children: [
              // 默认显示搜索图标,
              // Tween(开始1,结束0)
              // Interval时间为0-0.5之间
              //
              // 搜索图标开始为1.0显示,结束时0.0隐藏,时间从0.0开始,到0.5结束
              // 关闭图标开始为0.0隐藏,结束时1.0显示,时间从0.5开始,到1.0结束
              ScaleTransition(
                scale: _controller.drive(Tween(begin: 1.0,end: 0.0).chain(CurveTween(curve: const Interval(0.0, 0.5)))),
                child: Icon(Icons.search,size: 40,),
              ),
              ScaleTransition(
                scale: _controller.drive(Tween(begin: 0.0,end: 1.0).chain(CurveTween(curve: Interval(0.5, 1)))),
                child: const Icon(Icons.close,size: 40,),
              ),
            ],
          ),
          ElevatedButton(onPressed: (){
            _controller.forward();
          }, child: const Text('播放')),
          ElevatedButton(onPressed: (){
            _controller.reverse();
          }, child: const Text('返回')),
        ],
      ),
    );
  }
}
相关推荐
西西学代码3 小时前
Flutter---Listview横向滚动列表(2)
linux·运维·flutter
未来猫咪花4 小时前
🔥 神奇的 Dart Zone 机制
flutter
AskHarries5 小时前
RevenueCat 接入 Apple App Store 订阅全流程详解(2025 最新)
flutter·ios·app
白茶三许6 小时前
关于Flutter版本过低导致鸿蒙虚拟机启动失败的问题解决
flutter·开源·harmonyos·openharmony
消失的旧时光-194319 小时前
Flutter 与 React/Vue 为什么思想一致?——声明式 UI 体系的深度对比(超清晰版)
vue.js·flutter·react.js
rainboy1 天前
Flutter :自己动手,封装一个小巧精致的气泡弹窗库
前端·flutter·github
旧时光_1 天前
第4章:布局类组件 —— 4.5 流式布局(Wrap、Flow)
flutter
程序员老刘1 天前
Flutter 3.38 版本更新:客户端开发者需要关注这三点?
flutter·客户端
AskHarries1 天前
RevenueCat 接入 Google Play 订阅全流程详解(2025 最新)
android·flutter·google
不凡的凡1 天前
flutter 管理工具fvm
flutter·harmonyos