【发布时间】:2019-11-17 07:46:43
【问题描述】:
这是我在课堂上使用的简单动画
controller = AnimationController(vsync: this, duration: Duration(milliseconds: 200));
offset = Tween<Offset>(begin: Offset.zero, end: Offset(0.0, -1.0)).animate(controller);
controller.forward();
因为我只能在课堂内管理动画控制器,例如 forward、reverse,我尝试将此控制器移动到课堂外,就像另一个课堂 NotificationBarController 一样,是否有任何机构可以帮助我我实现了这个解决方案?
例如
controller = NotificationBarController();
controller.forward();
源代码
class NotificationBar extends StatefulWidget {
final Widget contentWidget;
final Widget barWidget;
NotificationBar({@required this.contentWidget, @required this.barWidget});
@override
State<StatefulWidget> createState() => NotificationBarState();
}
class NotificationBarState extends State<NotificationBar> with SingleTickerProviderStateMixin {
AnimationController controller;
Animation<Offset> offset;
@override
void initState() {
super.initState();
controller = AnimationController(vsync: this, duration: Duration(milliseconds: 200));
// offset = Tween<Offset>(begin: Offset.zero, end: Offset(1.0, 0.0)).animate(controller); from right
offset = Tween<Offset>(begin: Offset.zero, end: Offset(0.0, -1.0)).animate(controller);
controller.forward();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
),
);
}
}
我厌倦了实现它,但这是错误的
class NotificationBarController extends ChangeNotifier {
NotificationBarController({@required TickerProvider vsync}): _animationController = AnimationController(vsync: vsync);
Animation<double> get animation => _animationController?.view ?? kAlwaysCompleteAnimation;
Animation<double> get state => _animationController?.view ?? kAlwaysCompleteAnimation;
}
【问题讨论】: