Flutter 使用AppBarTheme会导致默认文本大小吗?

Flutter 使用AppBarTheme会导致默认文本大小吗?,flutter,dart,Flutter,Dart,我正在尝试使用MaterialApp主题中的AppBarTheme,在我的应用程序中随处可见透明的AppBar。但这会导致文本大小默认为14.0,而不是标题大小 我想这与文本样式继承有关,但我对此了解不多 示例代码: class ExampleScreen extends StatelessWidget { @override Widget build(BuildContext context) { ThemeData theme = ThemeData(); retur

我正在尝试使用MaterialApp主题中的AppBarTheme,在我的应用程序中随处可见透明的AppBar。但这会导致文本大小默认为14.0,而不是标题大小

我想这与文本样式继承有关,但我对此了解不多

示例代码:

class ExampleScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    ThemeData theme = ThemeData();
    return MaterialApp(
      theme: theme.copyWith(
        appBarTheme: AppBarTheme(
          color: Colors.transparent,
          brightness: Brightness.light,
          elevation: 0,
          //I want the defaults, which is why I'm copying an 'empty' ThemeData
          //perhaps there's a better way to do this?
          textTheme: theme.textTheme,
          iconTheme: theme.iconTheme,
        ),
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('AppBar!'),
        ),
        body: Text('Some text'),
      ),
    );
  }
}

这可以通过在AppBarTheme内部指定文本主题来实现

实际上,AppBarTheme()有一个完全可定制的参数,它采用文本主题。你的问题里差点就提到了

尝试:

textTheme有一个title参数,您可以在其中设置fontSize。标题的默认fontSize为20.0

请注意以下几行:

textTheme: theme.textTheme.copyWith(
                title: theme.textTheme.title.copyWith(fontSize: 20.0),
              ),
你可以阅读更多关于

每个参数都可以自定义,显示颤振的威力。

使用了


您可以像这样使用(上下文)的
Theme.of

@override
Widget build(BuildContext context) {
  return MaterialApp(
    theme: Theme.of(context).copyWith(
      appBarTheme: AppBarTheme(
        color: Colors.transparent,
        brightness: Brightness.light,
        elevation: 0,
        textTheme: Theme.of(context).textTheme,
        iconTheme: Theme.of(context).iconTheme,
      ),
    ),
    home: Scaffold(
      appBar: AppBar(
        title: Text('AppBar!'),
      ),
      body: Text('Some text'),
    ),
  );
}

我找到了解决办法。MaterialApp->builder函数

小部件构建(构建上下文){
返回材料PP(
生成器:(上下文,小部件){
var baseTheme=Theme.of(上下文);
返回主题(
数据:baseTheme.copyWith(
appBarTheme:appBarTheme(颜色:颜色。透明),
),
子组件(widget);
},
主页:HomeView());
}

谢谢,这是一个很好的解决方法,但我试图避免显式设置字体,因为如果我以后更改标题大小,我不想在两个位置设置它。显示在main.dart文件中为整个应用程序定义标题文本主题?请建议。谢谢。你找到解决方法了吗?看起来这是一个与此相关的bug:如果它也影响到你,就给它+1的反应!如何在main.dart文件中为整个应用程序定义标题文本主题?请建议。Thankshow是否在main.dart文件中为整个应用程序定义标题文本主题?请建议。谢谢
class ExampleScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    ThemeData theme = ThemeData();
    var localizedTheme = ThemeData.localize(theme, theme.typography.geometryThemeFor(ScriptCategory.englishLike));
    theme = theme.copyWith(
      appBarTheme: theme.appBarTheme.copyWith(
        color: Colors.transparent,
        brightness: Brightness.light,
        elevation: 0,
        textTheme: localizedTheme.textTheme,
      ),
    );
    return MaterialApp(
      theme: theme,
      home: Scaffold(
        appBar: AppBar(
          title: Text('AppBar!'),
        ),
        body: Text('Some text'),
      ),
    );
  }
}
@override
Widget build(BuildContext context) {
  return MaterialApp(
    theme: Theme.of(context).copyWith(
      appBarTheme: AppBarTheme(
        color: Colors.transparent,
        brightness: Brightness.light,
        elevation: 0,
        textTheme: Theme.of(context).textTheme,
        iconTheme: Theme.of(context).iconTheme,
      ),
    ),
    home: Scaffold(
      appBar: AppBar(
        title: Text('AppBar!'),
      ),
      body: Text('Some text'),
    ),
  );
}