Text

作者: 转移到CSDN名字丹丹的小跟班 | 来源:发表于2021-04-11 09:29 被阅读0次
介绍

Text用于显示简单样式文本,它包含一些控制文本显示样式的一些属性

列表参数
      Text(
        "Text组件的使用",
        style: TextStyle(
            // 文字颜色
            color: Color(0xfff0000),
            // none 不显示装饰线条,underline 字体下方,overline 字体上方,lineThrough穿过文字
            decoration: TextDecoration.none,
            // solid 直线,double 双下划线,dotted 虚线,dashed 点下划线,wavy 波浪线
            decorationStyle: TextDecorationStyle.solid,
            // 装饰线的颜色
            decorationColor: Colors.red,
            // 文字大小
            fontSize: 15.0,
            // normal 正常,italic 斜体
            fontStyle: FontStyle.normal,
            // 字体的粗细
            fontWeight: FontWeight.bold,
            // 文字间的宽度
            letterSpacing: 1.0,
            // 文本行与行的高度,作为字体大小的倍数(取值1~2,如1.2)
            height: 1,
            //对齐文本的水平线:
            //TextBaseline.alphabetic:文本基线是标准的字母基线
            //TextBaseline.ideographic:文字基线是表意字基线;
            //如果字符本身超出了alphabetic 基线,那么ideograhpic基线位置在字符本身的底部。
            textBaseline: TextBaseline.alphabetic),
        // 段落的间距样式
        strutStyle: StrutStyle(
          fontFamily: 'serif',
          fontFamilyFallback: ['monospace', 'serif'],
          fontSize: 20,
          height: 2,
          leading: 2.0,
          fontWeight: FontWeight.w300,
          fontStyle: FontStyle.normal,
          forceStrutHeight: true,
          debugLabel: 'text demo',
        ),
        // 文字对齐方式
        textAlign: TextAlign.center,
        // 文字排列方向 ltr 左到右,rtl右到左
        textDirection: TextDirection.ltr,
        // 用于选择区域特定字形的语言环境
        locale: Locale('zh_CN'),
        // 软包裹 ,文字是否应该在软断行出断行
        softWrap: false,
        // 如何处理视觉溢出:clip 剪切溢出的文本以修复其容器。ellipsis 使用省略号表示文本已溢出。fade 将溢出的文本淡化为透明。
        overflow: TextOverflow.clip,
        // 文字的缩放比例
        textScaleFactor: 1.0,
        // 文本要跨越的可选最大行数,
        maxLines: 2,
        // 图像的语义描述,用于向Andoid上的TalkBack和iOS上的VoiceOver提供图像描述
        semanticsLabel: 'text demo',
        textWidthBasis: TextWidthBasis.longestLine,
      )
练习
import 'package:flutter/material.dart';

main() {
  runApp(TextClass());
}

class TextClass extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.all(50),
        color: Colors.blue,
        child: Text("Hello world",
        textDirection: TextDirection.ltr,
        style: TextStyle(
          color: Colors.blue,
          fontSize: 48.0,
          height: 1.2,  
          fontFamily: "Courier",
          background: new Paint()..color=Colors.yellow,
          decoration:TextDecoration.underline,
          decorationStyle: TextDecorationStyle.dashed
      ),
    )
    );
  }
}
image.png
TextSpan介绍

在上面的例子中,Text的所有文本内容只能按同一种样式,如果我们需要对一个Text内容的不同部分按照不同的样式显示,这时就可以使用TextSpan,它代表文本的一个“片段”。而使用textRich则可以连接这些span。

属性:
const TextSpan({
  TextStyle style, 
  Sting text,
  TextDirection,
  List<TextSpan> children,
  GestureRecognizer recognizer,
});
练习:
import 'package:flutter/material.dart';

main() {
  runApp(TextClass());
}

class TextClass extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.all(50),
      color: Colors.blue,
      child: RichText(
        textDirection: TextDirection.ltr,
        text: TextSpan(
          text: "我是",
          style: TextStyle(color: Colors.black, fontSize: 20),
          children: [
            TextSpan(
              text: "textSpan",
              style: TextStyle(color: Colors.red,fontSize: 25, decoration: TextDecoration.underline,)
            ),
            TextSpan(
              text: "组件",
              style: TextStyle(color: Colors.teal[900], fontSize: 20)
            )
          ]
        )
      )
    );
  }
}
image.png
DefaultTextStyle介绍:

在Widget树中,文本的样式默认是可以被继承的(子类文本类组件未指定具体样式时可以使用Widget树中父级设置的默认样式),因此,如果在Widget树的某一个节点处设置一个默认的文本样式,那么该节点的子树中所有文本都会默认使用这个样式,而DefaultTextStyle正是用于设置默认文本样式的。

参考于:https://www.jianshu.com/p/585c5ce47bdf

相关文章

网友评论

      本文标题:Text

      本文链接:https://www.haomeiwen.com/subject/xxlikltx.html