如何使颜色从我的setSystemUIOverlayStyle依赖于当前的主题模式?我试着这样做:
bool isDarkModeOn(BuildContext context){
final ThemeData mode = Theme.of(context);
var whichMode=mode.brightness;
if(whichMode == Brightness.dark){
return true;
}else{
return false;
}
}
(检查暗模式是否启用-功能工作正常)
@override
Widget build(BuildContext context) {
final systemThemeLight = SystemUiOverlayStyle.light.copyWith(
systemNavigationBarColor: const Color(0xdfd6f5ca),
systemNavigationBarIconBrightness: Brightness.light,
);
final systemThemeDark = SystemUiOverlayStyle.dark.copyWith(
systemNavigationBarColor: Colors.black,
systemNavigationBarIconBrightness: Brightness.light,
);
SystemChrome.setSystemUIOverlayStyle( isDarkModeOn(context) ? systemThemeDark : systemThemeLight);
这个想法是有效的,但只有当我通过导航到另一个屏幕来更新状态时,而不是通过在我的应用程序设置中切换开关。有人有更好的主意吗?
我终于自己找到了解决这个问题的方法,也许对别人有帮助:
将脚手架包裹在底部导航栏的上方:[…]):
return AnnotatedRegion<SystemUiOverlayStyle>(
value: _currentStyle,
child: Scaffold(
Widget构建函数下面:
bool isDarkMode = Theme.of(context).brightness == Brightness.dark;
_changeColor(isDarkMode);
和内部Statefull widget:
SystemUiOverlayStyle _currentStyle = SystemUiOverlayStyle.dark;
void _changeColor(bool isDarkMode) {
setState(() {
_currentStyle = SystemUiOverlayStyle.dark.copyWith(
systemNavigationBarColor: isDarkMode ? const Color(0xff272727) : Colors.grey[200]
);
});
}
然后自动更改....