Flutter 中 Text 的使用详解(一) | Flutter Widgets

1,010 阅读3分钟

这是我参与更文挑战的第23天,活动详情查看: 更文挑战

前言

上篇我们聊完了图片,一款 App 的文字内容是非常核心的,从按钮提示到商品描述都离不开文字,这篇我们就开始聊聊 Flutter 中文字部分吧。

Text

Text 我们其实已经很熟悉了,之前在 AppBar 设置 title 的时候用过,这里我们就展开聊聊他的各种属性

普通的文字

  • 用代码这样写
Text("Text - ZeroFlutter")
  • 展示效果如下

image.png

加点样式

不同的内容所呈现的样式有所不一样,我们需要调整字号、粗细、颜色、字体、附加样式等方式来让文字展示更加合理且体现层次感,但是不要整花里胡哨的。
加载样式之前我们先看一个 TextStyle 类,这个就是给文字设置样式的,他有多达 24 个属性可以调配,但是常用的可能也就不到10个,其他的了解即可。我们先看一眼他的源码,然后展示几个我们常用的效果吧。
image.png
下面我们就开始吧

color - 先设置个颜色

Text(
  "Text - ZeroFlutter",
  style: TextStyle(
    // 颜色蓝色
    color: Colors.blue,
  ),
)

image.png

fontSize - 字号

Text(
  "Text - ZeroFlutter",
  style: TextStyle(
    // 颜色蓝色
    color: Colors.blue,
    // 字号 24
    fontSize: 24,
  ),
)

image.png

fontWeight - 字重

Text(
  "Text - ZeroFlutter",
  style: TextStyle(
    // 颜色蓝色
    color: Colors.blue,
    // 字号 24
    fontSize: 24,
    // 字重 粗
    fontWeight: FontWeight.bold,
  ),
)

image.png

  • 字重为 [w100 ~ w900] ,可以看如下源码

image.png

backgroundColor - 背景色

Text(
  "Text - ZeroFlutter",
  style: TextStyle(
    // 颜色蓝色
    color: Colors.blue,
    // 字号 24
    fontSize: 24,
    // 字重 粗
    fontWeight: FontWeight.bold,
    // 背景 橘色
    backgroundColor: Colors.orange,
  ),
)

image.png

height - 行高

Text(
  "Text - ZeroFlutter",
  style: TextStyle(
    // 颜色蓝色
    color: Colors.blue,
    // 字号 24
    fontSize: 16,
    // 字重 粗
    fontWeight: FontWeight.bold,
    // 背景 橘色
    backgroundColor: Colors.orange,
    // 行高,这里是 fontSize * height
    height: 3,
  ),
)

image.png

这里使用 Widget Inspector 查看的效果

  • 不设置或null 则是文字高度
  • 设置了则是fontSize * height 的高度

image.png

字间距

Text(
  "Text - ZeroFlutter 我们聊聊 Text",
  style: TextStyle(
    // 颜色蓝色
    color: Colors.blue,
    // 每个文字之间的间距
    letterSpacing: 4,
    // 每个单词之间的间距
    wordSpacing: 10,
  ),
)
默认letterSpacing: 4wordSpacing: 10
image.pngimage.pngimage.png
默认距离每个文字之间的距离每个单词之间,主要是解析空格来增加距离

fontStyle - 字样式

Text(
  "Text - ZeroFlutter",
  style: TextStyle(
    // 颜色蓝色
    color: Colors.blue,
    // 字号 24
    fontSize: 24,
    // 字重 粗
    fontWeight: FontWeight.bold,
    // 文字样式
    // fontStyle: FontStyle.normal,
    fontStyle: FontStyle.italic,
  ),
)
FontStyle.normalFontStyle.italic(倾斜)
image.pngimage.png

decoration - 文字装饰

image.png
这里需要使用组合属性,才可以达到我们想要的效果。

Text(
  "Text - ZeroFlutter",
  style: TextStyle(
    // 颜色蓝色
    color: Colors.blue,
    // 字号 24
    fontSize: 24,
    // 字重 粗
    fontWeight: FontWeight.bold,
    // 文字装饰
    decoration: TextDecoration.underline,
    // 装饰样式
    decorationStyle: TextDecorationStyle.wavy,
    // 装饰颜色
    decorationColor: Colors.red,
  ),
)
  • 4 种装饰 | TextDecoration.none | TextDecoration.underline | | :---: | :---: | | image.png | image.png | | TextDecoration.overline | TextDecoration.lineThrough | | image.png | image.png |

  • 5种装饰样式 | TextDecorationStyle.solid | TextDecorationStyle.double | TextDecorationStyle.wavy | | :---: | :---: | :---: | | image.png | image.png | image.png | | TextDecorationStyle.dotted | TextDecorationStyle.dashed | | | image.png | image.png | |

  • 装饰颜色

image.png

源码仓库

基于 Flutter 🔥 最新版本

参考链接

关注专栏

  • 此文章已收录到下面👇 的专栏,可以直接关注
  • 更多文章继续阅读|系列文章持续更新

👏 欢迎点赞➕收藏➕关注,有任何问题随时在下面👇评论,我会第一时间回复哦