Flutter development practice-WidgetsBinding monitors the exit status of the foreground and background of the page

Flutter development practice-WidgetsBinding monitors the exit status of the foreground and background of the page

During the development process, we often monitor the exit status of the foreground and background of the page. WidgetsBinding is used here

1. What is WidgetsBinding?

WidgetsBinding is one of the most important Bindings in Flutter, which provides interaction with the Widget tree. In Flutter, all UIs are Widgets, from the simplest text to the most complex layout, can be described by the Widget tree. WidgetsBinding is responsible for handling user input events, layout and drawing. It also handles platform channels for interacting with the system, such as calling native code and handling notifications. WidgetsBinding provides some commonly used methods, such as addPostFrameCallback, deferredUnmount, getRendererBinding, etc.

2. Monitor the exit status of the foreground and background of the page

WidgetsBinding monitors the exit status of the foreground and background of the page, we need to add it to the Widget

with WidgetsBindingObserver

Add observers to initState

 WidgetsBinding.instance?.addObserver(this);

Remove the observer in dispose

 WidgetsBinding.instance?.removeObserver(this);

Then monitor the exit status of the page foreground and background through the response method didChangeAppLifecycleState

// WidgetBinding
  ///生命周期变化时回调
  //  resumed:应用可见并可响应用户操作
  //  inactive:用户可见,但不可响应用户操作
  //  paused:已经暂停了,用户不可见、不可操作
  //  suspending:应用被挂起,此状态IOS永远不会回调
  
  void didChangeAppLifecycleState(AppLifecycleState state) {
    
    
    super.didChangeAppLifecycleState(state);
    switch (state) {
    
    
      case AppLifecycleState.inactive:
        {
    
    
          // 处于这种状态的应用程序应该假设它们可能在任何时候暂停。
          break;
        }
      case AppLifecycleState.resumed:
        {
    
    
          // 应用程序可见,前台
          if (_isAppBackground == true) {
    
    
            setState(() {
    
    });

            _isAppBackground = false;
          }

          break;
        }
      case AppLifecycleState.paused:
        {
    
    
          // 应用程序不可见,后台
          _isAppBackground = true;
          break;
        }
      case AppLifecycleState.detached:
        {
    
    
          // 页面退出
          break;
        }
    }
  }

  ///当前系统改变了一些访问性活动的回调
  
  void didChangeAccessibilityFeatures() {
    
    
    super.didChangeAccessibilityFeatures();
  }

  /// Called when the system is running low on memory.
  ///低内存回调
  
  void didHaveMemoryPressure() {
    
    
    super.didHaveMemoryPressure();
  }

  /// Called when the application's dimensions change. For example,
  /// when a phone is rotated.
  ///应用尺寸改变时回调,例如旋转
  
  void didChangeMetrics() {
    
    
    super.didChangeMetrics();
  }

  /// {@macro on_platform_brightness_change}
  
  void didChangePlatformBrightness() {
    
    
    super.didChangePlatformBrightness();
  }

  ///文字系数变化
  
  void didChangeTextScaleFactor() {
    
    
    super.didChangeTextScaleFactor();
  }

3. Summary

Flutter development practice-WidgetsBinding monitors the exit status of the foreground and background of the page.

https://blog.csdn.net/gloryFlow/article/details/132253284
learning records, making progress every day.

Guess you like

Origin blog.csdn.net/gloryFlow/article/details/132253284