Flutter常用的几种动画

Flutter 的动画系统可以帮助开发者创建流畅、生动的用户界面。下面是一些关于 Flutter 动画的详细介绍和示例代码。

动画类别

Flutter 中有多种类型的动画,包括:

  • 显式动画:通过使用 AnimationAnimationController 类手动控制动画。
  • 隐式动画:通过更改部件属性自动触发的预定义动画,例如 AnimatedContainerAnimatedOpacity 等等。
  • 物理动画:基于物理规律的动画,例如 SpringSimulationFrictionSimulation 等等。

显式动画

显式动画需要开发者手动控制其开始、停止、反转等行为。以下是一个简单的示例,演示如何使用 AnimationControllerTween 类来创建一个在屏幕顶部移动的绿色盒子:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Explicit Animation Demo',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<EdgeInsets> _animation;

  
  void initState() {
    super.initState();

    _controller = AnimationController(
      vsync: this,
      duration: Duration(seconds: 2),
    );

    _animation = EdgeInsetsTween(
      begin: EdgeInsets.only(top: 0.0),
      end: EdgeInsets.only(top: 200.0),
    ).animate(_controller);

    _controller.forward();
  }

  
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Explicit Animation Demo'),
      ),
      body: Center(
        child: AnimatedBuilder(
          animation: _animation,
          builder: (BuildContext context, Widget? child) {
            return Container(
              width: 100.0,
              height: 100.0,
              margin: _animation.value,
              color: Colors.green,
            );
          },
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个 AnimationController 对象和一个 EdgeInsetsTween 对象,并通过 animate() 方法将它们组合在一起。在 initState() 方法中,我们调用 _controller.forward() 来启动动画。在 build() 方法中,我们使用 AnimatedBuilder 将动画应用于 Containermargin 属性上。

隐式动画

隐式动画是指通过更改部件属性自动触发的预定义动画。以下是一个简单的示例,演示如何使用 AnimatedContainer 类来创建一个颜色、大小和边框半径都会自动变化的盒子:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Implicit Animation Demo',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Color _color = Colors.green;
  double _size = 100.0;
  double _borderRadius = 16.0;

  void _changeProperties() {
    setState(() {
      _color = Colors.red;
      _size = 200.0;
      _borderRadius = 64.0;
    });
  }

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Implicit Animation Demo'),
      ),
      body: Center(
        child: GestureDetector(
          onTap: _changeProperties,
          child: AnimatedContainer(
            duration: Duration(seconds: 1),
            curve: Curves.easeInOut,
            width: _size,
            height: _size,
            decoration: BoxDecoration(
              color: _color,
              borderRadius: BorderRadius.circular(_borderRadius),
            ),
          ),
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个颜色、大小和边框半径都会自动变化的盒子,当用户点击时,将调用 _changeProperties() 方法,使用 setState() 更新 _color_size_borderRadius 变量。我们将 AnimatedContainer 包装在一个 GestureDetector 中,以便当用户点击时触发 _changeProperties() 方法。

注意,在 AnimatedContainer 中使用 durationcurve 属性可以设置动画的持续时间和缓和曲线。在这个示例中,我们使用了 1 秒钟的持续时间和默认的缓和曲线。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT编程学习栈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值