flutter 为什么ThemeData的fontFamily不适用于AppBar、Tab等所有文本

dbf7pr2w  于 2023-04-22  发布在  Flutter
关注(0)|答案(2)|浏览(155)

我终于发现了。
我设置fontFamily如下。

@override
Widget build(BuildContext context) {
  return GetMaterialApp(
    theme: ThemeData(
      fontFamily: 'YourAwesomeFontFamily',
    ),
    ...,
  );
}

我还想更改AppBartextStyle,因此我将其设置如下。

child: Scaffold(
  appBar: AppBar(
    title: const Text(
      'Awesome Title'
      style: TextStyle(
        color: Colors.red,
        fontWeight: FontWeight.bold,
      ),
    ),
  ),
  ...,
),


AppBarfontFamily必须单独设置。

style: TextStyle(
  fontFamily: 'YourAwesomeFontFamily', // <- this must go in
  ...,
),

这是为什么呢?
找来找去,TabBar也是这样的吗?我设置labelStyle是因为我想让Tab的字体花哨,但是fontFamily不见了。

child: TabBar(
  labelStyles: TextStyle(
    fontFamily: 'YourAwesomeFontFamily', // <- this must go in
    color: Colors.red,
  ),
  ...,
),

但是什么呢?即使没有fontFamilyText小部件也会在ThemeData中设置fontFamily

Text(
  'Applied fontFamily',
  style(
    // here is no fontFamily
    color: Colors.red,
  ),
),

所以直到现在我才发现。
我现在很困惑
如果你能给予我点提示我会很感激的。

ego6inou

ego6inou1#

以下是您需要的提示:

return GetMaterialApp(
  theme: ThemeData(
    textTheme: TextTheme(
      // Setting fontFamily for the bodyText1 text style, that is used by default for the Text widget.
      bodyText1: TextStyle(
        fontFamily: 'YourAwesomeFontFamily',
      ),

      // Setting fontFamily for the bodyText1 text style, that is used by default for the AppBar title and also for TabBar label if you use DefaultTabController.
      headline6: TextStyle(
        fontFamily: 'YourAwesomeFontFamily',
      ),

      //Setting fontFamily for the bodyText1 text style, that is used by default for the TabBar label.
      subtitle1: TextStyle(
        fontFamily: 'YourAwesomeFontFamily',
      ),
    ),
    appBarTheme: AppBarTheme(
      // Setting fontFamily for the AppBar title text style.
      textTheme: TextTheme(
        headline6: TextStyle(
          fontFamily: 'YourAwesomeFontFamily',
        ),
      ),
    ),
    tabBarTheme: TabBarTheme(
      // Setting fontFamily for the TabBar label text style.
      labelStyle: TextStyle(
        fontFamily: 'YourAwesomeFontFamily',
      ),
    ),
  ),
  ...
);

快乐编码...

wecizke3

wecizke32#

我找到原因了。
Text小部件的TextStyle具有inherit属性。
如果inheritfalse,它将覆盖TextStyle,但默认值是true

text.dart

/// If non-null, the style to use for this text.
///
/// If the style's "inherit" property is true, the style will be merged with
/// the closest enclosing [DefaultTextStyle]. Otherwise, the style will
/// replace the closest enclosing [DefaultTextStyle].
final TextStyle? style;

但是AppBar的主题应用方式不同,它总是覆盖。

app_bar_theme.dart

/// Overrides the default value of [AppBar.titleTextStyle]
/// property in all descendant [AppBar] widgets.
///
/// If this property is specified, then [backwardsCompatibility]
/// should be false (the default).
///
/// See also:
///
///  * [toolbarTextStyle], which overrides the default of [AppBar.toolbarTextStyle]
///    in all descendant [AppBar] widgets.
final TextStyle? titleTextStyle;

相关问题