本节目标
Text 构造参数
TextStyle 样式构造参数
Text.rich、RichText、TextSpan 处理复杂字符显示
Text Text Widget,从名字也可以看出,在 Flutter 里是用来负责显示文本信息的一个组件,功能类似于 Android 的 TextView、HTML 的一些文本标签等等,属于基础组件。
构造函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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, })
style 属性比较常用,传入的是 TextStyle 对象,我们细看下它可以配置哪些属性样式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 const TextStyle({ this .inherit = true , this .color, this .fontSize, this .fontWeight, this .fontStyle, this .letterSpacing, this .wordSpacing, this .textBaseline, this .height, this .locale, this .foreground, this .background, this .shadows, this .decoration, this .decorationColor, this .decorationStyle, this .debugLabel, String fontFamily, List <String > fontFamilyFallback, String package, })
示例
1 2 3 4 5 6 Text('字体24下划线' , style: TextStyle( color: Colors.blue, fontSize: 24 , decoration: TextDecoration.underline, )),
1 2 3 4 5 6 7 8 Text('放大加粗' , textScaleFactor: 1.2 , style: TextStyle( fontWeight: FontWeight.bold, fontSize: 24 , color: Colors.green, decoration: TextDecoration.none, )),
1 2 3 4 5 6 7 8 9 10 11 Text( '缩放,Each line here is progressively more opaque. The base color is material.Colors.black, and Color.withOpacity is used to create a derivative color with the desired opacity. The root TextSpan for this RichText widget is explicitly given the ambient DefaultTextStyle, since RichText does not do that automatically. The inner TextStyle objects are implicitly mixed with the parent TextSpans TextSpan.style.' , textScaleFactor: 1.0 , textAlign: TextAlign.center, softWrap: true , maxLines: 3 , overflow: TextOverflow.ellipsis, style: TextStyle( fontWeight: FontWeight.bold, fontSize: 18 , )),
Text.rich、RichText 、TextSpan 构造函数 可以在 Text 里加入一些 Span 标签,对某部分文字进行个性化改变样式,如加入 @ 符号,加入超链接、变色、加表情等等。Text.rich(…) 等价于 RichText(…),用哪个都可以。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 const Text.rich( this .textSpan, { Key key, this .style, this .strutStyle, this .textAlign, this .textDirection, this .locale, this .softWrap, this .overflow, this .textScaleFactor, this .maxLines, this .semanticsLabel, }) const RichText({ Key key, @required this .text, this .textAlign = TextAlign.start, this .textDirection, this .softWrap = true , this .overflow = TextOverflow.clip, this .textScaleFactor = 1.0 , this .maxLines, this .locale, this .strutStyle, })
textSpan 类型是 TextSpan ,其它参数同上
1 2 3 4 5 6 7 8 9 10 const TextSpan({ this .style, this .text, this .children, this .recognizer, })
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Text.rich(TextSpan( text: 'TextSpan' , style: TextStyle( color: Colors.red, fontSize: 24.0 , ), children: <TextSpan>[ new TextSpan( text: 'aaaaa' , style: new TextStyle( color: Colors.blueGrey, ), ), new TextSpan( text: 'bbbbbb' , style: new TextStyle( color: Colors.cyan, ), ), ], )),
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Text.rich(TextSpan( children: <TextSpan>[ new TextSpan( text: 'Tap点击' , style: new TextStyle( color: Colors.blueGrey, ), recognizer: new TapGestureRecognizer() ..onTap = () { print ('被点击了' ); }, ), ], )),
recognizer 用来识别事件
TapGestureRecognizer tap 点击手势
代码 https://github.com/ducafecat/flutter-learn/tree/master/text_widget
参考
© 猫哥
https://ducafecat.tech
邮箱 ducafecat@gmail.com / 微信 ducafecat / 留言板 disqus