[英]How to change status bar icon and text color in flutter based on theme been set?
How to update color for status bar icon without any third party plugin?如何在没有任何第三方插件的情况下更新状态栏图标的颜色?
in my theme class I have a function in which i am trying below code but not results achieved as of yet:在我的主题 class 中,我有一个 function,我在其中尝试使用以下代码,但目前尚未取得结果:
CODE FOR THEME AS OF NOW:主题代码截至目前:
// custom light theme for app
static final customLightTheme = ThemeData.light().copyWith(
brightness: Brightness.light,
primaryColor: notWhite,
accentColor: Colors.amberAccent,
scaffoldBackgroundColor: notWhite,
primaryTextTheme: TextTheme(
title: TextStyle(
color: Colors.black
),
),
appBarTheme: AppBarTheme(
iconTheme: IconThemeData(color: Colors.black),
elevation: 0.0,
)
);
// custom dark theme for app
static final customDarkTheme = ThemeData.dark().copyWith(
brightness: Brightness.dark,
primaryColor: Colors.black,
accentColor: Colors.orange,
scaffoldBackgroundColor: Colors.black87,
primaryTextTheme: TextTheme(
title: TextStyle(
color: Colors.white
),
),
appBarTheme: AppBarTheme(
iconTheme: IconThemeData(color: Colors.white),
elevation: 0.0,
),
);
CODE FOR THEME CHANGER:主题转换器代码:
// set theme for the app
setTheme(ThemeData theme) {
_themeData = theme;
if(_themeData == ThemeChanger.customLightTheme){
SystemChrome.setSystemUIOverlayStyle(
const SystemUiOverlayStyle(
statusBarColor: Colors.white,
systemNavigationBarColor: Colors.white,
systemNavigationBarDividerColor: Colors.black,
systemNavigationBarIconBrightness: Brightness.dark,
),
);
} else {
SystemChrome.setSystemUIOverlayStyle(
const SystemUiOverlayStyle(
statusBarColor: Colors.blue,
systemNavigationBarColor: Colors.blue,
systemNavigationBarDividerColor: Colors.red,
systemNavigationBarIconBrightness: Brightness.light,
),
);
}
notifyListeners();
}
This is not what I want as I don't want the third party solution.这不是我想要的,因为我不想要第三方解决方案。
Icon's color in status bar (Flutter) 状态栏中图标的颜色(Flutter)
currently I am getting black icons in white / light theme and also black icons ( which should be white icons ) in dark / black theme on theme change.目前我在白色/浅色主题中得到黑色图标,在主题更改时在深色/黑色主题中得到黑色图标(应该是白色图标)。 rest is all working well. rest 一切正常。
You can set the status bar theme by calling the setSystemUIOverlayStyle
with required theme.您可以通过调用具有所需主题的setSystemUIOverlayStyle
来设置状态栏主题。
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);
or或者
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
UPDATE:更新:
You can specify your own properties like,您可以指定自己的属性,例如,
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.white
));
Check for available properties here . 在此处检查可用属性。
Note: For light
theme of app, use dark
overlay and vice versa.注意:对于应用的light
主题,使用dark
覆盖,反之亦然。 Also make sure you import 'package:flutter/services.dart';
还要确保你import 'package:flutter/services.dart';
Hope that helps!希望有帮助!
Note:笔记:
The accepted answer works if the set SystemUiOverlayStyle
not overwritten by other widgets like AppBar
.如果设置的SystemUiOverlayStyle
没有被AppBar
等其他小部件覆盖,则接受的答案有效。
If an AppBar
is used, try with,如果使用AppBar
,请尝试使用,
appBar: AppBar(
title: const Text('Title text'),
brightness: Brightness.dark,
),
For default light
or dark
themes.对于默认的light
或dark
主题。
This answer to changing theme did not work for me but I use this.这个改变主题的答案对我不起作用,但我使用了这个。 I know both are the same things but in this way, we can work with colour and Icons theme as well.我知道两者是一样的,但通过这种方式,我们也可以使用颜色和图标主题。
SystemChrome.setSystemUIOverlayStyle(
SystemUiOverlayStyle(
statusBarColor: Color(0xFFdde6e8),
statusBarIconBrightness: Brightness.dark),
);
To Change the status bar icon color (to white).更改状态栏图标颜色(为白色)。
Add this SystemChrome... to your void main() .将此SystemChrome...添加到您的void main() 。 Before this don't forget to import 'package:flutter/services.dart';
在此之前不要忘记import 'package:flutter/services.dart';
. .
void main() {
SystemChrome.setSystemUIOverlayStyle(
SystemUiOverlayStyle(
statusBarIconBrightness: Brightness.light,
),
);
runApp(MyApp());
}
After this, if you are using Scaffold()
for a screen/page, then in the AppBar() , add this line brightness: Brightness.dark,
like this,在此之后,如果您将Scaffold()
用于屏幕/页面,则在AppBar() 中,添加此行brightness: Brightness.dark,
如下所示,
return Scaffold(
appBar: AppBar(
brightness: Brightness.dark,
Then done.然后完成。
If you're using AppBar there's brightness
property.如果您使用的是 AppBar,则有brightness
属性。 You can set the brightness property to dark
so the icons of status bar will get white.您可以将亮度属性设置为dark
这样状态栏的图标就会变成白色。
AppBar(
brightness: Brightness.dark
)
If you are using the theme builder for entire app there is new way to change the brightness (icons color) to be white or black as follow如果您正在为整个应用程序使用主题构建器,则有一种新方法可以将亮度(图标颜色)更改为白色或黑色,如下所示
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My App',
debugShowCheckedModeBanner: false,
builder: (context, navigator) {
return Theme(
data: ThemeData(
primaryColor: AppColors.primary,
accentColor: AppColors.accent,
appBarTheme: AppBarTheme(systemOverlayStyle:
SystemUiOverlayStyle.light) ,//this is new way to change icons color of status bar
fontFamily: 'Poppins'
),
child: navigator,
);
},
);
}
}
Don't use AnnotatedRegion
or AppBar.brightness
as they are deprecated, use this instead:不要使用AnnotatedRegion
或AppBar.brightness
因为它们已被弃用,请改用:
Less customizations:更少的定制:
AppBar( systemOverlayStyle: SystemUiOverlayStyle.dark, // Both iOS and Android (dark icons) )
More customizations:更多定制:
AppBar( systemOverlayStyle: SystemUiOverlayStyle( statusBarBrightness: Brightness.light, // For iOS: (dark icons) statusBarIconBrightness: Brightness.dark, // For Android: (dark icons) statusBarColor: ..., ), )
I haven't found a way to set the color of the statusbaricons in particular but for me It worked to set the property systemStatusBarContrastEnforced
to true
.我还没有找到特别设置 statusbaricons 颜色的方法,但对我来说,它可以将属性systemStatusBarContrastEnforced
设置为true
。
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
systemStatusBarContrastEnforced: true,
));
I found that the best way to do this is via AppBar widget.我发现最好的方法是通过 AppBar 小部件。 If you are not using appbar on exact screen, you can achieve this with implementing dummy transparent appbar:如果您没有在确切的屏幕上使用 appbar,您可以通过实现虚拟透明 appbar 来实现:
Scaffold(
// critical to imitate appbar absence
extendBodyBehindAppBar: true,
appBar: AppBar(
// setup your style here
systemOverlayStyle: SystemUiOverlayStyle.light,
backgroundColor: Colors.transparent,
elevation: 0.0,
toolbarHeight: 0.0,
),
body: ...,
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.