Flutter - Text

简介

text,文本控件,用来展示文本内容

构造

  1. const Text(this.data, {
  2. Key key,
  3. this.style,
  4. this.textAlign,
  5. this.textDirection,
  6. this.locale,
  7. this.softWrap,
  8. this.overflow,
  9. this.textScaleFactor,
  10. this.maxLines,
  11. this.semanticsLabel,
  12. }
  13. const Text.rich(this.textSpan, {
  14. Key key,
  15. this.style,
  16. this.textAlign,
  17. this.textDirection,
  18. this.locale,
  19. this.softWrap,
  20. this.overflow,
  21. this.textScaleFactor,
  22. this.maxLines,
  23. this.semanticsLabel,
  24. }

属性介绍

style

style是Text的样式集合,主要是Text本身的显示样式。

color

文字的颜色

fontSize

文字的大小

fontWeight

字重,粗体等会使用此字段

fontStyle

字体,需要斜体时会使用此字段

letterSpacing

字间距

textBaseline

对齐文本的水平线:
TextBaseline.alphabetic:文本基线是标准的字母基线
TextBaseline.ideographic:文字基线是表意字基线;如果字符本身超出了alphabetic 基线,那么ideograhpic基线位置在字符本身的底部。

height

行高
会乘以fontSize,所以不以设置过大

decoration

通常用来添加下划线、上划线和删除线

decorationColor

与decoration相关,修改划线的颜色

fontFamily

字体

textAlign

  1. enum TextAlign {
  2. /// Align the text on the left edge of the container.
  3. left,
  4. /// Align the text on the right edge of the container.
  5. right,
  6. /// Align the text in the center of the container.
  7. center,
  8. /// Stretch lines of text that end with a soft line break to fill the width of
  9. /// the container.
  10. /// Lines that end with hard line breaks are aligned towards the [start] edge.
  11. justify,
  12. /// Align the text on the leading edge of the container.
  13. /// For left-to-right text ([TextDirection.ltr]), this is the left edge.
  14. /// For right-to-left text ([TextDirection.rtl]), this is the right edge.
  15. start,
  16. /// Align the text on the trailing edge of the container.
  17. /// For left-to-right text ([TextDirection.ltr]), this is the right edge.
  18. /// For right-to-left text ([TextDirection.rtl]), this is the left edge.
  19. end,
  20. }

其中 justinfy就是两边贴边对齐,start和end类似,差别是当文字方向是从右往左时会有些差别。

textDirection

文字方向,部分地区是从右往左排布。

softWrap

文本是否能换行,bool类型

overFlow

超出文本的表现形式,有三种枚举

  1. enum TextOverflow {
  2. /// Clip the overflowing text to fix its container.
  3. clip,
  4. /// Fade the overflowing text to transparent.
  5. fade,
  6. /// Use an ellipsis to indicate that the text has overflowed.
  7. ellipsis,
  8. }

maxLines

指定文本最多显示多少行

textScaleFactor

字体缩放

Text.rich

通过TextSpan可以对部分文字进行修饰

  1. new Text.rich(
  2. new TextSpan(
  3. text: '聆听璇律测试',
  4. children: <TextSpan>[
  5. new TextSpan(
  6. text: '测试1',
  7. style: new TextStyle(color: Colors.red)
  8. ),
  9. new TextSpan(
  10. text: '哈哈',
  11. style: new TextStyle(color: Colors.yellow)
  12. ),
  13. ]
  14. )
  15. )

以上就是Flutter中Text的基本使用。如有问题,欢迎指正。