[英]Flutter Custom Animated Icon
我希望制作一个可以从列表中添加和删除的项目。
我正在寻找的是拥有 + 图标和 - 图标,并在这两个图标之间设置动画,以获得干净流畅的外观。
我有以下代码
Container(
padding: EdgeInsets.fromLTRB(0, 0, 20, 0),
child: AnimatedIcon(
icon: AnimatedIcons.home_menu,
progress: _animationController,
color: Colors.white,
))
和
void _handleOnPressed() {
setState(() {
isPlaying = !isPlaying;
isPlaying
? _animationController.forward()
: _animationController.reverse();
});
}
这对于 flutter 中的内置动画图标来说很好。
我希望使用 + 和 - 图标,但外观相同。
有没有办法做到这一点?
你很幸运,我的朋友! Flutter 已经为您提供了 AnimatedIcon()
现在用 Flare 为您的图标设置动画。 Jeff Delaney 为此做了一个很好的教程。
https://fireship.io/lessons/animated-navigation-flutter-flare/
class _CreatePackageViewState extends State<CreatePackageView>
with SingleTickerProviderStateMixin {
bool expanded = true;
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;
});
}),
}
自定义动画需要三样东西
下面的简单示例
import 'package:flutter/material.dart';
class AnimationsPractice extends StatefulWidget {
@override
_AnimationsPracticeState createState() => _AnimationsPracticeState();
}
class _AnimationsPracticeState extends State<AnimationsPractice> with SingleTickerProviderStateMixin {
AnimationController controller;
Animation animation;
@override
void initState() {
super.initState();
controller = AnimationController(
duration: Duration(milliseconds: 500),
vsync: this);
animation = ColorTween(begin: Colors.purple, end: Colors.red[700]).animate(controller);
controller.forward();
animation.addStatusListener((status) {
if(status == AnimationStatus.completed){
controller.reverse(from: 1.0);
print(animation.value);
}
else if(status == AnimationStatus.dismissed){
controller.forward();
}
});
controller.addListener(() {
setState(() {
});
print(controller.value);
});
}
@override
void dispose() {
super.dispose();
controller.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.yellow,
appBar: AppBar(
title: Text('Heart Beat'),
),
body: Center(
child: Icon(
Icons.favorite,
size: animation.value * 100,
),
),
);
}
}
使用 animated_icon_button package https://pub.dev/packages/animated_icon_button/install
我知道它不如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;
});
},
);
或者您可以使用ScaleTransition
代替FadeTransition
,并获得更相似的 animation:
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;
});
},
)
使用这种方法,您可以使用任何您想要的图标,并且不需要创建单独的AnimationController
来控制过渡,这与AnimatedIcon不同
看起来 Flutter 团队使用名为 vitool 的命令行工具将 SVG 转换为可用作动画图标的路径。 如果你有一个相对简单的动画 SVG,你可以尝试在这个存储库的 bin 文件夹中运行 main.dart 命令行工具:https://github.com/flutter/flutter/tree/master/dev/tools/vitool 。 我还没有尝试过,但如果我发现任何问题,我会尝试并报告。 output 应如下所示:
const _AnimatedIconData _$menu_arrow = _AnimatedIconData(
Size(48.0, 48.0),
<_PathFrames>[
_PathFrames(
opacities: <double>[
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
(...)
您可以简单地使用animate_icons 2.0.0
package 为两个图标设置动画
AnimateIcons(
startIcon: Icons.add_circle,
endIcon: Icons.add_circle_outline,
size: 100.0,
controller: controller,
// add this tooltip for the start icon
startTooltip: 'Icons.add_circle',
// add this tooltip for the end icon
endTooltip: 'Icons.add_circle_outline',
size: 60.0,
onStartIconPress: () {
print("Clicked on Add Icon");
return true;
},
onEndIconPress: () {
print("Clicked on Close Icon");
return true;
},
duration: Duration(milliseconds: 500),
startIconColor: Colors.deepPurple,
endIconColor: Colors.deepOrange,
clockwise: false,
),
https://pub.dev/packages/animate_icons
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.