Android Flutter 中等效的 onResume() 方法

Android onResume() method equivalent in Flutter

我正在开发 Flutter 应用程序,需要弹出屏幕。我尝试了 initState() 方法,但没有成功。 initState() 在我第一次打开 class 时被调用。

我们在 Flutter 中是否有等同于 Android onResume() 的方法?

有什么想法吗?

您可以使用 WidgetsBindingObserver 并检查 AppLifeCycleState 就像这个例子:

class YourWidgetState extends State<YourWidget> with WidgetsBindingObserver {

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

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

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
       //do your stuff
    }
  }
}

请记住,它会在您每次打开应用程序或进入后台并 return 进入应用程序时调用。 (如果您的小部件处于活动状态)

如果您只想在第一次加载 Widget 时使用监听器,您可以使用 addPostFrameCallback 进行监听,例如:

class YourWidgetState extends State<YourWidget> {

  _onLayoutDone(_) {
    //do your stuff
  }

  @override
  void initState() {
    WidgetsBinding.instance?.addPostFrameCallback(_onLayoutDone);
    super.initState();
  } 

}

信息:https://docs.flutter.io/flutter/widgets/WidgetsBindingObserver-class.html

更新:安全合规性无效

您可以通过注册一个 didChangeAppLifecycleState 观察者来实现:

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

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

  @override
  void didChangeAppLifecycleState(final AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      setState(() {
        // ...your code goes here...
      });
    }
  }

  @override
  Widget build(final BuildContext context) {
    // ...your code goes here...
  }
}

有关详细信息,请参阅 WidgetsBindingObserver

如果你转到另一个页面,然后在你回来时调用

Navigator.push(
  context,
  MaterialPageRoute(
    builder: (context) => SecondPage(),
  ),
).then((value) {
  _refreshFirstPage();
});

使用focus_detector more information can see visibility_detector

每次您的小部件从屏幕上出现或消失时都会收到通知。

类似于 Android 上的 onResume()/onPause() 和 iOS 上的 viewDidAppear()/viewDidDisappear()。

焦点检测器会在您的小部件焦点发生变化时为您触发回调。例如,这样的事件可能是用户:

导航to/from另一个屏幕;

在您的小部件可见时转动设备的屏幕 on/off;

在您的小部件可见时切换to/from 另一个应用程序;

滚动您的小部件in/out 屏幕;

@override
Widget build(BuildContext context) =>
    FocusDetector(
      onFocusLost: () {
        logger.i(
          'Focus Lost.'
          '\nTriggered when either [onVisibilityLost] or [onForegroundLost] '
          'is called.'
          '\nEquivalent to onPause() on Android or viewDidDisappear() on iOS.',
        );
      },
      onFocusGained: () {
        logger.i(
          'Focus Gained.'
          '\nTriggered when either [onVisibilityGained] or [onForegroundGained] '
          'is called.'
          '\nEquivalent to onResume() on Android or viewDidAppear() on iOS.',
        );
      },
      onVisibilityLost: () {
        logger.i(
          'Visibility Lost.'
          '\nIt means the widget is no longer visible within your app.',
        );
      },
      onVisibilityGained: () {
        logger.i(
          'Visibility Gained.'
          '\nIt means the widget is now visible within your app.',
        );
      },
      onForegroundLost: () {
        logger.i(
          'Foreground Lost.'
          '\nIt means, for example, that the user sent your app to the background by opening '
          'another app or turned off the device\'s screen while your '
          'widget was visible.',
        );
      },
      onForegroundGained: () {
        logger.i(
          'Foreground Gained.'
          '\nIt means, for example, that the user switched back to your app or turned the '
          'device\'s screen back on while your widget was visible.',
        );
      },
      child: Container(),
    );