【问题标题】:Flutter navigation bar not change with themeFlutter 导航栏不随主题变化
【发布时间】:2020-07-16 22:59:21
【问题描述】:

当用户更改主题时,我尝试设置导航栏颜色更改,但它不起作用。我猜是因为我在定义之前使用了主题?所以我将 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(),
         
          },
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter colors navigation themes navbar


    【解决方案1】:

    我猜是因为我在定义之前使用了主题

    正是如此。尝试使用 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(),
                );
              }
            ),
             
              },
            ),
          ),
        );
      }
    }
    

    【讨论】:

    • 奇怪,更多反馈在这里:大多数时候你的代码工作导航栏确实会随着主题而改变,但有些时候不会。然后我重新启动设备,它再次工作。有什么想法吗?
    • 它最终在 Home() 中使用 AnnotatedRegion 将所有根小部件包装在所有 dart 文件中。代码看起来很难看,但很有效。不过一定是更好的方法......
    • 好吧,如果你推送另一个页面 AnnotatedRegion 将无法在那里工作,除非你也将它包装起来,它就像 SafeArea,任何新页面都需要它,否则它会落在外面
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    相关资源
    最近更新 更多