微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用颤动动画将图标旋转 180 度

如何解决使用颤动动画将图标旋转 180 度

我有一个IconButton,通常它的图标是Icons.expand_more,但是当我按下它的图标应该是Icons.expand_less。我想对此进行动画处理,以便如果我按下该按钮,它会旋转并从 upwords 指向 downwords。同样,当我按下 expand_less 时,它应该变成带有旋转动画的 expand_more。我怎样才能做到这一点? 下面是我的代码

    IconButton(
      icon:  _expanded ? Icon(Icons.expand_less) : Icon(Icons.expand_more),onpressed: () {
        setState(() {
           _expanded = !_expanded;
        });
      },)

我尝试使用animatedContainer,但它不起作用,因为我使用了两个不同的图标,而且我无法用这个来实现旋转效果。 我也尝试使用以下方法旋转图标,但是当它从 0 度旋转到 180 度时无法显示动画。

IconButton(
              icon: AnimatedContainer(
                  duration: Duration(seconds: 3),child: Transform.rotate(
                      angle: _expanded ? 0 : 180 * math.pi / 180,child: Icon(Icons.expand_less))),// icon:
              //     _expanded ? Icon(Icons.expand_less) : Icon(Icons.expand_more),onpressed: () {
                setState(() {
                  _expanded = !_expanded;
                });
              },)

这是扩展前:

before expansion

这是扩展后的:

after expansion

我想要按钮点击时的旋转动画。

解决方法

试试这个: 添加动画控制器:AnimationController _animationController:

initState() {
    _animationController = new AnimationController(
        vsync: this,duration: Duration(milliseconds: 300));
    }

用这个包裹你的图标:

RotationTransition(
         turns: Tween(begin: 0.0,end: 1.0)
                .animate(_animationController),child: IconButton(icon: //YOUR ICON),onPressed: () {
             setState(() {
        _animationController1.forward(from: 0.0);
      });
     },),)

最后:

  @override
  void dispose() {
    super.dispose();
    _animationController?.dispose();
  }
,

achieved Animation

感谢@krumpli。

  1. 定义 AnimationController _controller

  2. init 方法定义为:

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

    _controller = AnimationController(
      vsync: this,duration: Duration(milliseconds: 300),upperBound: 0.5,);
  }
  1. dispose 方法定义为:
@override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }
  1. 使用小部件 RotationTransition
RotationTransition(
              turns: Tween(begin: 0.0,end: 1.0).animate(_controller),child: IconButton(
                icon: Icon(Icons.expand_less),onPressed: () {
                  setState(() {
                    if (_expanded) {
                      _controller..reverse(from: 0.5);
                    } else {
                      _controller..forward(from: 0.0);
                    }
                    _expanded = !_expanded;
                  });
                },)

不要忘记在类定义中添加 with SingleTickerProviderStateMixin

,

看看这个 test sample 我为你所需要的东西。

这也应用了一条曲线,这是一个很好的 Flutter 建议。

import 'package:flutter/material.dart';

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

  @override
  _RotateIconState createState() => _RotateIconState();
}

class _RotateIconState extends State<RotateIcon>
    with SingleTickerProviderStateMixin {
  late final AnimationController _controller;
  late final Animation<double> _animation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
        duration: const Duration(milliseconds: 500),vsync: this);
    _animation =
        Tween(begin: 0.0,end: .5).animate(CurvedAnimation(
        parent: _controller,curve: Curves.easeOut));
    }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
          child: Column(
            children: [
              RotationTransition(
                turns: _animation,child: const Icon(Icons.expand_more),ElevatedButton(
                  onPressed: () {
                    if (_controller.isDismissed) {
                      _controller.forward();
                    } else {
                      _controller.reverse();
                    }
                  },child: const Text("Animate"))
            ],)),);
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。