Flutter 导航栏不随主题变化
Flutter navigation bar not change with theme
我尝试在用户更改主题时设置导航栏颜色更改,但它不起作用。我想这是因为我在定义之前使用了主题?所以我将 systemNavigationBarColor:Theme.of(context).accentColor 放入 home() 但仍然没有用。如果我删除代码,在某些设备的导航栏中是深色的,而另一些则是浅色的,与我设置的主题无关。是什么原因请帮帮我
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
systemNavigationBarColor:Theme.of(context).accentColor,
));
return StreamProvider<User>.value(
value: AuthService().user,
child: GestureDetector(
onTap: (){
FocusScope.of(context).requestFocus(FocusNode());
},
child: MaterialApp(
theme: ThemeData(
brightness: Brightness.light,
primaryColor: Colors.white,
accentColor: Colors.grey[300],
disabledColor: Colors.grey[400],
),
darkTheme: ThemeData(
brightness: Brightness.dark,
primaryColor: Colors.black,
accentColor: Colors.grey[800],
disabledColor: Colors.grey[700],
),
home: Home(),
},
),
),
);
}
}
I guess it is because I use theme before it is defined
正是这样。尝试使用 AnnotatedRegion
import 'package:flutter/services.dart';
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return StreamProvider<User>.value(
value: AuthService().user,
child: GestureDetector(
onTap: (){
FocusScope.of(context).requestFocus(FocusNode());
},
child: MaterialApp(
theme: ThemeData(
brightness: Brightness.light,
primaryColor: Colors.white,
accentColor: Colors.grey[300],
disabledColor: Colors.grey[400],
),
darkTheme: ThemeData(
brightness: Brightness.dark,
primaryColor: Colors.black,
accentColor: Colors.grey[800],
disabledColor: Colors.grey[700],
),
home: Builder( //so you can use the context down the tree for Theme.of(context)
builder: (context) {
return AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle(
statusBarColor: Theme.of(context).accentColor,
),
child: Home(),
);
}
),
},
),
),
);
}
}
我尝试在用户更改主题时设置导航栏颜色更改,但它不起作用。我想这是因为我在定义之前使用了主题?所以我将 systemNavigationBarColor:Theme.of(context).accentColor 放入 home() 但仍然没有用。如果我删除代码,在某些设备的导航栏中是深色的,而另一些则是浅色的,与我设置的主题无关。是什么原因请帮帮我
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
systemNavigationBarColor:Theme.of(context).accentColor,
));
return StreamProvider<User>.value(
value: AuthService().user,
child: GestureDetector(
onTap: (){
FocusScope.of(context).requestFocus(FocusNode());
},
child: MaterialApp(
theme: ThemeData(
brightness: Brightness.light,
primaryColor: Colors.white,
accentColor: Colors.grey[300],
disabledColor: Colors.grey[400],
),
darkTheme: ThemeData(
brightness: Brightness.dark,
primaryColor: Colors.black,
accentColor: Colors.grey[800],
disabledColor: Colors.grey[700],
),
home: Home(),
},
),
),
);
}
}
I guess it is because I use theme before it is defined
正是这样。尝试使用 AnnotatedRegion
import 'package:flutter/services.dart';
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return StreamProvider<User>.value(
value: AuthService().user,
child: GestureDetector(
onTap: (){
FocusScope.of(context).requestFocus(FocusNode());
},
child: MaterialApp(
theme: ThemeData(
brightness: Brightness.light,
primaryColor: Colors.white,
accentColor: Colors.grey[300],
disabledColor: Colors.grey[400],
),
darkTheme: ThemeData(
brightness: Brightness.dark,
primaryColor: Colors.black,
accentColor: Colors.grey[800],
disabledColor: Colors.grey[700],
),
home: Builder( //so you can use the context down the tree for Theme.of(context)
builder: (context) {
return AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle(
statusBarColor: Theme.of(context).accentColor,
),
child: Home(),
);
}
),
},
),
),
);
}
}