[flutter]监听系统主题改变

/ 65评论 / 1357阅读 / 0点赞

项目需要跟随系统主题变化,修改一些自定义组件的样式,但百度一直没找到可以监听系统主题变化的方法,而 MaterialApp 是有一个设置ThemeMode.system,使得app可以跟随系统主题变化而切换使用传入的theme和darkTheme,因此我就去看了看里面的源码,找到了监听系统主题变化的函数绑定方法。

解决

import 'dart:ui' as ui;
    ui.PlatformDispatcher.instance.onPlatformBrightnessChanged = () {
      // 设置跟随系统主题变化
      followingSystem();
    };
ui.PlatformDispatcher.instance.onPlatformBrightnessChanged = null;
if(null == ui.PlatformDispatcher.instance.onPlatformBrightnessChanged) {
    // 当前没有在监听
} else {
    // 有在监听
}
    ui.PlatformDispatcher.instance.onPlatformBrightnessChanged = () {
       fun1();
       fun2();
       fun3();
       fun4();
       ......
    };
if(ui.PlatformDispatcher.instance.platformBrightness == Brightness.dark) {
    // 系统已经转变为夜间模式
    // app跟随修改主题为夜间
} else {
    // 系统已经转变为白天模式
    // app跟随修改主题为白天
}
// 进入app时手动调用一次 followingSystem(), 然后调用 setFollowingSystem() 绑定监听函数

  void followingSystem() {
    if (ui.PlatformDispatcher.instance.platformBrightness == Brightness.dark) {
      // app跟随修改主题为夜间
    } else {
      // app跟随修改主题为白天
    }
  }

  // 设置跟随系统主题变化
  void setFollowingSystem() {
    ui.PlatformDispatcher.instance.onPlatformBrightnessChanged = () {
      followingSystem();
    };
  }
  1. dobry sklep说道:

    Wow, superb weblog structure! How long have you ever been running a blog for?
    you make running a blog look easy. The full glance of your web site is excellent, as
    smartly as the content! You can see similar here e-commerce

  2. ラブドール アニメ キャラ 138cmアイアンテックリーズナブルラブドールDoll4Ever165cm –5ft5巨大な胸新しいWM人形168cm–5ft6ビスチェで覆われた大きなヒップの小さなおっぱい

  3. hitman.Agency说道:

    Hi there! Do you know if they make any plugins to help with Search Engine Optimization? I’m trying to get
    my blog to rank for some targeted keywords but I’m not seeing
    very good results. If you know of any please share. Thanks!
    You can read similar blog here: GSA List

  4. Hi! Do you know if they make any plugins to assist with Search Engine Optimization? I’m trying
    to get my site to rank for some targeted keywords but I’m not
    seeing very good success. If you know of any please share.
    Thank you! I saw similar blog here: Auto Approve List

  5. najlepszy sklep说道:

    Hello there! Do you know if they make any plugins to assist with Search
    Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m not
    seeing very good success. If you know of any please share.

    Many thanks! You can read similar article here: Sklep online

  6. najlepszy sklep说道:

    Wow, fantastic blog structure! How long have you ever been running a blog for?

    you made running a blog look easy. The total glance of your website is
    wonderful, as neatly as the content material! You can see similar here ecommerce

  7. hd porn videos download ggjgodherogg.8fMb7gOEkoG

  8. Ignatius Rolfson说道:

    Hi Neat post Theres an issue together with your web site in internet explorer may test this IE still is the marketplace chief and a good component of people will pass over your fantastic writing due to this problem

  9. temp mail说道:

    Hello, I thought you had looked at my blog, so I came back to say hello. Since I’m trying to improve my website, I suppose I can use some of your advice.

回复 נערת ליווי 取消回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注