不同动画的不同持续时间的Flutter
创始人
2025-01-08 22:32:48
0

在Flutter中,可以使用Tween动画和AnimationController来创建不同持续时间的动画。下面是一个示例代码,演示了如何使用Tween动画和AnimationController来创建不同持续时间的动画。

import 'package:flutter/material.dart';

class CustomAnimationExample extends StatefulWidget {
  @override
  _CustomAnimationExampleState createState() => _CustomAnimationExampleState();
}

class _CustomAnimationExampleState extends State
    with SingleTickerProviderStateMixin {
  AnimationController _controller;
  Animation _animation1;
  Animation _animation2;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: Duration(seconds: 2), // 设置总体动画持续时间为2秒
      vsync: this,
    );

    _animation1 = Tween(begin: 0, end: 1).animate(
      CurvedAnimation(
        parent: _controller,
        curve: Interval(0, 0.5), // 从0到0.5的动画持续时间为1秒
      ),
    );

    _animation2 = Tween(begin: 0, end: 1).animate(
      CurvedAnimation(
        parent: _controller,
        curve: Interval(0.5, 1), // 从0.5到1的动画持续时间为1秒
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Custom Animation Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            AnimatedBuilder(
              animation: _animation1,
              builder: (context, child) {
                return Opacity(
                  opacity: _animation1.value,
                  child: Container(
                    width: 200,
                    height: 200,
                    color: Colors.blue,
                  ),
                );
              },
            ),
            SizedBox(height: 20),
            AnimatedBuilder(
              animation: _animation2,
              builder: (context, child) {
                return Opacity(
                  opacity: _animation2.value,
                  child: Container(
                    width: 200,
                    height: 200,
                    color: Colors.red,
                  ),
                );
              },
            ),
            SizedBox(height: 20),
            RaisedButton(
              child: Text('Start Animation'),
              onPressed: () {
                _controller.reset();
                _controller.forward();
              },
            ),
          ],
        ),
      ),
    );
  }
}

void main() {
  runApp(MaterialApp(
    home: CustomAnimationExample(),
  ));
}

在上面的示例中,我们使用AnimationController来控制整体动画的持续时间为2秒。然后,我们使用Tween动画和CurvedAnimation来创建两个不同持续时间的动画。第一个动画的持续时间为1秒,从0到0.5秒;第二个动画的持续时间也为1秒,从0.5秒到1秒。

在build方法中,我们使用AnimatedBuilder来构建动画的UI。通过监听_animation1和_animation2,我们可以在动画值发生变化时更新UI。在这个示例中,我们使用Opacity小部件来改变容器的透明度,创建一个淡入淡出的动画效果。

最后,在按下按钮时,我们通过调用_controller的reset和forward方法来启动动画。

相关内容

热门资讯

第六分钟辅助挂!微信闲来辅助神... 第六分钟辅助挂!微信闲来辅助神器app(透视)四川熊猫辅助软件(详细透视外开挂教程)是一款可以让一直...
6分钟辅助挂!兴动互娱辅助脚本... 6分钟辅助挂!兴动互娱辅助脚本(透视)人人燕赵辅助(详细透视外开挂教程);兴动互娱辅助脚本是一种具有...
第7分钟辅助挂!川南小闲辅助器... 1、第7分钟辅助挂!川南小闲辅助器(透视)蜀山四川智能辅助插件(详细透视外开挂教程);代表性(透视辅...
七分钟辅助挂!皮皮游戏挂机辅助... 七分钟辅助挂!皮皮游戏挂机辅助(透视)情怀蒲仙辅助(详细透视外开挂教程)1)皮皮游戏挂机辅助辅助挂:...
第一分钟辅助挂!佛手在线大菠萝... 第一分钟辅助挂!佛手在线大菠萝可以作弊码(透视)胡乐辅助脚本是真的假的(详细透视外开挂教程)1、在佛...
9分钟辅助挂!天天辅助工具(透... 9分钟辅助挂!天天辅助工具(透视)微乐家乡麻辣自建房辅助app(详细透视外开挂教程);致您一封信;亲...
5分钟辅助挂!微乐小程序辅助插... 1、5分钟辅助挂!微乐小程序辅助插件(透视)开心门一番有挂吗(详细透视外开挂教程)。2、微乐小程序辅...
3分钟辅助挂!永久免费脚本辅助... 3分钟辅助挂!永久免费脚本辅助工具(透视)同城游辅助软件(详细透视外开挂教程);永久免费脚本辅助工具...
第一分钟辅助挂!天天爱柳州有没... 1、第一分钟辅助挂!天天爱柳州有没有辅助器(透视)中至赣州黑科技辅助软件视频(详细透视外开挂教程)2...
第五分钟辅助挂!哈糖大菠萝怎么... 第五分钟辅助挂!哈糖大菠萝怎么让系统发好牌(透视)哈灵脚本辅助(详细透视外开挂教程)1、许多玩家不知...