Flutter 自定义动画图标

Bnd*_*706 15 dart flutter

我希望制作一个可以从列表中添加和删除的项目。

我正在寻找的是具有 + 图标和 - 图标并在 2 之间设置动画以获得干净流畅的外观。

我有以下代码

Container(
            padding: EdgeInsets.fromLTRB(0, 0, 20, 0),
            child: AnimatedIcon(
              icon: AnimatedIcons.home_menu,
              progress: _animationController,
              color: Colors.white,
            ))
Run Code Online (Sandbox Code Playgroud)

void _handleOnPressed() {
setState(() {
  isPlaying = !isPlaying;
  isPlaying
      ? _animationController.forward()
      : _animationController.reverse();
  });
}
Run Code Online (Sandbox Code Playgroud)

这对于颤振中的内置动画图标来说很好。

我希望使用 + 和 - 图标,但外观相同。

有没有办法做到这一点?

Max*_*yba 33

我知道它不如AnimatedIcon那么漂亮,但实际上您只需几行代码就可以使用您选择的任何 2 个图标获得非常相似的过渡:

 IconButton(
      icon: AnimatedSwitcher(
          duration: const Duration(milliseconds: 300),
          transitionBuilder: (child, anim) => RotationTransition(
                turns: child.key == ValueKey('icon1')
                    ? Tween<double>(begin: 1, end: 0.75).animate(anim)
                    : Tween<double>(begin: 0.75, end: 1).animate(anim),
                child: FadeTransition(opacity: anim, child: child),
              ),
          child: _currIndex == 0
              ? Icon(Icons.close, key: const ValueKey('icon1'))
              : Icon(
                  Icons.arrow_back,
                  key: const ValueKey('icon2'),
                )),
      onPressed: () {
        setState(() {
          _currIndex = _currIndex == 0 ? 1 : 0;
        });
      },
    );
Run Code Online (Sandbox Code Playgroud)

结果: 结果


或者您可以使用,ScaleTransition代替FadeTransition, 并获得更相似的动画:

IconButton(
      icon: AnimatedSwitcher(
          duration: const Duration(milliseconds: 350),
          transitionBuilder: (child, anim) => RotationTransition(
                turns: child.key == ValueKey('icon1')
                    ? Tween<double>(begin: 1, end: 0.75).animate(anim)
                    : Tween<double>(begin: 0.75, end: 1).animate(anim),
                child: ScaleTransition(scale: anim, child: child),
              ),
          child: _currIndex == 0
              ? Icon(Icons.close, key: const ValueKey('icon1'))
              : Icon(
                  Icons.arrow_back,
                  key: const ValueKey('icon2'),
                )),
      onPressed: () {
        setState(() {
          _currIndex = _currIndex == 0 ? 1 : 0;
        });
      },
    )
Run Code Online (Sandbox Code Playgroud)

结果: 结果-2


通过这种方法,您可以使用任何您想要的图标,并且不需要创建单独的图标AnimationController来控制过渡,这与AnimatedIcon不同


Pat*_*lly 12

你很幸运,我的朋友!Flutter 已经为您提供了 AnimatedIcon()

文档中的 AnimatedIcon 类

本周视频的动画图标小工具

现在使用 Flare 为您的图标设置动画。杰夫德莱尼为此做了一个很好的教程。

https://fireship.io/lessons/animated-navigation-flutter-flare/

  • 我知道,正如前面所说,如果您想要他们选择的图标,使用动画图标就可以了。但我想要我自己的图标,我想知道如何使用我的而不是他们的。他们的影响力对他们的偶像来说非常有效……只是不符合我想要的。 (4认同)
  • 您始终可以使用 CMD/Ctrl 单击 AnimatedIcons.someIcon 来查看它们是如何创建的,如果链接的教程无法让您到达您想要的位置,您可以制作自己的图标。 (2认同)

小智 5

Flutter 提供了AnimatedIcon可以使用,这是一个如何使用它的示例。

class _CreatePackageViewState extends State<CreatePackageView>
    with SingleTickerProviderStateMixin {  
       bool expanded = true;
       late AnimationController controller;
      @override
       void initState() {
        super.initState();
        controller = AnimationController(
          vsync: this,
          duration: Duration(milliseconds: 400),
          reverseDuration: Duration(milliseconds: 400),
        );
      }



      IconButton(
            icon: AnimatedIcon(
              icon: AnimatedIcons.menu_close,
              progress: controller,
              semanticLabel: 'Show menu',
            ),
            onPressed: () {
              setState(() {
                expanded ? controller.forward() : controller.reverse();
                expanded = !expanded;
              });
            }),

    }

Run Code Online (Sandbox Code Playgroud)

  • 虽然此代码可以解决问题,但[包括解释](https://meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers)确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而那些人可能不知道您的代码建议的原因 (10认同)