如何在颤动中缩小和增大图标动画?

How to shrink and grow icon animation in flutter?

我正在使用下面的代码来执行图标的动画增长和收缩动画但是为此,我必须单击图标,我希望动画在我们进入屏幕后重复。

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,
          ),
        ],
      ),
    );

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

您可以使用多种方法解决此问题。我更喜欢使用自己重复的 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),
              ),
            ),
          ),

        ),
      ),
    );
  }
}