flutter 字体演示

flutter 字体演示

目录

示意图:

字体文件下载地址:https:fonts.google

1.首先设置 pubspec.yaml 文件:C:\Users\user\StudioProjects\myflutter\pubspec.yaml

2.入口程序:C:\Users\user\StudioProjects\myflutter\lib\main.dart

3.局部字体的代码在每个组件中也是只有一条:fontFamily: 'Zhi_Mang_Xing',

执行效果如下:

字体文件的具体位置:


示意图:

字体文件下载地址:

1.首先设置 pubspec.yaml 文件:C:\Users\user\StudioProjects\myflutter\pubspec.yaml

# To add assets to your application, add an assets section, like this:# assets:#   - images/a_dot_burr.jpeg#   - images/a_dot_ham.jpeg# An image asset can refer to one or more resolution-specific "variants", see# /assets-and-images/#resolution-aware.# For details regarding adding assets from package dependencies, see# /assets-and-images/#from-packages# To add custom fonts to your application, add a fonts section here,# in this "flutter" section. Each entry in this list should have a# "family" key with the font family name, and a "fonts" key with a# list giving the asset and other descriptors for the font. For# example:# fonts:#   - family: Schyler#     fonts:#       - asset: fonts/Schyler-Regular.ttf#       - asset: fonts/Schyler-Italic.ttf#         style: italic#   - family: Trajan Pro#     fonts:#       - asset: fonts/TrajanPro.ttf#       - asset: fonts/TrajanPro_Bold.ttf#         weight: 700## For details regarding fonts from package dependencies,# see /custom-fonts/#from-packagesfonts:- family: Zhi_Mang_Xingfonts:- asset: fonts/Zhi_Mang_Xing/ZhimangXing-Regular.ttfstyle: normalweight: 700  # 100 的整数

2.入口程序:C:\Users\user\StudioProjects\myflutter\lib\main.dart

  主要代码只有一条:fontFamily: 'Zhi_Mang_Xing', 其他都是浮云!!

  看一下代码在下面的具体位置:

import 'package:flutter/material.dart';
import 'package:myflutter/basic/text.dart';String mytitle = '首页';void main(List<String> args) {return runApp(const MyApp());
}class MyApp extends StatelessWidget {const MyApp({Key? key}) : super(key: key);@overrideWidget build(BuildContext context) {// 1.首先,程序进入 flutter 的顶级组件return MaterialApp(title: "hello myflatter", // 应用在任务管理器中的标题;// 应用程序的全局主题样式theme: ThemeData(primarySwatch: Colors.blueGrey,fontFamily: 'Zhi_Mang_Xing', // **** 这是今天的主角,全局字体设置!!!!),home: my_flutter(title: mytitle), // 应用程序的主内容debugShowCheckedModeBanner: false // 应用是否显示主上角调试标记);}
}// ignore: camel_case_types
class my_flutter extends StatelessWidget {const my_flutter({Key? key, required this.title}) : super(key: key);final String title;@overrideWidget build(BuildContext context) {// 2.其次,程序进入 flutter 的脚手架组件return Scaffold(// 应用程序的头部组件appBar: AppBar(// 应用程序的头部中间标题title: Text(title),// leading 是主上角的图标leading: IconButton(onPressed: () {print('This is home!');},icon: const Icon(Icons.view_headline),),// 应用程序的头部右边图标组(多个图标)actions: [// 图标1IconButton(onPressed: () {print('This is share!');},icon: const Icon(Icons.share),),// 图标2Padding(padding: const EdgeInsets.symmetric(horizontal: 0),child: IconButton(icon: const Icon(Icons.search),onPressed: () {print('This is search!');},),),// 图标3IconButton(onPressed: () {print('This is more!');},icon: const Icon(Icons.more_vert),)],),// 3.这是整个应用程序内容的主体组件入口!!body: const TextDemo(),);}
}

3.局部字体的代码在每个组件中也是只有一条:fontFamily: 'Zhi_Mang_Xing', 其他都是浮云!

  RichText 多文本组件里面没有此代码,就不会设置字体,全局设置字体无效(是不是版本的问题?),具体看一下代码的位置:

import 'package:flutter/material.dart';/// Text
///     TestDirection(文本方向)
///
///     TextStyle(文本样式)
///         Colors(文本颜色)
///         FontWeight(字体粗细)
///         FontStyle(字体样式)
///
///     TextAlign(文本对齐)
///     TextOverflow(文本溢出)
///     maxLines(指定显示的行数)
///
/// RichText 与 TextSpan(给一段文本声明不同的多个样式)
///class TextDemo extends StatelessWidget {const TextDemo({Key? key}) : super(key: key);@overrideWidget build(BuildContext context) {/// Column 是例组件,可以传入多个内容,/// 传多个内容的时候用 children,child 只能传入一个内容。return Column(children: [const Text("Flutter 为应用开发带来了革新: 只要一套代码库,即可构建、测试和发布适用于移动、Web、桌面和嵌入式平台的精美应用。",textDirection:TextDirection.ltr, // 文本方向:ltr 是 left to right 从左到右;rtl 从右到左style: TextStyle(fontSize: 30,color: Colors.red,fontWeight: FontWeight.w500,fontStyle: FontStyle.italic,// decoration: TextDecoration.lineThrough, //文本修饰:中划线decorationColor: Colors.blue,),textAlign: TextAlign.right,maxLines: 3, // 文本最大显示行数overflow: TextOverflow.ellipsis, // 文本溢出显示三个点textScaleFactor: 1.5, // 文本放大倍数),// 多行文本组件 RichTixt 相当于 HTML 的 <div></div> 标签RichText(// TextSpan 相当于 HTML 的 <span></span> 标签text: const TextSpan(text: "hello",style: TextStyle(fontSize: 40,color: Colors.deepOrange,),// children 可以显示多行文本。children: [TextSpan(text: "flutter",style: TextStyle(fontSize: 40,color: Colors.blue,),),TextSpan(text: "你好世界!",style: TextStyle(fontSize: 40,color: Colors.blue,fontFamily: 'Zhi_Mang_Xing', // **** 这是今天的主角,局部字体设置!!!!),),TextSpan(text: "你好世界!",style: TextStyle(fontSize: 40,color: Colors.blue,// fontFamily: 'Zhi_Mang_Xing', // **** 这是今天的主角,局部字体设置!!!!),),]),),],);}
}

执行效果如下:

字体文件的具体位置:

发布者:admin,转转请注明出处:http://www.yc00.com/news/1700104766a981339.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信