我正在尝试使用 WidgetsBindingObserver 来查看我的应用程序是否被带到前台。但似乎并没有什么作用。它只适用于有状态的小部件吗?
class TheHomeView extends StatelessWidget with WidgetsBindingObserver {
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
print('lifecycle changed');
if (state == AppLifecycleState.resumed) {
print('resumed');
showLatestGroupNotification();
}
}
Run Code Online (Sandbox Code Playgroud)
事实证明,您可以在无状态小部件中使用它。但你需要使用
WidgetsBinding.instance.addObserver(this);
Run Code Online (Sandbox Code Playgroud)
您可以在小部件的构造函数中执行此操作。但是如果你想删除 dispose 上的绑定
WidgetsBinding.instance.removeObserver(this);
Run Code Online (Sandbox Code Playgroud)
您需要一个仅在有状态小部件中可用的处置。或者你必须手动完成。
https://dev.to/pedromassango/onresume-and-onpause-for-widgets-on-flutter-27k2
| 归档时间: |
|
| 查看次数: |
4244 次 |
| 最近记录: |