项目需要跟随系统主题变化,修改一些自定义组件的样式,但百度一直没找到可以监听系统主题变化的方法,而 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();
};
}
The road to the Premier League https://english-championship.premier-league-ar.com begins long before a team gets promoted to the English Premier League for the first time
The golf https://arabic.golfclub-ar.com industry in the Arab world is growing rapidly, attracting players from all over the world.
can i order clomid tablets: get cheap clomid without rx – cost clomid without rx
I seriously love your website.. Very nice colors & theme. Did you create this website yourself? Please reply back as I’m planning to create my own blog and would love to find out where you got this from or exactly what the theme is named. Many thanks!
doxycycline 100 mg tablets: doxycycline 40 mg coupon – how to get doxycycline prescription
can you buy amoxicillin uk: amoxicillin 500mg capsules antibiotic – amoxicillin no prescription
http://ciprodelivery.pro/# buy cipro online canada
https://amoxildelivery.pro/# how to buy amoxicillin online
https://doxycyclinedelivery.pro/# where can i buy doxycycline
https://paxloviddelivery.pro/# paxlovid buy
buy ciprofloxacin over the counter: ciprofloxacin generic price – ciprofloxacin 500 mg tablet price
This website definitely has all the information and facts I needed concerning this subject and didn’t know who to ask.
лучшее порево лучшее порево .
лучшее порево онлайн http://besplatny-sex-online.ru/ .
I was able to find good advice from your blog posts.
where can i buy doxycycline online: doxycycline for dogs – doxycycline 10mg tablets
Manny Pacquiao https://filipino-boxer.manny-pacquiao-ar.com is one of the most prominent boxers in the history of the sport.
Mike Tyson https://american-boxer.mike-tyson-ar.com one of the most famous and influential boxers in history, was born on June 30, 1966 in Brooklyn, New York.
Muhammad Ali https://american-boxer.muhammad-ali-ar.com is perhaps one of the most famous and greatest athletes in the history of boxing.
car hire in Budva Montenegro car rental
Temp Mail Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
paxlovid for sale: paxlovid generic – paxlovid cost without insurance
http://ciprodelivery.pro/# buy generic ciprofloxacin
Pretty! This has been a really wonderful article. Many thanks for providing these details.
http://amoxildelivery.pro/# amoxicillin without rx
https://amoxildelivery.pro/# amoxicillin online without prescription
http://amoxildelivery.pro/# where can i buy amoxocillin
дизайн интерьера зала https://dizayn-interera-doma.ru
Michael Jordan https://chicago-bulls.michael-jordan-ar.com is one of the greatest basketball players of all time, whose career with the Chicago Bulls is legendary.
The Formula One World Championship https://world-circuit-racing-championship.formula-1-ar.com, known as the Formula Championship in motor racing, is the highest tier of professional motor racing.
After some difficult years in the late 2010s, Manchester United https://england.manchester-united-ar.com returned to greatness in English football by 2024.
I do trust all the ideas youve presented in your post They are really convincing and will definitely work Nonetheless the posts are too short for newbies May just you please lengthen them a bit from next time Thank you for the post
can i buy generic clomid now: where can i get generic clomid without prescription – where to buy generic clomid online
ciprofloxacin 500mg buy online: cipro 500mg best prices – ciprofloxacin mail online
лучшее порно видео онлайн бесплатно лучшее порно видео онлайн бесплатно .
самое лучшее бесплатное порно http://www.best-free-porno.ru .
Jennifer Lopez https://lets-get-loud.jenniferlopez-ar.com was born in 1969 in the Bronx, New York, to parents who were Puerto Rican immigrants.
Paxlovid buy online: paxlovid generic – paxlovid price
I enjoy looking through a post that can make men and women think. Also, many thanks for allowing me to comment.
https://doxycyclinedelivery.pro/# doxycycline 400 mg
When Taylor Swift https://shake-it-off.taylor-swift-ar.com released “Shake It Off” in 2014, she had no idea how much the song would impact her life and music career.
Priyanka Chopra https://baywatch.priyankachopra-ar.com is an Indian actress, singer, film producer and model who has achieved global success.
Liverpool https://england.liverpool-ar.com holds a special place in the history of football in England.
Автомобили Hongqi https://hongqi-krasnoyarsk.ru в наличии – официальный дилер Hongqi Красноярск
https://clomiddelivery.pro/# buy cheap clomid without prescription
After checking out a number of the blog posts on your site, I really like your way of blogging. I saved it to my bookmark website list and will be checking back soon. Take a look at my website as well and let me know how you feel.
http://amoxildelivery.pro/# can we buy amoxcillin 500mg on ebay without prescription
https://ciprodelivery.pro/# cipro ciprofloxacin
where buy clomid without dr prescription: how to buy cheap clomid online – where buy clomid without a prescription
Great article. I’m dealing with a few of these issues as well..