我为我的应用程序创建了以下主题:

ThemeData _buildDarkTheme() {
  final baseTheme = ThemeData(fontFamily: "Sunflower",);
  return baseTheme.copyWith(
      brightness: Brightness.dark,
      primaryColor: Colors.grey[800],
      accentColor: Colors.grey[850]);
}

然后,我将其应用于我的应用程序,如下所示:

class MyApp extends StatelessWidget {
  MyApp({Key key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        theme: _buildDarkTheme(),
        home: new Scaffold(
          appBar: _buildAppBar(),
          body: new Container(
            color: Theme.of(context).accentColor,
            height: double.infinity,
            child: new ListView.builder(...

但是,当我try 访问容器内(或其他任何地方)的强调色而不是预期的Colors.Grey[850]时,它默认为蓝色.此外,try 使用自定义字体向日葵字体系列不起作用,但当我改用

new Text("Hello World", style: new TextStyle(fontFamily: "Sunflower"))

字体显示正确.

我是新来的Flutter Flutter 和dart ,所以任何帮助解决这些问题将不胜感激.

推荐答案

这与contextTheme.of的工作方式有关.

来自主题类源代码:

  static ThemeData of(BuildContext context, { bool shadowThemeOnly = false }) {
    final _InheritedTheme inheritedTheme =
        context.inheritFromWidgetOfExactType(_InheritedTheme);
    if (shadowThemeOnly) {
      if (inheritedTheme == null || inheritedTheme.theme.isMaterialAppTheme)
        return null;
      return inheritedTheme.theme.data;
    }

    final ThemeData colorTheme = (inheritedTheme != null) ? inheritedTheme.theme.data : _kFallbackTheme;
    final MaterialLocalizations localizations = MaterialLocalizations.of(context);
    final TextTheme geometryTheme = localizations?.localTextGeometry ?? MaterialTextGeometry.englishLike;
    return ThemeData.localize(colorTheme, geometryTheme);
  }

Theme.of(和Navigator.of(),....例如,查看传递给它们的上下文,然后向上遍历小部件树,查找指定类型的小部件.

现在,看看你的代码

Widget build(BuildContext context) {
    return new MaterialApp(
        theme: _buildDarkTheme(),
        home: new Scaffold(
          appBar: _buildAppBar(),
          body: new Container(
            color: Theme.of(context).accentColor,

你可以看到,你传递到Theme.ofcontext实际上是你正在创建的主题之上的上下文.因此,它将找不到您的主题,并将恢复为默认设置.这是因为小部件树看起来有点像下面(忽略所有中间层,箭头指向您正在使用的上下文).

MyApp - context <--------
  MaterialApp
    Theme
      Scaffold
        

有两种方法可以解决这个问题;第一种是使用Builder类在主题下面具有上下文的闭包中构建小部件.它看起来就像这样:

class MyApp extends StatelessWidget {
  MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: _buildDarkTheme(),
      home: Scaffold(
        appBar: _buildAppBar(),
        body: Builder(
          builder: (context) => Container(
            color: Theme.of(context).accentColor,
            height: double.infinity,
            child: ListView.builder(...)
          ),
        ),
      ),
    );
  }
}

它会做出一棵看起来有点像这样的树:

MyApp - context
  MaterialApp
    Theme
      Scaffold
        Builder - context <---------

另一个(更好的)选项是将构建器的代码拆分为自己的类——StatelessWidget继承类或StatefulWidgetState对.

Dart相关问答推荐

flutter.io (dart) - 将填充设置为设备宽度的百分比?

是否从 Dart 中删除了interface关键字?

Flutter-ExpansionTile 在点击时展开和折叠

如何在 Dart 中设置文本框的值?

如何在 Flutter 中找出 URL 的有效性?

没有 AppBar 的 Flutter 应用设计

当文本在 Flutter 中溢出时如何使 Text 小部件像选取框一样

Flitter中的谷歌 map 没有出现

如何使用flatter删除Firestore文档中的字段

如何在 flutter 中对json进行字符串化

Dart 构造函数与静态方法;例如,为什么 int.parse() 不是工厂构造函数?

错误:default constructor is already defined

您可以将命名参数与简写构造函数参数结合起来吗?

Dart 中 Promise.all 的类比?

比较在 Dart 中创建单例的方法

Dart 中的with关键字

如何在 Dart 中为 Completer.CompleteException(exception, stackTrace) 获取当前堆栈跟踪;

Dart 中的全局变量

如何在 Dart 中获取时间戳?

如何在 Dart 中将 double 转换为 int?