admin管理员组文章数量:1594755
1. 下载字体文件(.ttf 或.otf )放到根目录下的foots文件夹下,如果没有自己创建。
给个字体下载的地址:http://www.diyiziti/Download/308
2. 在pubspec.yaml中定义
fonts:
- family: MaoTi # 字体别名
fonts:
- asset: fonts/mao_ti.ttf # 字体文件目录
weight: 700 # 权重 700表示粗体,相当于bold
- family: PingFang
fonts:
- asset: fonts/ping_fang.ttf
3. 全局配置— 就在主题theme中配置
MaterialApp(
theme: ThemeData(
fontFamily: "PingFang", // 统一指定应用的字体。
primarySwatch: Colors.green,
primaryColor: Colors.white),
。。。
4. 局部配置—指定TextStyle中的fontFamily
TextStyle(
fontFamily:"PingFang", // 指定该Text的字体。
fontSize: SizeUtil.getFontSize(Dimens.font_sp12),
color: CustomColors.colorPrimary,
fontWeight: FontWeight.bold)
5. 如果多处用到指定的字体,但又不是全局用到, 你可以定义一个公共的 textStyle .如:
var textFontStyle = TextStyle(
fontFamily:"PingFang", // 指定该Text的字体。
)
用到的地方用 .copyWith 这个方法, 如:
Text(
"显示我想要的字体",
style: textFontStyle.copyWith(
fontSize: 18.0,
color: Colors.red,
fontWeight: FontWeight.bold,
),
)
TextStyle的copyWith如下:
TextStyle copyWith({
bool inherit,
Color color,
Color backgroundColor,
String fontFamily,
List<String> fontFamilyFallback,
double fontSize,
FontWeight fontWeight,
FontStyle fontStyle,
double letterSpacing,
double wordSpacing,
TextBaseline textBaseline,
double height,
Locale locale,
Paint foreground,
Paint background,
List<ui.Shadow> shadows,
TextDecoration decoration,
Color decorationColor,
TextDecorationStyle decorationStyle,
double decorationThickness,
String debugLabel,
})
版权声明:本文标题:Flutter 指定字体(全局指定、局部指定) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1728202712a1149593.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论