![Flutter实战入门](https://wfqqreader-1252317822.image.myqcloud.com/cover/55/32436055/b_32436055.jpg)
3.1.1 文本组件(Text)
Text组件是用于展示文本的组件,是最基本的组件之一。下面是Text源码的构造函数:
class Text extends StatelessWidget { const Text( this.data, {//显示的文本 Key key, this.style, //文本的样式,包括字体、颜色、大小等 this.strutStyle, this.textAlign,// 对齐方式,包括左对齐、右对齐 this.textDirection,// 文本方向,包括从左到右,从右到左 this.locale, this.softWrap,// 是否自动换行 this.overflow,// 文本的截取方式 this.textScaleFactor, this.maxLines,// 显示的最大行数 this.semanticsLabel, this.textWidthBasis, }) : assert( data != null, 'A non-null String must be provided to a Text widget.', ), textSpan = null, super(key: key);
通过源码我们发现,很多情况下属性都是“顾名思义”的,比如data表示Text组件展示的文本,是必填参数,style表示文本的样式等。Text主要属性参见表3-1。
表3-1 Text属性
![](https://epubservercos.yuewen.com/E5359F/17517093106688906/epubprivate/OEBPS/Images/b3-1-i.jpg?sign=1739306322-Su0Xrq5eFdOVHHZaJUL1pSyQ47XjQ20i-0-7b7cbcef60633f07a2fd6c81b5949816)
![](https://epubservercos.yuewen.com/E5359F/17517093106688906/epubprivate/OEBPS/Images/35-i.jpg?sign=1739306322-gSVrsfMQmDIf8owUzKTpa0vqMiEeEdGO-0-6689de89ed2112f710773041710cd5d7)
使用Text组件直接显示“Flutter实战入门”,代码如下:
Text('Flutter 实战入门')
Text组件中style属性表示文本的样式,类型为TextStyle,TextStyle主要属性参见3-2。
表3-2 TextStyle属性
![](https://epubservercos.yuewen.com/E5359F/17517093106688906/epubprivate/OEBPS/Images/b3-2-i.jpg?sign=1739306322-5XUdkphnfyukcViSkLHYVkA8UizSdh5E-0-13a281eb6e5eb063e87977de0ee68855)
设置字体颜色为蓝色、字体大小为20,带阴影,代码如下:
Text( style: TextStyle(color: Colors.blue,fontSize: 20, shadows: [ Shadow(color: Colors.black12,offset: Offset(3, 3),blurRadius: 3) ]), )
效果如图3-1所示。
![](https://epubservercos.yuewen.com/E5359F/17517093106688906/epubprivate/OEBPS/Images/t3-1-i.jpg?sign=1739306322-ilTV6DNT8trc0JRSODndosq93I6gMZCR-0-9b4460e41cedf6d0e7b4e586d9fdfb76)
图3-1 Text样式实战
Text组件中textAlign属性代表文本对齐方式,值包括左对齐、中间对齐、右对齐。分别设置为左对齐、中间对齐、右对齐,代码如下:
Container( width: 300, color: Colors.black12, child: Text('Flutter 实战入门'), ), SizedBox( height: 10, ), Container( width: 300, color: Colors.black12, child: Text( 'Flutter 实战入门', textAlign: TextAlign.center, ), ), SizedBox( height: 10, ), Container( width: 300, color: Colors.black12, child: Text( 'Flutter 实战入门', textAlign: TextAlign.end, ), ),
若要看到文本的对齐效果,需要父组件比文本组件大,所以加入Container父组件。Container是一个容器组件,SizeBox是为了分割开3个Text,效果如图3-2所示。
![](https://epubservercos.yuewen.com/E5359F/17517093106688906/epubprivate/OEBPS/Images/t3-2-i.jpg?sign=1739306322-Vp2j6tNIinOWg8BkRzR8F1wHVgmz0nXc-0-85b6cccfceeb2925894090292f8de1c7)
图3-2 Text对齐方式
softWrap属性表示是否自动换行,设置为true表示自动换行,设置为false表示不自动换行,代码如下:
Text( 'Flutter 实战入门Flutter 实战入门Flutter 实战入门Flutter 实战入门 Flutter 实战入门Flutter 实战入门', softWrap: true, ), SizedBox( height: 10, ), Text( 'Flutter 实战入门Flutter 实战入门Flutter 实战入门Flutter 实战入门 Flutter 实战入门Flutter 实战入门', softWrap: false, ),
效果如图3-3所示。
![](https://epubservercos.yuewen.com/E5359F/17517093106688906/epubprivate/OEBPS/Images/t3-3-i.jpg?sign=1739306322-aitSUmJRZ3lV8FS9TGuj31IeZWSEUIfl-0-734d6eea3f5e5fe40790cc17da14b0b3)
图3-3 Text自动换行
Overflow属性表示当文本超过范围时的截取方式,包括直接截取、渐隐、省略号。Overflow的值参见表3-3。
表3-3 Overflow的值
![](https://epubservercos.yuewen.com/E5359F/17517093106688906/epubprivate/OEBPS/Images/b3-3-i.jpg?sign=1739306322-JSSeOt70qq4slqkHub5GdXBiuzF0YPBf-0-9e410cfb8deac0d8362e714fe8143124)
Overflow用法如下:
Container( width: 150, color: Colors.black12, child: Text( 'Flutter 实战入门 截取方式:直接截取', overflow: TextOverflow.clip, softWrap: false, ), ), SizedBox( height: 10, ), Container( width: 150, color: Colors.black12, child: Text( 'Flutter 实战入门 截取方式:渐隐', overflow: TextOverflow.fade, softWrap: false, ), ), SizedBox( height: 10, ), Container( width: 150, color: Colors.black12, child: Text( 'Flutter 实战入门 截取方式:省略号', overflow: TextOverflow.ellipsis, softWrap: false, ), ), SizedBox( height: 10, ), Container( width: 150, color: Colors.black12, child: Text( 'Flutter 实战入门 截取方式:显示', overflow: TextOverflow.visible, softWrap: false, ), ),
效果如图3-4所示。
![](https://epubservercos.yuewen.com/E5359F/17517093106688906/epubprivate/OEBPS/Images/t3-4-i.jpg?sign=1739306322-LcjcapOHnWN0dRvZrjLHAGeVSQk9OO2F-0-7b102ed8f504b4ae1946d2483d3361e9)
图3-4 Overflow的使用效果
项目中经常会遇到这样的需求:将一句话中的某几个字高亮显示。可以通过多个Text控件来实现这个需求,当然还有更简单的方式——通过TextSpan实现。例如,文本展示“当前你所看的书是《Flutter实战入门》。”,其中“Flutter实战入门”用红色高亮显示,代码如下:
Text.rich( TextSpan( text: '当前你所看的书是《', style: TextStyle(color: Colors.black), children: <InlineSpan>[ TextSpan(text: 'Flutter 实战入门', style: TextStyle(color: Colors.red)), TextSpan( text: '》。', ), ], ), ),
效果如图3-5所示。
![](https://epubservercos.yuewen.com/E5359F/17517093106688906/epubprivate/OEBPS/Images/t3-5-i.jpg?sign=1739306322-ktK8T0gfgG2cQsTcmc1xQneITJ1vVKoM-0-ec281463e90eadc3ca7f32e897502c43)
图3-5 TextSpan的使用效果