【问题标题】:how to change the color of status bar in flutter when we are using dark/light theme?当我们使用深色/浅色主题时,如何更改状态栏的颜色?
【发布时间】:2021-05-24 04:10:10
【问题描述】:

[重复]。我正在尝试通过在 main 中使用以下代码在整个应用程序中更改状态栏的颜色,但是当主题为深色时,我无法将其设置为不同的颜色。现在我已将其设置为白色,但当它是深色主题时我想要黑色。这是一个重复的问题,但我无法得到它,因为其中一些正在使用一些已停产的软件包,而在某些情况下,他们正在使用 Appbar 的每个屏幕中都这样做。能够以较早的答案执行此操作,但如何在主题更改时更改它。

我也试过这个:- statusBareColor:themeProvider.isDarkMode? black :white,但这显示错误。

 class MyApp extends StatelessWidget {
 @override
Widget build(BuildContext context) => ChangeNotifierProvider(

create:(context) => ThemeProvider(),
builder:(context,_){
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
    statusBarColor:white,
  ));
  final themeProvider = Provider.of<ThemeProvider>(context,listen: true);
  return MaterialApp(

    debugShowCheckedModeBanner: false,
    themeMode: themeProvider.thememode,
    theme: MyThemes.lightTheme,
    darkTheme: MyThemes.darkTheme,
    );

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您可以使用AppBar 小部件的Brightness 属性

    Brightness.light
    Brightness.dark
    

    在您的 ThemeData 集中 appBarTheme 如下

    MaterialApp(
       theme: ThemeData( 
          appBarTheme: AppBarTheme(
            brightness: Brightness.dark, // set dark of light
          ),
        ),
      ),
    

    您可以在任何状态管理提供者的帮助下更改brightness

    或者你可以使用类似的东西,把它放在你的build方法中

    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
    

    【讨论】:

    • 我只使用了你下面提到的这个,但我的问题。当它是深色主题时如何更改它?我想在 main 中执行它,以便它在整个应用程序中执行。
    • 我已经编辑了我的问题,请看一下
    • 这是改变底部导航背景的颜色,和状态栏没有区别
    • 请检查最后一个答案。我已经编辑了。
    • 只有在切换到深色主题时才会保持亮光
    【解决方案2】:

    我认为你必须在 Build 函数和 ChangeNotifierProvider 之外使用此代码,请检查它。

    class MyApp extends StatelessWidget {
     @override
    Widget build(BuildContext context) { 
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
        statusBarColor:white,
      ));
      return ChangeNotifierProvider(
    
    create:(context) => ThemeProvider(),
    builder:(context,_){
      
      final themeProvider = Provider.of<ThemeProvider>(context,listen: true);
      return MaterialApp(
    
        debugShowCheckedModeBanner: false,
        themeMode: themeProvider.thememode,
        theme: MyThemes.lightTheme,
        darkTheme: MyThemes.darkTheme,
        )};
    

    【讨论】:

    • 你能用 stateFullWidget 告诉我结果吗?
    • 还是一样的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-09
    • 2021-11-29
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 2022-08-12
    相关资源
    最近更新 更多