项目需要跟随系统主题变化,修改一些自定义组件的样式,但百度一直没找到可以监听系统主题变化的方法,而 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();
};
}
تم تصميم تركيبات المصنع من البولي فينيل كلوريد (uPVC) مع الاهتمام الدقيق بالتفاصيل ، مما يضمن اتصالات آمنة ومحكمة تقلل من مخاطر التسرب أو الأعطال. إيليت بايب Elite Pipe
1993; 8 1835 1836 will propecia work on hairline Though, I believe that the highest level of masculinity can only be achieved by daily routine and ritual
Eli should have seen the corner levitra se vende en farmacias
Well, inside, the Ford Escort GT was fairly fun too.
Thee first-ever Ford Escort was laujched in 1986, the Ford Escort MK1, which offered
drivers one hundred ten mph. Drivers may take pleasure in air controls, a cassette player and a radio.
“We heard he was air lifted, and we heard he had passed. Fortunately the plane landed safely in Tel Aviv about four hours after the unique name was made to air site visitors management about a bomb, and just below 10 hours after departing New York. While flying over Spain, the EL AL pilots knowledgeable air traffic control concerning the bomb risk, which is when the state of affairs was escalated. Using the SM300, shoreside operators had remote situational consciousness and had been able to exhibit the capabilities of remote command and control of the vessel, her operating techniques and flight deck in a reside marine setting. With many people evaluating the 34-yr-old pop star to a personality from “The Hunger Games,
” it turns out the oversized Schiaparelli hute couture gown she wore wwas the rationale she wanted a Marine to accompany her in the primary place. Onee of the extra notable moments from Inauguration Day was Lady Gaga’s hiughly effecttive rendition of tthe “The
Star-Spangled Banner.” But it surely was her outfit and hher Marine escort that aldo went viral that day.
my blog … דירה דיסקרטית ברחובות
Primary SjГ¶gren syndrome clinical and immunologic disease patterns in a cohort of 400 patients generic levitra canada pharmacy 36 Recruitment maneuvers attempt to increase the amount of aerated lung tissue to improve gas exchange
The microarray data indicated that both PDGFRО± and PDGFRОІ were upregulated in the IGF IR independent, recurrent tumors compared to the IGF IR dependent, primary tumors 14 buying cialis online forum
The very next time I read a blog, I hope that it doesnt disappoint me just as much as this one. After all, I know it was my choice to read, however I truly believed you would have something interesting to talk about. All I hear is a bunch of crying about something you can fix if you werent too busy seeking attention.
Of can caffeine lower blood sugar course, there are basically all newly ascended monks buy generic cialis online safely Can we take ovasitol while being on birth control
lasix order online 781 and Rerank score 135
猫可以多久不吃东西?
@猫可以多久不吃东西? buying cialis online forum In addition, there was no significant difference in the duration from the start of single administration of TAM to the initial detection of a high concentration of estradiol between both chemotherapy treated and non treated groups
@猫可以多久不吃东西? medicamento levitra precio 2008; 81 1 13 18
@猫可以多久不吃东西? You might merely observe that your dog seems less active than usual and is breathing harder than would be expected brand viagra For apoptosis, RTK feedback regulation, or cell cycle arrest assay, PANC 1 or KPC were cultured on six well plates 5 10 5 cells per well and treated with a serial titration of TH Z835 at 0, 1, 3, 6, 12, 24, 48, and 72 h for apoptosis or at 3, 12, and 24 h for RTK feedback regulation and cell cycle arrest assay