![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=1739281163-dxy3Vv1cn8HMnz5a5MMo9Q7TRCIG8h0g-0-3b4f42a6bc65016b0f185c64a4adeef5)
![](https://epubservercos.yuewen.com/E5359F/17517093106688906/epubprivate/OEBPS/Images/35-i.jpg?sign=1739281163-Ty3Kn0IVSUvMUG5PV7LUGOtpBXozpdnI-0-8103c57b005f6e485208f421331b89ac)
使用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=1739281163-sk0ca9YOiiMoniOQbEWM1FLy4rKJUMdB-0-136d9d3c4219dc05b1969a33fda42975)
设置字体颜色为蓝色、字体大小为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=1739281163-dj1iRdE769S2kOCeN5eRV99kHmQKie41-0-c1c1a84e735937b3007382b859918f80)
图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=1739281163-guJYeAQDqLaeikzVYOmGjPwHj74xWrPc-0-e7d3d92c27ae13efa718cd5a16df1591)
图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=1739281163-4EHhmCHp501zBRZeY1pksq7t9nWO359r-0-f71bf49e16f370cb1d9c6dc8bac0ca94)
图3-3 Text自动换行
Overflow属性表示当文本超过范围时的截取方式,包括直接截取、渐隐、省略号。Overflow的值参见表3-3。
表3-3 Overflow的值
![](https://epubservercos.yuewen.com/E5359F/17517093106688906/epubprivate/OEBPS/Images/b3-3-i.jpg?sign=1739281163-lLxxlzz4mNyfJAbt8nQBAlbDFZbREAOI-0-03ebb79a64c3d2bbc92532c2eefcf265)
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=1739281163-MfMYrSLGPXLZzZWmllXBUzMsE3o3LIFp-0-dba3eab0739a3639cd3f996bc4f21025)
图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=1739281163-ZtdfPysT6D8is9aGC9tZS7lnVovurEaG-0-3f1ec3166c54d6ae43fd0a2545f071d4)
图3-5 TextSpan的使用效果