【问题标题】:How to shrink and grow icon animation in flutter?如何在颤动中缩小和放大图标动画?
【发布时间】:2020-08-03 20:32:04
【问题描述】:

我正在使用下面的代码来执行图标的动画增长和缩小动画,但为此,我必须点击图标,我希望动画在屏幕上重复。

return new Container(
      child: new Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          new IconButton(
            onPressed: () {
              setState(() {
                if (_resized) {
                  _resized = false;
                  _height = 20.0;
                } else {
                  _resized = true;
                  _height = 40.0;
                }
              });
            },
            icon: Icon(Icons.calendar_today, size: _height),
            color: _color,
          ),
        ],
      ),
    );

我想要如下所示的输出,其中外部部分不断增长和缩小。

【问题讨论】:

    标签: flutter flutter-layout flutter-animation flutter-widget


    【解决方案1】:

    您可以使用多种方法来解决此问题。我更喜欢使用可重复的 AnimationController。

    animationController = AnimationController(
      vsync: this,
      duration: Duration(seconds: 1),
    )
      ..forward()
      ..repeat(reverse: true);
    

    例如,您可以为按钮周围的填充大小设置动画。我会在 IconButton 周围使用圆形容器。

    为此,您需要在您的状态下初始化 AnimationController。记住在小部件的生命周期结束时释放它。

    这是一个关于 codepen 和 dartpad 的示例:

    https://codepen.io/orestesgaolin/pen/MWajRGV

    https://dartpad.dartlang.org/ca4838f17ea6061cf0212a4b689eaf2a

    完整的源代码可以在这个要点中找到

    import 'package:flutter/material.dart';
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Animated Icon',
          theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
      AnimationController animationController;
    
      @override
      void initState() {
        super.initState();
        animationController = AnimationController(
          vsync: this,
          duration: Duration(seconds: 1),
        )
          ..forward()
          ..repeat(reverse: true);
      }
    
      @override
      void dispose() {
        animationController.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Container(
            color: Colors.blue,
            child: Center(
              child: AnimatedBuilder(
                animation: animationController,
                builder: (context, child) {
                  return Container(
                    decoration: ShapeDecoration(
                      color: Colors.white.withOpacity(0.5),
                      shape: CircleBorder(),
                    ),
                    child: Padding(
                      padding: EdgeInsets.all(8.0 * animationController.value),
                      child: child,
                    ),
                  );
                },
                child: Container(
                  decoration: ShapeDecoration(
                    color: Colors.white,
                    shape: CircleBorder(),
                  ),
                  child: IconButton(
                    onPressed: () {},
                    color: Colors.blue,
                    icon: Icon(Icons.calendar_today, size: 24),
                  ),
                ),
              ),
    
            ),
          ),
        );
      }
    }
    

    【讨论】:

    • 谢谢。这段代码运行良好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多