简体   繁体   English

"如何在 main.dart 中添加字体系列"

[英]how to add font family into in main.dart flutter

I'm new to flutter and struggling to add font family in my code..already updated my pubspec.yamal with OpenSans fonts.. now how to add font family into this main.我是新来的颤振和努力在我的代码中添加字体系列..已经用 OpenSans 字体更新了我的 pubspec.yamal.. 现在如何将字体系列添加到这个主目录中。 dart material app widget??飞镖材料应用小部件??

fontFamily: "OpenSans", fontFamily: "OpenSans",

<\/blockquote>

 Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, theme: theme.copyWith( colorScheme: theme.colorScheme.copyWith(primary: Color(0xff075e54),secondary: Color(0xff128C7E), ), ), home: Homescreen(key: null), ); } }<\/code><\/pre>"

You can try this你可以试试这个

theme: theme.copyWith(
        colorScheme: theme.colorScheme.copyWith(primary: Color(0xff075e54),secondary: Color(0xff128C7E)),
        textTheme: theme.textTheme.apply(fontFamily: "OpenSans"),
),

You can create a style file in your dart folder and use the font where ever you want like this way- text_styles.dart你可以在你的 dart 文件夹中创建一个样式文件,然后像这样在任何你想要的地方使用字体 - text_styles.dart

class TextStyles {
  static TextStyle overline(
      {required BuildContext context, required Color color}) {
    return GoogleFonts.montserrat(
      textStyle: Theme.of(context).textTheme.overline?.copyWith(color: color),
    );
  }
}

and use it as-并将其用作-

TextStyles.overline(context: context, color: theme.textColor).copyWith(fontWeight: FontWeight.w900),

Note:if you use custom font you have to install the package from your pubspec.yaml.Example-注意:如果您使用自定义字体,则必须从 pubspec.yaml.Example- 安装包

google_fonts:

Create a simple file for the theme like the one I created theme_util.dart<\/strong> and use it as follows.为主题创建一个简单的文件,就像我创建的theme_util.dart<\/strong>一样,并按如下方式使用它。

return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: appTheme,
      home: Homescreen(key: null),
 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM