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

相关内容

热门资讯

四分钟操作!wepoker有没... 四分钟操作!wepoker有没有挂(透视)原来真的有辅助开挂(哔哩哔哩)1、wepoker有没有挂免...
第五分钟指南!hhpkoer辅... 第五分钟指南!hhpkoer辅助器视频(透视)原来真的是有辅助插件(哔哩哔哩)1、hhpkoer辅助...
第2分钟办法!wepoker模... 第2分钟办法!wepoker模拟器哪个(透视)好像真的有辅助脚本(哔哩哔哩)1、下载好wepoker...
第3分钟绝活!sohoo po... 第3分钟绝活!sohoo poker辅助器(透视)一直是有辅助插件(哔哩哔哩)1、在sohoo po...
三分钟机巧!德州机器人代打脚本... 三分钟机巧!德州机器人代打脚本(透视)其实真的有辅助透视(哔哩哔哩)1、全新机制【德州机器人代打脚本...
三分钟积累!wepoker数据... 三分钟积累!wepoker数据分析(透视)果然真的是有辅助神器(哔哩哔哩)1、wepoker数据分析...
一分钟方案!we-poker辅... 一分钟方案!we-poker辅助器(透视)好像是真的有辅助教程(哔哩哔哩)1、we-poker辅助器...
8分钟诀窍!wepoker辅助... 您好,wepoker辅助器有哪些功能这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275...
四分钟法子!wpk透视是真的假... 四分钟法子!wpk透视是真的假的(透视)都是是有辅助神器(哔哩哔哩)wpk透视是真的假的辅助器是一种...
第七分钟总结!hhpoker有... 第七分钟总结!hhpoker有后台操作吗(透视)果然是有辅助教程(哔哩哔哩)1、该软件可以轻松地帮助...