Flutter之Text的基本使用

401 阅读2分钟

学习flutter的时候做的笔记,方便以后查看。

1、普通文本样式:

Text("设置普通文本样式")

2、设置文本颜色:

Text("设置文本颜色为红色", style: TextStyle(color: Color.fromARGB(255, 0, 0, 1)))

Text("设置文本颜色为红色", style: TextStyle(color: Colors.red))

3、设置文本背景颜色:

Text("设置文本背景颜色为红色", style: TextStyle(backgroundColor: Colors.red))

4、设置文本字体大小:

Text("设置文本字体大小为15", style: TextStyle(fontSize: 15))

5、设置文本加粗:

Text("设置文本加粗", style: TextStyle(fontWeight: FontWeight.w900))

6、设置文本为斜体:

Text("设置文本为斜体", style: TextStyle(fontStyle: FontStyle.italic))

7、设置文本之间的间隙:

Text("设置文本之间的间隙space为5", style: TextStyle(letterSpacing: 5))

8、设置文本内单词间距:

Text("设置文本内单词间距 word space为10", style: TextStyle(wordSpacing: 10))

9、设置文本行高,加上背景颜色效果更明显:

Text("设置文本行高为3", style: TextStyle(height: 3, backgroundColor: Colors.red))

10、设置文本阴影:

Text("设置文本阴影", style: TextStyle(shadows: [Shadow(color: Colors.black, offset: Offset(2, 1))]))

11、设置文本文字删除线:

Text('设置文本文字删除线', style: TextStyle(decoration: TextDecoration.lineThrough, decorationColor: Colors.red))

12、设置文本文字下划线:

Text('设置文本文字下划线', style: TextStyle(height: 3, decoration: TextDecoration.underline, decorationStyle: TextDecorationStyle.solid))

13、设置文本对齐方式,其他方式改变参数即可:

Text("设置文本对齐方式", textAlign: TextAlign.left)

14、设置文本换行softWrap:

Text("设置文本换行softWrap,自动换行自动换行自动换行自动换行自动换行自动换行自动换行", softWrap: true)

Text("设置文本换行softWrap,不换行不换行不换行不换行不换行不换行不换行不换行不换行", softWrap: false)

15、设置文本溢出处理:

Text("设置文本溢出overflow文本溢出文本溢出文本溢出文本溢出",overflow: TextOverflow.ellipsis)

Text("设置文本溢出clip文本溢出文本溢出文本溢出文本溢出",overflow: TextOverflow.clip)

Text("设置文本溢出fade文本溢出文本溢出文本溢出文本溢出",overflow: TextOverflow.fade)

16、设置文本显示行数:

Text("设置文本显示行数为2设置文本显示行数为2设置文本显示行数为2",maxLines: 2)

17、设置富文本:

Text.rich(TextSpan(children: [
  TextSpan(
      text: "文本1",
      style: TextStyle(
          color: Colors.red,
          fontSize: 18.0,
          fontWeight: FontWeight.w700))
  TextSpan(
      text:  "文本2",
      style: TextStyle(
          color: Color.fromRGBO(255, 85, 46, 1),
          fontSize: 24.0,
          fontWeight: FontWeight.w700))
  TextSpan(
      text:  "文本3",
      style: TextStyle(
        decoration: TextDecoration.lineThrough,
        color: Color.fromRGBO(153, 153, 153, 1),
        fontSize: 14.0
      )),
  TextSpan(text:  "文本4文本4文本4文本4文本4文本4")
]))

18、指定文本方向

Text("指定文本方向为从左到右", textDirection: TextDirection.ltr)

Text("指定文本方向为从右到左", textDirection: TextDirection.rtl)