Flutter-自定义的ThemeData无法正常工作

时间:2020-06-12 10:16:23

标签: flutter fonts

我为我的应用创建了以下主题:

class MyPortfolio extends StatelessWidget {
@override
Widget build(BuildContext context) {
final myPortfolio = "my portfolio";

return MaterialApp(
  title: myPortfolio,
  theme: ThemeData(
    primaryColor: Color.fromRGBO(10, 16, 19, 100),
    //  accentColor:,
    fontFamily: 'Futura',
    textTheme: TextTheme(
      headline5: TextStyle(fontSize: 13),
  ),
  home: MyAppBar(),
);}}

然后按照以下步骤将其应用于我的应用程序:

class _MyAppBarState extends State<MyAppBar> {
@override
Widget build(BuildContext context) {
return MaterialApp(
  debugShowCheckedModeBanner: false,
  home: Scaffold(
    appBar: AppBar(
      backgroundColor: Colors.white,
      elevation: 0.0,
      title: Row(
        children: <Widget>[
          Logo(),
          // action button
          FlatButton(
              onPressed: () {
                _select(choices[0]);
              },
              child: Text(
                'Portfolio',
                style: Theme.of(context).textTheme.headline5,
              )),...

我的pubspec.yaml如下:

flutter:
 fonts:
  - family: Baskerville
    fonts:
      - asset: fonts/Baskerville.ttc
  - family: Futura
    fonts:
      - asset: fonts/Futura.ttc
  - family: Tinos
    fonts:
      - asset: fonts/Tinos-Italic.ttf
        style: italic
      - asset: fonts/Tinos-BoldItalic.ttf
        weight: 700
 uses-material-design: true

但是,当我尝试在此处访问该字体时,它显示的是默认的headline5字体样式,而不是我自定义的字体样式。 请问有人可以提供建议吗?

2 个答案:

答案 0 :(得分:0)

您正在_MyAppBarState中使用MaterialApp小部件的新实例,它正在使用默认参数。

class MyPortfolio extends StatelessWidget {
@override
Widget build(BuildContext context) {
final myPortfolio = "my portfolio";

return MaterialApp(
debugShowCheckedModeBanner: false,
  title: myPortfolio,
  theme: ThemeData(
    primaryColor: Color.fromRGBO(10, 16, 19, 100),
    //  accentColor:,
    fontFamily: 'Futura',
    textTheme: TextTheme(
      headline5: TextStyle(fontSize: 13),
  ),
  home: MyAppBar(),
);}}

class _MyAppBarState extends State<MyAppBar> {
@override
Widget build(BuildContext context) {
return Scaffold(
    appBar: AppBar(
      backgroundColor: Colors.white,
      elevation: 0.0,
      title: Row(
        children: <Widget>[
          Logo(),
          // action button
          FlatButton(
              onPressed: () {
                _select(choices[0]);
              },
              child: Text(
                'Portfolio',
                style: Theme.of(context).textTheme.headline5,
              )),...

答案 1 :(得分:0)

我的问题也解决了,因为我也愚蠢地在应用程序的其他部分中使用了MaterialApps,因此ThemeData无法正常运行。