项目需要跟随系统主题变化,修改一些自定义组件的样式,但百度一直没找到可以监听系统主题变化的方法,而 MaterialApp 是有一个设置ThemeMode.system,使得app可以跟随系统主题变化而切换使用传入的theme和darkTheme,因此我就去看了看里面的源码,找到了监听系统主题变化的函数绑定方法。
解决
- 首先需要import标准库,flutter自带的,不需要pub等方式下载:
import 'dart:ui' as ui;
- 系统主题变化函数绑定:
- 直接把下面代码中的onPlatformBrightnessChanged 赋值一个我们想执行的函数即可
ui.PlatformDispatcher.instance.onPlatformBrightnessChanged = () {
// 设置跟随系统主题变化
followingSystem();
};
- -
- 如果想取消监听,则同样把它赋值为null
ui.PlatformDispatcher.instance.onPlatformBrightnessChanged = null;
- -
- 如果想判断现在是否有在监听,则判断它是否不为null
if(null == ui.PlatformDispatcher.instance.onPlatformBrightnessChanged) {
// 当前没有在监听
} else {
// 有在监听
}
- -
- 注意 onPlatformBrightnessChanged 其实就是一个变量而已,你如果给它赋值好几次,那显然它的值就是最后一次赋值的结果
- 因此重复赋值并不会让它能执行多个函数
- 如果需要执行多个函数,应当把这些函数放进一个函数里,然后把它赋值给 onPlatformBrightnessChanged :
ui.PlatformDispatcher.instance.onPlatformBrightnessChanged = () {
fun1();
fun2();
fun3();
fun4();
......
};
- 系统主题变化时会执行 onPlatformBrightnessChanged 绑定的函数,那么我怎么知道现在系统的主题是什么类型呢?
- 可以使用如下代码判断:
if(ui.PlatformDispatcher.instance.platformBrightness == Brightness.dark) {
// 系统已经转变为夜间模式
// app跟随修改主题为夜间
} else {
// 系统已经转变为白天模式
// app跟随修改主题为白天
}
- 还有一个问题需要注意,启动app时,并不会触发上面绑定的函数
- 假设我们给app的默认主题是白天模式,此时系统已经是夜间模式,然后用户启动app,此时并不会触发 onPlatformBrightnessChanged ,因此app呈现的还是白天模式
- 解决方法:显然就是进入app时,我们手动执行一次判断系统主题,跟随变化,然后绑定监听函数即可:
// 进入app时手动调用一次 followingSystem(), 然后调用 setFollowingSystem() 绑定监听函数
void followingSystem() {
if (ui.PlatformDispatcher.instance.platformBrightness == Brightness.dark) {
// app跟随修改主题为夜间
} else {
// app跟随修改主题为白天
}
}
// 设置跟随系统主题变化
void setFollowingSystem() {
ui.PlatformDispatcher.instance.onPlatformBrightnessChanged = () {
followingSystem();
};
}
PBN sites
We’ll create a structure of privately-owned blog network sites!
Benefits of our privately-owned blog network:
We perform everything so GOOGLE does not comprehend THAT this A self-owned blog network!!!
1- We purchase domain names from various registrars
2- The leading site is hosted on a VPS server (VPS is rapid hosting)
3- Other sites are on various hostings
4- We allocate a individual Google profile to each site with verification in Google Search Console.
5- We develop websites on WordPress, we don’t employ plugins with assistance from which Trojans penetrate and through which pages on your websites are generated.
6- We refrain from repeat templates and employ only exclusive text and pictures
We don’t work with website design; the client, if wished, can then edit the websites to suit his wishes
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
ラブドール アニメ キャラ 138cmアイアンテックリーズナブルラブドールDoll4Ever165cm –5ft5巨大な胸新しいWM人形168cm–5ft6ビスチェで覆われた大きなヒップの小さなおっぱい
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
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
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
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
hd porn videos download ggjgodherogg.8fMb7gOEkoG
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
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.
http://danceway74.ru/users/2244
https://legalsochi.ru/users/20
https://chervonogradcity.webtalk.ru/viewtopic.php?id=828#p3340