flutter自定义文本主题变量

huangapple go评论65阅读模式
英文:

flutter custom textheme variables

问题

我想要在主题数据中使用自定义的文本主题,但是我创建的文本主题没有使用我自己的变量名称。如何使用我自己的变量名称创建 TextStyle?

final myTextTheme = TextTheme(
  appBarText: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold),
  xText: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
  myText: TextStyle(fontSize: 16.0),
);

ThemeData darkTheme = ThemeData(
  textTheme: myTextTheme
);

例如,我想要创建 appBarTextxTextmyText 等,而不是 displayLargedisplayMedium 等。

英文:

I am coding a dark and light theme. And I want to use custom textTheme inside themeDatas. I created the textthemes but not with my own variable names. How to create TextStyle with my own variable names.

ThemeData darkTheme = ThemeData(
  textTheme: myTextTheme
);



final myTextTheme = TextTheme(
  displayLarge: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold),
  displayMedium: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
  bodyLarge: TextStyle(fontSize: 16.0),
);

For example I want to create appBarText, xText, myText and etc intead of displayLarge, displayMedium and etc.

答案1

得分: 1

只要您使用Flutter的内置TextTheme,就无法在其中传递自定义参数。

然而,在处理这个问题时,有几种选择供您选择。

  1. 最常见和方便的方法是在相应元素的主题中提供文本样式。这样,Flutter会自动应用样式。
    例如,对于AppBar,相应的主题将是AppBarTheme

    final darkTheme = ThemeData(
       textTheme: myTextTheme,
       appBarTheme: myAppBarTheme,
     );
    
     const myTextTheme = TextTheme(
       displayLarge: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold),
       displayMedium: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
       bodyLarge: TextStyle(fontSize: 16.0),
     );
    
     const myAppBarTheme = AppBarTheme(
       titleTextStyle: TextStyle(fontSize: 36),
     );
    
  2. 第二种方法是使用自定义主题扩展。这比第一种方法更灵活,但您需要手动应用样式。这种方法在设计具有自定义样式的自定义小部件时非常有用。这里有一个扩展主题的良好指南:Medium

英文:

As long as you use Flutter's built-in TextTheme, you cannot pass custom parameters inside of it.

However, there are a few options for you to choose when dealing with this issue.

  1. The most common and convenient approach is to supply the text styles for an element at the respective element's theme. This way, the styles will be applied by Flutter automatically.
    For example, for AppBar the respective theme would be AppBarTheme:

    final darkTheme = ThemeData(
       textTheme: myTextTheme,
       appBarTheme: myAppBarTheme,
     );
    
     const myTextTheme = TextTheme(
       displayLarge: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold),
       displayMedium: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
       bodyLarge: TextStyle(fontSize: 16.0),
     );
    
     const myAppBarTheme = AppBarTheme(
       titleTextStyle: TextStyle(fontSize: 36),
     );
    
  2. The second approach is to use custom theme extensions. This is a more flexible way, than the first one, but you will need to apply the styles manually. This approach is useful when designing custom widgets with their styles. Here is a decent guide on extending themes: Medium.

huangapple
  • 本文由 发表于 2023年5月6日 17:28:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定