gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:38:51 28 4
gpt4 key购买 nike

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

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

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

enter image description here

最佳答案

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

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

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

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

这是 codepen 和 dartpad 上的示例:

enter image description here

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

),
),
);
}
}

关于flutter - 如何在 flutter 中缩小和增大图标动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61337709/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com