Flutter 补间动画

现在我需要点击文本后将文本旋转方向

现在我有两种方法实现此动画:效果就不看了,喜欢看的可以自己复制代码去查看

第一种,通过使用旋转动画来完成旋转效果:

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

  @override
  State<MyApp1> createState() => _MyApp1State();
}

class _MyApp1State extends State<MyApp1> {
  bool bool1 = true;
  var xz = 0;

  @override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;
    return Container(
      width: size.width,
      height: size.height,
      child: Stack(
        children: [
          AnimatedPositioned(
              //此动画必须在Stack中有效
              child: Container(
                height: size.height,
                width: size.width,
                child: Container(
                  color: Colors.blue.withOpacity(.2),
                ),
              ),
              duration: Duration(milliseconds: 300)),
          AnimatedPositioned(
            //也是一个动画组件具体想要了解可以去看之前的文章
            top: 100,
            left: 200,
            duration: Duration(milliseconds: 300),
            child: AnimatedRotation(
              //旋转动画
              turns: xz * pi / 180, //旋转位置
              duration: Duration(milliseconds: 300), //动画时间
              child: GestureDetector(
                onTap: () {
                  //设置点击时间
                  setState(() {
                    bool1 = !bool1; //点击后bool1取反
                    xz = bool1 ? 0 : -14; //当bool1的时候xz=0否则等于-14,这里也可以等于14,
                    // 区别是一个顺时针,一个逆时针旋转
                    print(xz);
                  });
                },
                child: Text(
                  //按钮
                  "旋转动画",
                  style: TextStyle(
                      color: Colors.white,
                      fontWeight: FontWeight.bold,
                      fontSize: 22),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}

第二种通过补间动画来完成

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

  @override
  State<MyApp1> createState() => _MyApp1State();
}

bool bool1 = true;

class _MyApp1State extends State<MyApp1> with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  late Animation<double> animation =
      Tween<double>(begin: 0, end: 90).animate(_controller);

  void abc() {
    setState(() {
      bool1 = !bool1;
    });
    bool1 ? _controller.forward() : _controller.reverse();
  }

  @override
  void initState() {
    super.initState();

    _controller =
        AnimationController(vsync: this, duration: Duration(milliseconds: 300));
  }

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

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
        animation: animation,
        builder: (context, _) {
          var size = MediaQuery.of(context).size;
          return Stack(
            children: [
              AnimatedPositioned(
                  width: size.width,
                  height: size.height,
                  child: Container(
                    color: Colors.green.withOpacity(.7),
                  ),
                  duration: Duration(milliseconds: 300)),
              AnimatedPositioned(
                  child: Transform.rotate(
                      angle: (animation.value) * pi / 180,
                      child: GestureDetector(
                        onTap: () {
                          abc();
                        },
                        child: Container(
                          width: 200,
                          height: 200,
                          child: Text(
                            "补间动画",
                            style: TextStyle(
                                color: Colors.white,
                                fontWeight: FontWeight.bold,
                                fontSize: 44),
                          ),
                        ),
                      )),
                  duration: Duration(milliseconds: 300))
            ],
          );
        });
  }
}

补间动画不仅可以使用旋转动画,只要涉及到动画基本都可以使用

补间动画,但是会稍微麻烦一些。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flutter 加载中动画是指在数据加载或处理过程中为用户提供的视觉反馈。在 Flutter 中,我们可以使用多种方法来实现加载中动画。 一种常见的方式是使用 Flutter 自带的 CircularProgressIndicator 组件。这是一个圆形进度指示器,可以显示加载进度的百分比。我们可以通过设置 value 属性来控制进度的大小,通常可以将它与 FutureBuilder 或 StreamBuilder 结合使用来显示网络请求或异步操作的进度。此外,我们还可以通过设置颜色和线条粗细等属性来自定义 CircularProgressIndicator 的外观。 除了 CircularProgressIndicator,Flutter 还提供了许多其他的加载中动画,比如 LinearProgressIndicator、RefreshIndicator 等。LinearProgressIndicator 是一个线性进度指示器,适用于表示长时间操作的进度。RefreshIndicator 则是下拉刷新的效果,用户在列表或页面顶部下拉时,会出现一个圈圈的加载动画,表示正在刷新数据。 除了使用预置的加载中动画组件,我们还可以通过自定义动画来实现更丰富的加载中效果。Flutter 提供了强大的动画库和组件,如 AnimatedContainer、AnimationController 等,使得创建自定义加载中动画变得更加方便。 总之,Flutter 提供了各种加载中动画的选择,无论是使用预置组件还是自定义动画,都可以根据项目需求和个人喜好来实现加载中效果。这些加载中动画可以提升用户体验,让用户在等待数据加载时感到更舒适和愉悦。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值