You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Flutter的RichText组件中引入Google Fonts字体

为Flutter的RichText添加Google Fonts字体样式

前置准备

确保项目已添加google_fonts依赖,在pubspec.yaml中补充:

dependencies:
  google_fonts: ^6.1.0 # 可替换为最新版本

修改后的完整代码

child: RichText(
  textAlign: TextAlign.start,
  text: TextSpan(
    text: "What are you looking for?",
    style: GoogleFonts.hammersmithOne().copyWith(
      color: Colors.black87,
      fontSize: 18.0,
      fontWeight: FontWeight.bold,
    ),
  ),
),

关键说明

  • 移除了原代码的const关键字,因为GoogleFonts.hammersmithOne()无法作为常量使用
  • 通过copyWith方法,在应用Google字体的同时保留原有文本样式属性,无需重复编写样式参数

内容的提问来源于stack exchange,提问作者user19908504

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 20:31:12