我正在尝试使用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'),
      ),
    );
  }
}

最佳答案

这可以通过在AppBarTheme中指定 textTheme 来实现。

实际上,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'),
      ),
    );
  }
}

文本主题具有title参数,您可以在其中设置 fontSize 。标题的默认 fontSize 是20.0。

注意以下几行:
textTheme: theme.textTheme.copyWith(
                title: theme.textTheme.title.copyWith(fontSize: 20.0),
              ),

您可以阅读有关TextTheme class here的更多信息

每个参数都是可自定义的,显示了Flutter的功能。

关于flutter - 使用AppBarTheme会导致默认文本大小吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58359425/

10-11 19:22