我的应用程序有几页。在appbar中,我有一个selfmade有状态的小部件,它的徽章显示了新消息的数量。当我滑动刷新数据时,如果徽章值被更改,该徽章将运行一个小动画。
问题是,警徽值来自一个作用域模型。如何从作用域模型类运行动画。我试着让作用域模型类保存animationController和函数。它在第一和第二屏幕上工作。但是,当我再次导航回第一页,并拉刷新。就像animationController处于糟糕的状态。
作用域模型中的代码:
Function _runNotificationAnimation;
set runNotificationAnimation(Function fun) => _runNotificationAnimation = fun;
void _setNotificationCount(int count) {
_notificationCount = count;
if (count > 0 && _runNotificationAnimation != null) {
_runNotificationAnimation();
}
notifyListeners();
}运行动画的函数。
runAnim() {
setState(() {
controller.reset();
controller.forward(from: 0.0);
});
}颤振产生的错误:
详细-2:shell.cc. on (184) Dart错误:未处理异常: NoSuchMethodError:方法'stop‘在null上被调用。接收方:空尝试调用:停止(取消:真)0 Object.noSuchMethod (dart:core/运行时/libobject_patch.dart:50:5)1 AnimationController.stop 2 AnimationController.value= AnimationController.value=3 AnimationController.reset (package:flutter/src/animation/animation_controller.dart:370:5) 4 NotificationIconState(package:volvopenta/widgets/notificaton_icon.dart:38:16) 5 (package:volvopenta/scoped-models/settings-model.dart:57:7) 6 SettingsModel.updateAppData (package:volvopenta/scoped-models/settings-model.dart:185:5) 7 MyMachines.build.(软件包:volvopenta/page/flet.dart:83:27) 8<…>
发布于 2019-12-29 02:56:30
因为您的动画将构建在有状态Widget中,所以最好将animationController保留在有状态Widget中,而只移动模型类中的动画(Tween)。重要的是将notifyListener()放在controller.addListerner()中,而不是放在函数的末尾。
class MyModel extends Model{
Animation animation;
runAnimation(controller) {
animation = Tween(begin: 0,0, end:
400).animate(controller);
controller.forward();
controller.addListener((){
notifyListeners();
});
}
}您可以在有状态小部件中调用此函数,如下所示:
class _MyScreenState extends State<MyScreen> with
SingleTickerProviderStateMixin{
AnimationController controller;
MyModel myModel = MyModel();
@overide
void initState(){
super.initState();
controller = AnimationController(duration: Duration(seconds: 2), vsync:
this);
myModel.runAnimation(controller);
}
//dispose here
@override
Widget build(Buildcontext context){
return ScopedModel<MyModel>(
model: myModel,
child: Scaffold(
body: Text("Hello", style: TextStyle(fontSize: 13 *
controller.value)),
),
);
}
}https://stackoverflow.com/questions/53349931
复制相似问题