【问题标题】:Theme in MaterialApp not working in widgets FlutterMaterialApp 中的主题在小部件 Flutter 中不起作用
【发布时间】:2021-07-29 01:16:37
【问题描述】:

这是我的代码,我已经清楚地定义了主题,如下所示。 但是,home 中容器的颜色在 Chrome 浏览器中仍然显示为蓝色。

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Spotify UI',
      debugShowCheckedModeBanner: false,
      themeMode: ThemeMode.dark,
      darkTheme: ThemeData(
        brightness: Brightness.dark,
        appBarTheme: const AppBarTheme(backgroundColor: Colors.black),
        scaffoldBackgroundColor: const Color(0xFF121212),
        backgroundColor: const Color(0xFF121212),
        primaryColor: Colors.black,
        accentColor: const Color(0xFF1DB954),
        iconTheme: const IconThemeData().copyWith(color: Colors.white),
        fontFamily: 'Montserrat',
        textTheme: TextTheme(
          headline2: const TextStyle(
            color: Colors.white,
            fontSize: 32.0,
            fontWeight: FontWeight.bold,
          ),
          headline4: TextStyle(
            fontSize: 12.0,
            color: Colors.grey[300],
            fontWeight: FontWeight.w500,
            letterSpacing: 2.0,
          ),
          bodyText1: TextStyle(
            color: Colors.grey[300],
            fontSize: 14.0,
            fontWeight: FontWeight.w600,
            letterSpacing: 1.0,
          ),
          bodyText2: TextStyle(
            color: Colors.grey[300],
            letterSpacing: 1.0,
          ),
        ),
      ),
      home: Column(
        children: [
          Expanded(
            child: Container(
              color: Theme.of(context).primaryColor,
            ),
          )
        ],
      ),
    );
  }
}

【问题讨论】:

    标签: flutter flutter-web


    【解决方案1】:

    这是因为您的Container 小部件的颜色取自context,这是MyAppbuild 方法的参数。

    MaterialApp 有主题,但MyApp 没有主题。因此,当您使用 MyAppcontext 获取原色时,该上下文还没有任何主题,您将获得默认的原色。

    相反,你可以做的是:

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Spotify UI',
          debugShowCheckedModeBanner: false,
          themeMode: ThemeMode.dark,
          darkTheme: ThemeData(
            // ...
          ),
          home: MyWidget(),
        );
      }
    }
    
    class MyWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Column(
          children: [
            Expanded(
              child: Container(
                color: Theme.of(context).primaryColor,
              ),
            )
          ],
        );
      }
    }
    

    【讨论】:

    • 我刚刚注意到已经有答案了。这个答案也很好。
    【解决方案2】:

    您的MaterialApps 正文尚未包含更新后的BuildContext 正文中的主题。 用Builder 包裹你的Column 以提供更新的上下文:

    import 'package:flutter/material.dart';
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Spotify UI',
          debugShowCheckedModeBanner: false,
          themeMode: ThemeMode.dark,
          darkTheme: ThemeData(
            brightness: Brightness.dark,
            appBarTheme: const AppBarTheme(backgroundColor: Colors.black),
            scaffoldBackgroundColor: const Color(0xFF121212),
            backgroundColor: const Color(0xFF121212),
            primaryColor: Colors.black,
            accentColor: const Color(0xFF1DB954),
            iconTheme: const IconThemeData().copyWith(color: Colors.white),
            fontFamily: 'Montserrat',
            textTheme: TextTheme(
              headline2: const TextStyle(
                color: Colors.white,
                fontSize: 32.0,
                fontWeight: FontWeight.bold,
              ),
              headline4: TextStyle(
                fontSize: 12.0,
                color: Colors.grey[300],
                fontWeight: FontWeight.w500,
                letterSpacing: 2.0,
              ),
              bodyText1: TextStyle(
                color: Colors.grey[300],
                fontSize: 14.0,
                fontWeight: FontWeight.w600,
                letterSpacing: 1.0,
              ),
              bodyText2: TextStyle(
                color: Colors.grey[300],
                letterSpacing: 1.0,
              ),
            ),
          ),
          home: Builder( // <- Here
            builder: (context) {
              return Column(
                children: [
                  Expanded(
                    child: Container(
                      color: Theme.of(context).primaryColor,
                    ),
                  )
                ],
              );
            }
          ),
        );
      }
    }
    

    【讨论】:

    • 感谢您的回答,这也解决了问题。有趣的方法!
    猜你喜欢
    • 2020-02-28
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 2018-12-25
    • 1970-01-01
    相关资源
    最近更新 更多