I am a Text, if you want ro use Flutter to write the text, you have to use me
简介:
Flutter中书写文本的时候Text是一个必不可少的一个组件。
书写了Text发现好多属性,我本来也是不知道有那么多属性,知识笔者去查源码的时候发现的,你也可以尝试一下,如果不会可以留言。
Text的基本用法和基本属性
var _width = ediaQuery.of(context).size.width; // 获取当前屏幕宽度。
Container(
width: _width,
height: 100,
alignment: Alignment.center,
margin: EdgeInsets.fromLTRB(15, (_height / 2 ) - 100, 15, 0),
decoration: BoxDecoration(
color: Colors.green[300],
borderRadius: BorderRadius.circular(15)
),
child: Text(
"I am a text, so if you are going to write with a Flutter, you have to use me",
style: TextStyle(
inherit: false, // 继承
fontSize : 24, // 字号
fontWeight: FontWeight.w500, // 字体粗细程度
fontStyle: FontStyle.italic, // 字体样式 斜体
fontFamily: "Courier", // 字体
color: Colors.pink[300], // 字体颜色 默认字体颜色是白色
backgroundColor: Colors.yellow[200], // 背景颜色
letterSpacing: 3, // 字体间距
wordSpacing: 3, // 词间距
textBaseline: TextBaseline.alphabetic, // 对齐文本基线
height: 2, // 文本的高度,以字体大小的倍数显示。
shadows: [ Shadow(color : Colors.blue , offset : Offset(6,9) , blurRadius : 0.0) ], // 阴影
locale: Locale('fr', "CH"),
// decoration: TextDecoration.underline, // 装饰 如顶部线条,底部线条
decoration: TextDecoration.none // 去掉底部双横线
decorationColor: Colors.black54, // 装饰容器颜色
decorationStyle: TextDecorationStyle.dashed , // 实线,虚线,双线等。
debugLabel: '',
package: ""
),
// strutStyle: StrutStyle(
// fontFamily : "Courier",
// fontSize : 22,
// fontWeight : FontWeight.w800
// ),
textAlign: TextAlign.right, // 对齐方式
textDirection : TextDirection.rtl, // 上所用文本的方向 感觉和对齐方式没有什么区别
locale: Locale('fr', "CH"), // 这个属性我也不知道有啥用处。
softWrap: false, // 如果为false,文本中的符号将被定位为没有限制的水平空间。
overflow: TextOverflow.ellipsis, // 实现益处打点。
textWidthBasis:TextWidthBasis.longestLine ,
textHeightBehavior: TextHeightBehavior(applyHeightToFirstAscent : true),
textScaleFactor : 1.2 , // 字体放大倍数。
maxLines: 1, // 最大的行
),
)
- 看上去比较拥挤,下面我们来介绍具体属性吧。
- 但是上面的属性常用的基本上都覆盖了。要是不想看上面的话,下面还有细分小部分内容讲解。
- 一般字体显示的样式都是在style内修改。
给字体加下划线
- 直接粘图吧。
- 在style样式中添加。
设置文本最多显示多少行。
设置益处打点
文本添加阴影
很多属性不可能全都能记得牢固,所以还是需要查阅源码来解决事情。这才是最快的效率。