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

Luc*_*cas 2 dart flutter

我正在尝试使用我的 MaterialApp 主题中的 AppBarTheme 在我的应用程序中的任何地方实现透明的应用程序栏。但这导致文本大小成为默认值 14.0 而不是标题大小。

我想这与 TextStyle 继承有关,但我对此知之甚少。

示例代码:

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'),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

这可以通过指定来实现textTheme所述的内部AppBarTheme

事实上,AppBarTheme()有一个完全可定制的参数,它接受一个TextTheme。你几乎在你的问题中得到了它。

尝试:

  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.copyWith(
            title: theme.textTheme.title.copyWith(fontSize: 20.0),
          ),
          iconTheme: theme.iconTheme,
        ),
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('AppBar!'),
        ),
        body: Text('Some text'),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

textTheme有一个标题参数其内部可以设置fontSize的。标题的默认字体大小为 20.0。

请注意以下几行:

textTheme: theme.textTheme.copyWith(
                title: theme.textTheme.title.copyWith(fontSize: 20.0),
              ),
Run Code Online (Sandbox Code Playgroud)

您可以在此处阅读有关TextTheme 类的更多信息

每个参数都是可定制的,这显示了 Flutter 的强大功能。