[英]how to handle state of the app or lifecycle in stacked or provider architecture in flutter
I am using stacked architecture in my project.我在我的项目中使用堆叠架构。 Here is my code这是我的代码
class InfoScreen extends StatelessWidget {
InfoViewModel viewModel;
@override
Widget build(BuildContext context) {
return ViewModelBuilder<InfoViewModel>.reactive(
builder: (context, model, child) => _buildUI(model),
viewModelBuilder: () => InfoViewModel());
}
_buildUI(InfoViewModel viewModel) {
return Scaffold(backgroundColor: Colors.white, body: MainScreen());
}
}
I am using the Stateless widget, So I can't use the didChangeDependencies() method to know the app state.我正在使用无状态小部件,因此我无法使用didChangeDependencies()方法来了解应用程序状态。
My Question is How do I handle app state in this screen?我的问题是如何处理此屏幕中的应用程序状态? any help or idea is appreciated.任何帮助或想法表示赞赏。 thanks in advance提前致谢
You can implement didChangeDependencies() in your ViewModel.您可以在 ViewModel 中实现didChangeDependencies() 。
For example:例如:
class InfoViewModel extends BaseViewModel with WidgetsBindingObserver{
void initialise() {
WidgetsBinding.instance.addObserver(this);
}
@override
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
switch (state) {
case AppLifecycleState.resumed:
print('On Resume');
break;
case AppLifecycleState.inactive:
case AppLifecycleState.paused:
case AppLifecycleState.detached:
break;
}
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
}
Don't forget to call onModelReady: (model) => model.initialise()
in your View widget.不要忘记在您的 View 小部件中调用onModelReady: (model) => model.initialise()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.