Flutter IOS AppLifecycleState不工作

xqkwcwgp  于 2023-04-07  发布在  Flutter
关注(0)|答案(1)|浏览(269)

使用Flutter Stable 3.3.5和dart 2.18.2,使用下面的代码,我可以在Android的logcat中看到状态更改和打印的响应,但在iOS的xcode控制台中,我什么也看不到。看起来Flutter的这一部分现在已经不再适用于iOS了,如果它曾经有过的话。
我正在使用WidgetsBindingObserver。
我试过将我的主要脚手架 Package 在手势检测器中,并使用计时器以某种方式触发iOS,但这也不起作用。
我想知道接下来检测iOS的AppLifecycleState的解决方案是什么?

class MyApp extends StatefulWidget {
 
  @override
  State<StatefulWidget> createState() => _MyAppState();
}

 class _MyAppState extends State<MyApp> with WidgetsBindingObserver {

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
     
    switch (state) {
      case AppLifecycleState.resumed:
        print:("resume");
        break;
      case AppLifecycleState.inactive:
        print"inactive");
        break;
      case AppLifecycleState.paused:
        print("paused");
        break;
      case AppLifecycleState.detached:
        print("detached");
        break;
    }
  }
}
sg24os4d

sg24os4d1#

你做了widgetbindingobserver吗?

class CameraState extends State<Camera> with WidgetsBindingObserver {

@override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
     
    switch (state) {
      case AppLifecycleState.resumed:
        print:("resume");
        break;
      case AppLifecycleState.inactive:
        print"inactive");
        break;
      case AppLifecycleState.paused:
        print("paused");
        break;
      case AppLifecycleState.detached:
        print("detached");
        break;
    }
  }
}

相关问题