如何在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
相关产品推荐
相关产品推荐

