不同动画的不同持续时间的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方法来启动动画。

相关内容

热门资讯

玩家攻略推荐!斗棋联盟俱乐部辅... 玩家攻略推荐!斗棋联盟俱乐部辅助挂(透视)hhpoker开挂教程(wpk教程有挂秘籍);无需打开直接...
实测揭晓!樱花之盛辅助器下载(... 实测揭晓!樱花之盛辅助器下载(透视)pokermaster破解版(安装教程有挂方针) >>您好:软件...
推荐一款!奕乐贵州麻将开挂版软... 大家好,今天小编来为大家解答奕乐贵州麻将开挂版软件这个问题咨询软件客服可以免费测试直接加微信(136...
重大通报!苹果手机微信小程序游... 重大通报!苹果手机微信小程序游戏破解器(透视)德扑HHpoker有挂吗(新2026版有挂技巧)《详细...
透视工具!衢州都莱软件辅助器(... 透视工具!衢州都莱软件辅助器(透视)德州透视插件(切实教程有挂方针);无需打开直接搜索加薇13670...
每日必备!蜀山四川破解版ios... 每日必备!蜀山四川破解版ios(透视)wepoker辅助器最新版本更新内容(黑科技教程有挂秘籍)>>...
透视辅助!凑一桌游戏软件下载辅... 透视辅助!凑一桌游戏软件下载辅助(透视)aapoker怎么开辅助器(必赢方法真是有挂);无需打开直接...
透视好友!桂林字牌辅助科技(透... 透视好友!桂林字牌辅助科技(透视)wepoker怎么提高运气(AI教程有人有挂);无需打开直接搜索打...
技术分享!皮皮游戏辅助工具(透... 技术分享!皮皮游戏辅助工具(透视)hhpoker辅助器(wpk教程证实有挂);亲,皮皮游戏辅助工具这...
一起来讨论!创思维激k透视挂(... wepoker软件辅助程序 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大...