简体   繁体   English

在 Flutter(Dart) 中定义主题对象的好方法是什么?

[英]What's a good way of defining theme objects in Flutter(Dart)?

I know about the theme object "ThemeData" and i'm making use of that as well, but alas the need for extending it has arisen.我知道主题 object “ThemeData”,我也正在使用它,但可惜已经出现了扩展它的需要。

What I'm trying to accomplish is defining style sets to reference throughout my app.我想要完成的是定义样式集以在我的应用程序中引用。 For example i have this例如我有这个

child: Text(
  advisoryServiceStatus[item.status - 1],
  style: TextStyle(
      color: Color.fromRGBO(0, 0, 0, 0.6),
      fontSize: 12,
      fontWeight: FontWeight.w500),
),

and i want to move the TextStyle in a file so i can do something like我想将 TextStyle 移动到一个文件中,这样我就可以执行类似的操作

child: Text(
  advisoryServiceStatus[item.status - 1],
  style: extendedThemeConfig.textStyles.mutedText,

but i have troubled properly defining my style object.但我很难正确定义我的风格 object。 Here's what i tried.这是我尝试过的。 Maybe i shouldn't be using classes, but i haven't managed to define them as objects.也许我不应该使用类,但我还没有设法将它们定义为对象。 (my understanding of the concepts is a bit shabby) (我对概念的理解有点寒酸)

This is how i tried to define my extendedThemeConfig这就是我尝试定义我的扩展主题配置的方式

class TextStyles {
  final TextStyle mutedText = TextStyle(
      color: Color.fromRGBO(0, 0, 0, 0.6),
      fontSize: 12,
      fontWeight: FontWeight.w500);
}

class ExtendedThemeConfig {
  TextStyles textStyles;
}

const extendedThemeConfig = ExtendedThemeConfig;

Why your approach doesn't work为什么你的方法不起作用

It probably does work, but features like hot-reload aren't supported, because you introduce global state to your app, which is often not what you want.它可能确实有效,但不支持热重载等功能,因为您将全局 state 引入您的应用程序,这通常不是您想要的。

So, how to do it better?那么,如何做得更好呢?

I already answered a similar question here more elaborately, but here's a version adapted to your problem:我已经在这里更详细地回答了一个类似的问题,但这里有一个适合您的问题的版本:

Because Flutter is open source, we can just look at how the Theme is implemented and copy that code to create a custom widget that functions just like a Theme .因为 Flutter 是开源的,所以我们可以看看Theme是如何实现的,然后复制该代码来创建一个自定义小部件,其功能就像Theme一样。 Here's a boiled-down version:这是一个简化的版本:

@immutable
class MyThemeData {
  MyThemeData({
    this.mutedText,
  });

  final TextStyle mutedText;
}

class MyTheme extends StatelessWidget {
  MyTheme({
    Key key,
    @required this.data,
    @required this.child,
  }) : super(key: key);

  final MyThemeData data;
  final Widget child;

  static MyThemeData of(BuildContext context) {
    return (context.ancestorWidgetOfExactType(MyTheme) as MyTheme)?.data;
  }

  @override
  Widget build(BuildContext context) => child;
}

Now, you can just wrap the MaterialApp in a MyTheme widget:现在,您可以将MaterialApp包装在MyTheme小部件中:

MyTheme(
  data: MyThemeData(
    mutedText: ...,
  ),
  child: ... (here goes the MaterialApp)
)

Then anywhere in your app, you can write MyTheme.of(context).mutedText .然后在您的应用程序的任何地方,您都可以编写MyTheme.of(context).mutedText
You can adapt the MyThemeData class to your needs, storing anything you want.您可以根据需要调整MyThemeData class,存储您想要的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM