WidgetsBindingObserver監(jiān)測(cè)頁(yè)面生命周期

class FlutterLifeCycleState extends State<FlutterLifeCycle>
    with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this); //添加觀察者
  }

  ///生命周期變化時(shí)回調(diào)
//  resumed:應(yīng)用可見并可響應(yīng)用戶操作
//  inactive:用戶可見,但不可響應(yīng)用戶操作
//  paused:已經(jīng)暫停了,用戶不可見、不可操作
//  suspending:應(yīng)用被掛起,此狀態(tài)IOS永遠(yuǎn)不會(huì)回調(diào)
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    print("@@@@@@@@@  didChangeAppLifecycleState: $state");
  }

  ///當(dāng)前系統(tǒng)改變了一些訪問性活動(dòng)的回調(diào)
  @override
  void didChangeAccessibilityFeatures() {
    super.didChangeAccessibilityFeatures();
    print("@@@@@@@@@ didChangeAccessibilityFeatures");
  }

  /// Called when the system is running low on memory.
  ///低內(nèi)存回調(diào)
  @override
  void didHaveMemoryPressure() {
    super.didHaveMemoryPressure();
    print("@@@@@@@@@ didHaveMemoryPressure");
  }

  /// Called when the system tells the app that the user's locale has
  /// changed. For example, if the user changes the system language
  /// settings.
  ///用戶本地設(shè)置變化時(shí)調(diào)用,如系統(tǒng)語(yǔ)言改變
  @override
  void didChangeLocales(List<Locale> locale) {
    super.didChangeLocales(locale);
    print("@@@@@@@@@ didChangeLocales");
  }

  /// Called when the application's dimensions change. For example,
  /// when a phone is rotated.
  ///應(yīng)用尺寸改變時(shí)回調(diào),例如旋轉(zhuǎn)
  @override
  void didChangeMetrics() {
    super.didChangeMetrics();
    Size size = WidgetsBinding.instance.window.physicalSize;
    print("@@@@@@@@@ didChangeMetrics  :寬:${size.width} 高:${size.height}");
  }

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

  ///文字系數(shù)變化
  @override
  void didChangeTextScaleFactor() {
    super.didChangeTextScaleFactor();
    print(
        "@@@@@@@@@ didChangeTextScaleFactor  :${WidgetsBinding.instance.window.textScaleFactor}");
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Text("flutter"),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    WidgetsBinding.instance.removeObserver(this); //銷毀觀察者
  }
}

//屏幕旋轉(zhuǎn)
//豎屏切橫屏
//2019-05-22 09:04:58.350 27881-27901/com.yourcompany.test1 I/flutter: @@@@@@@@@ didChangeLocales
//2019-05-22 09:04:58.353 27881-27901/com.yourcompany.test1 I/flutter: @@@@@@@@@ didChangeTextScaleFactor  :1.0
//2019-05-22 09:04:58.354 27881-27901/com.yourcompany.test1 I/flutter: @@@@@@@@@ didChangePlatformBrightness
//2019-05-22 09:04:58.401 27881-27901/com.yourcompany.test1 I/flutter: @@@@@@@@@ didChangeLocales
//2019-05-22 09:04:58.402 27881-27901/com.yourcompany.test1 I/flutter: @@@@@@@@@ didChangeTextScaleFactor  :1.0
//2019-05-22 09:04:58.402 27881-27901/com.yourcompany.test1 I/flutter: @@@@@@@@@ didChangePlatformBrightness
//2019-05-22 09:04:58.404 27881-27901/com.yourcompany.test1 I/flutter: @@@@@@@@@ didChangeMetrics  :寬:1080.0 高:2280.0
//2019-05-22 09:04:58.405 27881-27901/com.yourcompany.test1 I/flutter: @@@@@@@@@ didChangeMetrics  :寬:2198.0 高:1080.0
//橫屏切豎屏
//2019-05-22 09:05:05.714 27881-27901/com.yourcompany.test1 I/flutter: @@@@@@@@@ didChangeLocales
//2019-05-22 09:05:05.715 27881-27901/com.yourcompany.test1 I/flutter: @@@@@@@@@ didChangeTextScaleFactor  :1.0
//2019-05-22 09:05:05.715 27881-27901/com.yourcompany.test1 I/flutter: @@@@@@@@@ didChangePlatformBrightness
//2019-05-22 09:05:05.766 27881-27901/com.yourcompany.test1 I/flutter: @@@@@@@@@ didChangeLocales
//2019-05-22 09:05:05.767 27881-27901/com.yourcompany.test1 I/flutter: @@@@@@@@@ didChangeTextScaleFactor  :1.0
//2019-05-22 09:05:05.767 27881-27901/com.yourcompany.test1 I/flutter: @@@@@@@@@ didChangePlatformBrightness
//2019-05-22 09:05:05.768 27881-27901/com.yourcompany.test1 I/flutter: @@@@@@@@@ didChangeMetrics  :寬:2198.0 高:1080.0
//2019-05-22 09:05:05.769 27881-27901/com.yourcompany.test1 I/flutter: @@@@@@@@@ didChangeMetrics  :寬:1080.0 高:2280.0

//改變系統(tǒng)語(yǔ)言,回到應(yīng)用才調(diào)用以下方法
//2019-05-22 09:08:04.428 27881-27901/com.yourcompany.test1 I/flutter: @@@@@@@@@ didChangeLocales
//2019-05-22 09:08:04.429 27881-27901/com.yourcompany.test1 I/flutter: @@@@@@@@@ didChangeTextScaleFactor  :1.0
//2019-05-22 09:08:04.429 27881-27901/com.yourcompany.test1 I/flutter: @@@@@@@@@ didChangePlatformBrightness


//  inactive:用戶可見,但不可響應(yīng)用戶操作
//  resumed:應(yīng)用可見并可響應(yīng)用戶操作
//  paused:已經(jīng)暫停了,用戶不可見、不可操作
//  suspending:應(yīng)用被掛起,此狀態(tài)IOS永遠(yuǎn)不會(huì)回調(diào)

//  Home鍵退出,鎖屏
//  2019-05-22 08:29:26.321 27526-27546/com.yourcompany.test1 I/flutter:  AppLifecycleState.inactive
//  2019-05-22 08:29:26.378 27526-27546/com.yourcompany.test1 I/flutter: AppLifecycleState.paused
//  Home鍵退出后再點(diǎn)擊應(yīng)用圖標(biāo)啟動(dòng),解鎖屏幕
//  2019-05-22 08:30:37.533 27526-27546/com.yourcompany.test1 I/flutter: AppLifecycleState.inactive
//  2019-05-22 08:30:37.535 27526-27546/com.yourcompany.test1 I/flutter: AppLifecycleState.resumed
//  進(jìn)入該頁(yè)面或者back鍵退出該頁(yè)面,沒有調(diào)用didChangeAppLifecycleState方法

碼云地址:https://gitee.com/xgljh/Flutter.git

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容