Flutter MaterialApp中的主题在窗口小部件中不起作用

Flutter MaterialApp中的主题在窗口小部件中不起作用,flutter,flutter-web,Flutter,Flutter Web,这是我的代码,我已经明确定义了主题,如下所示。 但是,在Chrome浏览器中,家中容器的颜色仍然是蓝色 class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Spotify UI', debugShowCheckedModeBanner: false, the

这是我的代码,我已经明确定义了主题,如下所示。 但是,在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,
            ),
          )
        ],
      ),
    );
  }
}
MaterialApps主体尚未包含主体中包含主题的更新的BuildContext。 使用生成器包装列以提供更新的上下文:

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,
                ),
              )
            ],
          );
        }
      ),
    );
  }
}
MaterialApps主体尚未包含主体中包含主题的更新的BuildContext。 使用生成器包装列以提供更新的上下文:

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,
                ),
              )
            ],
          );
        }
      ),
    );
  }
}

这是因为容器小部件的颜色取自上下文,这是MyApp构建方法的一个参数

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

相反,您可以做的是:

类MyApp扩展了无状态小部件{ @凌驾 小部件构建上下文上下文{ 返回材料PP 标题:“颤振Spotify UI”, debugShowCheckedModeBanner:false, themeMode:themeMode.dark, 黑暗主题:主题数据 // ... , 主页:MyWidget, ; } } 类MyWidget扩展了无状态Widget{ @凌驾 小部件构建上下文上下文{ 返回列 儿童:[ 扩大 子:容器 颜色:Theme.ofcontext.primaryColor, , ], ; } }
这是因为容器小部件的颜色取自上下文,这是MyApp构建方法的一个参数

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

相反,您可以做的是:

类MyApp扩展了无状态小部件{ @凌驾 小部件构建上下文上下文{ 返回材料PP 标题:“颤振Spotify UI”, debugShowCheckedModeBanner:false, themeMode:themeMode.dark, 黑暗主题:主题数据 // ... , 主页:MyWidget, ; } } 类MyWidget扩展了无状态Widget{ @凌驾 小部件构建上下文上下文{ 返回列 儿童:[ 扩大 子:容器 颜色:Theme.ofcontext.primaryColor, , ], ; } }
我只是注意到已经有了答案。这个答案也很好。我只是注意到已经有答案了。这个答案也很好。谢谢你的回答,这也解决了问题。有趣的方法!感谢您的回答,这也解决了问题。有趣的方法!