我正在尝试在 Flutter 中制作一个径向菜单,并希望我的菜单按钮每次按下时都有一个旋转动画。我在 Youtube 上关注了 TensorProgramming 的 Flutter 动画基础教程,但由于某种原因我的动画无法正常工作。下面我包含了我的 RadialMenuWidget 的代码。
我已经正确处理了动画控制器,使用 SingleTickerProviderStateMixin 扩展了小部件的状态。PS我在模拟器上运行,当我热重载时,图标的旋转有时会发生变化。
任何帮助将不胜感激!
import 'package:flutter/material.dart';
import 'package:savings/utils/colors.dart';
import 'package:savings/widgets/themed_radial_menu_item.dart';
class CustomThemedRadialMenu extends StatefulWidget {
final List<CustomThemedRadialMenuItem> items;
CustomThemedRadialMenu({@required this.items});
@override
_CustomThemedRadialMenuState createState() =>
_CustomThemedRadialMenuState();
}
class _CustomThemedRadialMenuState extends State<CustomThemedRadialMenu>
with SingleTickerProviderStateMixin {
Animation animationOpenClose;
AnimationController animationControllerOpenClose;
bool isOpen;
@override
void initState() {
isOpen = false;
animationControllerOpenClose =
AnimationController(duration: new Duration(seconds: 5), vsync: this);
animationOpenClose =
Tween(begin: 0.0, end: 360.0).animate(animationControllerOpenClose)
..addListener(() {
setState(() {});
});
animationControllerOpenClose.repeat();
super.initState();
}
@override
Widget build(BuildContext context) {
///A list of the items and the center menu button
final List<Widget> menuContents = <Widget>[];
for (int i = 0; i < widget.items.length; i++) {
///Menu items
menuContents.add(widget.items[1]);
///Menu Close/Open button
menuContents.add(new InkWell(
onTap: () {},
child: Container(
padding: new EdgeInsets.all(10.0),
decoration: new BoxDecoration(
color: Colors.white,
border: new Border.all(color: darkHeadingsTextColor),
shape: BoxShape.circle,
),
child: new Transform.rotate(
angle: animationControllerOpenClose.value,
child: isOpen ? new Icon(Icons.clear) : new Icon(Icons.menu)),
),
));
}
return new Stack(
alignment: Alignment.center,
children: menuContents,
);
}
@override
void dispose() {
animationControllerOpenClose.dispose();
super.dispose();
}
closeMenu() {
animationControllerOpenClose.forward();
setState(() {
isOpen = false;
});
print("RadialMenu Closed");
}
openMenu() {
animationControllerOpenClose.forward();
setState(() {
isOpen = true;
});
print("RadialMenu Opened");
}
}