所以我在这里尝试获取当前主题,无论是浅色还是深色。所以我可以相应地改变小部件颜色..但是,它不起作用,我使用 if 语句来知道它何时是深色模式..但它总是 False ..这是代码..顺便说一句,它在深色和浅色主题之间切换。 ..但是当我尝试获取当前主题..即使主题更改为黑暗.. if 语句总是显示错误...
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
bool darkModeOn = MediaQuery.of(context).platformBrightness == Brightness.dark;
Color containerColor;
if (darkModeOn == true) {
containerColor = Colors.blueGrey;
print("----------------");
print("dark mode ON");
print("----------------");
} else {
containerColor = Colors.deepPurple;
print("LIGHT mode ON");
}
return Scaffold(
floatingActionButton: FloatingActionButton.extended(
onPressed: () {
//----switch theme---
currentTheme.switchTheme();
},
label: Text(
"Switch theme",
style: TextStyle(
),
),
icon: …Run Code Online (Sandbox Code Playgroud)