【问题标题】:Android onResume() method equivalent in FlutterFlutter 中等效的 Android onResume() 方法
【发布时间】:2018-11-03 04:37:05
【问题描述】:

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

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

有什么想法吗?

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    您可以使用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
        }
      }
    }
    

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

    如果你只是想在你的 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

    更新:安全合规性为零

    【讨论】:

    • addPostFrameCallbackLayoutBuilderbuild() 方法是否同时接收控制/焦点?
    • 不,在树的第一次渲染之后调用 addPostFrameCallback。
    • 尝试在WidgetsBinding.instance?.addObserver(this);WidgetsBinding.instance?.removeObserver(this); 中添加? 以实现空安全......
    • AppLifecycleState.resumed 对于 iOS 和 Android 有不同的行为。在 android 上,它仅在我们从后台到前台时调用(而不是在屏幕加载时)。对于 iOS,它也调用了屏幕加载。这是故意的吗?
    • stackoverflow.com/a/58504433/5219642您可以查看此以获取更多详细信息
    【解决方案2】:

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

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

    【讨论】:

    • 非常感谢。就我而言,当我从一个流行事件回来时,我不得不强制刷新状态。我不能使用 push 事件,不得不使用 pop,它没有 then 方法。所以这很完美。
    • 如果我可以从页面 A 导航到五个不同的页面,那么我需要在 5 个位置添加 _refreshFirstPage();。我希望有更好的方法来做到这一点。
    【解决方案3】:

    您可以通过注册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

    【讨论】:

      【解决方案4】:

      使用focus_detector更多信息可以看visibility_detector

      @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(),
          );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-15
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-25
        • 1970-01-01
        相关资源
        最近更新 更多