修改AppBar
中状态栏颜色
在AppBar
控件中添加systemOverlayStyle
参数,SystemUiOverlayStyle
主要包含三个参数:
- (only for Android)
statusBarColor
:状态栏背景颜色;
- (only for Android)
statusBarIconBrightness
:状态栏字体和图标颜色,设置为Brightness.dark
时为黑色字体和图标,Brightness.light
时为白色字体和图标;
- (only for iOS)
statusBarBrightness
:状态栏字体和图标颜色,设置为Brightness.dark
时为字白色字体和图标,Brightness.light
时为黑色字体和图标;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| AppBar( systemOverlayStyle: SystemUiOverlayStyle( statusBarColor: Colors.green, statusBarIconBrightness: Brightness.dark, statusBarBrightness: Brightness.light, ), centerTitle: true, title: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Image.asset( 'assets/images/logo.png', scale: 12, ), const SizedBox( width: 10, ), const Text( 'Flutter', style: TextStyle(color: Colors.black), ), ], ), )
|
全局修改状态栏颜色
在MaterialApp
控件中指定theme
参数,可以设置全局主题,theme
参数类型为ThemeData
,其中再传入appBarTheme
参数用于指定标题栏主题。
AppBarTheme
中指定systemOverlayStyle
参数来设置状态栏颜色。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, appBarTheme: AppBarTheme( iconTheme: IconThemeData(color: Colors.black), color: Colors.deepPurpleAccent, foregroundColor: Colors.black, systemOverlayStyle: SystemUiOverlayStyle( statusBarColor: Colors.green, statusBarIconBrightness: Brightness.dark, statusBarBrightness: Brightness.light, ), ), ), home: ChangeStatusBarColorDemo(), );
|
没有AppBar
时修改状态栏颜色
当没有AppBar
控件又需要改变状态栏颜色时,可以使用AnnotatedRegion
控件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| @override Widget build(BuildContext context) { return AnnotatedRegion<SystemUiOverlayStyle>( value: SystemUiOverlayStyle.dark, child: Scaffold( body: Center( child: Column( children: [ SizedBox( height: 50, ), Container( width: 200, height: 200, color: Colors.red, ), ElevatedButton( onPressed: () {}, child: const Text( 'Elevated Button 1', style: TextStyle(fontSize: 24), ), ), ], ), ), ), ); }
|