项目需要跟随系统主题变化,修改一些自定义组件的样式,但百度一直没找到可以监听系统主题变化的方法,而 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();
};
}
http://amoxildelivery.pro/# medicine amoxicillin 500
Chelsea https://england.chelsea-ar.com is one of the most successful English football clubs of our time.
Juventus Football Club https://italy.juventus-ar.com is one of the most successful and decorated clubs in the history of Italian and world football.
price for amoxicillin 875 mg: buy amoxicillin 500mg online – buying amoxicillin online
The history of one of France’s https://france.paris-saint-germain-ar.com most famous football clubs, Paris Saint-Germain, began in 1970, when capitalist businessmen Henri Delaunay and Jean-Auguste Delbave founded the club in the Paris Saint-Germain-en-Laye area.
Very nice write-up. I certainly love this site. Keep it up!
Zinedine Zidane https://real-madrid.zinedine-zidane-ar.com the legendary French footballer, entered the annals of football history as a player and coach.
where to buy amoxicillin: medicine amoxicillin 500 – generic amoxicillin
Travis Scott https://astroworld.travis-scott-ar.com is one of the brightest stars in the modern hip-hop industry.
http://clomiddelivery.pro/# how can i get clomid without a prescription
https://doxycyclinedelivery.pro/# buy cheap doxycycline uk
http://doxycyclinedelivery.pro/# doxycycline over the counter usa
buy cipro without rx: buy cipro no rx – buy cipro without rx
I have to thank you for the efforts you’ve put in writing this site. I really hope to view the same high-grade blog posts from you later on as well. In truth, your creative writing abilities has motivated me to get my own site now 😉
order doxycycline without prescription: doxycycline prescription online – buy doxycycline no prescription
Howdy! This article could not be written any better! Reading through this post reminds me of my previous roommate! He constantly kept talking about this. I will send this post to him. Fairly certain he’s going to have a great read. I appreciate you for sharing!
https://clomiddelivery.pro/# how to buy cheap clomid tablets
http://ciprodelivery.pro/# buy cipro online without prescription
Paxlovid over the counter: paxlovid covid – paxlovid for sale
http://amoxildelivery.pro/# amoxicillin 500mg capsules price
http://doxycyclinedelivery.pro/# buy cheap doxycycline
I truly love your website.. Pleasant colors & theme. Did you create this web site yourself? Please reply back as I’m planning to create my own personal site and would like to know where you got this from or exactly what the theme is called. Cheers!
mexican drugstore online: mexican drugstore online – mexican pharmaceuticals online
I like reading an article that can make people think. Also, many thanks for allowing me to comment.
I see this topic in a new light now.seo 최적화
http://indiapharmast.com/# indianpharmacy com
http://foruspharma.com/# reputable mexican pharmacies online
buy prescription drugs from india: reputable indian online pharmacy – world pharmacy india
canada pharmacy: pharmacy wholesalers canada – certified canadian international pharmacy
reputable mexican pharmacies online: purple pharmacy mexico price list – purple pharmacy mexico price list
top 10 pharmacies in india: india pharmacy mail order – pharmacy website india
canadian pharmacy: best online canadian pharmacy – onlinepharmaciescanada com
You should take part in a contest for one of the most useful blogs on the net. I’m going to highly recommend this website!
https://canadapharmast.com/# canadapharmacyonline
canadian pharmacy cheap: canadapharmacyonline legit – vipps canadian pharmacy
canada pharmacy 24h reputable canadian online pharmacies northwest pharmacy canada
магазин аккаунтов вконтакте http://www.magazin-akkauntov.ru .
магазин ключей и аккаунтов магазин ключей и аккаунтов .
canada discount pharmacy: canada drugs online – canadian pharmacy store
mexico pharmacy mexican pharmaceuticals online buying from online mexican pharmacy
https://foruspharma.com/# mexico drug stores pharmacies
top 10 pharmacies in india: best india pharmacy – indian pharmacies safe
mexican mail order pharmacies buying prescription drugs in mexico online medicine in mexico pharmacies
озвучивание ozvuchivanie-pomeshhenij.ru .
озвучивание http://ozvuchivanie-pomeshhenij.ru/ .
canadian pharmacy: online pharmacy canada – online canadian pharmacy review
india online pharmacy: reputable indian online pharmacy – india online pharmacy
reputable canadian online pharmacy: canadian pharmacy meds – global pharmacy canada
best online pharmacy india: Online medicine order – online shopping pharmacy india
canada pharmacy world certified canadian international pharmacy canadian pharmacy king