Flutter

Flutter基本组件--Text

2022-02-12  本文已影响0人  初见soulmate

构造方法

const Text(this.data, {
    Key key,
    this.style,
    this.textAlign,
    this.textDirection,
    this.locale,
    this.softWrap,
    this.overflow,
    this.textScaleFactor,
    this.maxLines,
    this.semanticsLabel,
  }

const Text.rich(this.textSpan, {
    Key key,
    this.style,
    this.textAlign,
    this.textDirection,
    this.locale,
    this.softWrap,
    this.overflow,
    this.textScaleFactor,
    this.maxLines,
    this.semanticsLabel,
  }
 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,
            //用paint来渲染text,也可以用他来改变字体颜色等
            foreground,
            //单词间距
            wordSpacing,
            // 文本行与行的高度,作为字体大小的倍数(取值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'),
        // 文本是否能换行,bool类型
        softWrap: false,
        // 如何处理视觉溢出:clip 剪切溢出的文本以修复其容器。ellipsis 使用省略号表示文本已溢出。fade 将溢出的文本淡化为透明。
        overflow: TextOverflow.clip,
        // 文字的缩放比例
        textScaleFactor: 1.0,
        // 文本要跨越的可选最大行数,
        maxLines: 2,
        // 图像的语义描述,用于向Andoid上的TalkBack和iOS上的VoiceOver提供图像描述
        semanticsLabel: 'text demo',
        textWidthBasis: TextWidthBasis.longestLine,
      )

TextSpan - 配合 textStyle 实现各种文字效果,可以添加点击事件
WidgetSpan - 可以添加其他类型的 widget

    return Scaffold(
      appBar: AppBar(
        title: Text('RichText'),
      ),
      body: RichText(
        text: TextSpan(
          text: '登陆即同意',
          style: TextStyle(color: Colors.black),
          children: [
            WidgetSpan(
              alignment: PlaceholderAlignment.middle,
              child: Image.asset(
                AssetPathConstant.imageScan,
                width: 40,
                height: 40,
              ),
            ),
            TextSpan(
              text: '《服务条款》',
              style: TextStyle(color: Colors.red),
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  ToastUtil.showToast('服务条款');
                },
            ),
            TextSpan(
              text: '和',
              style: TextStyle(color: Colors.black),
            ),
            WidgetSpan(
              alignment: PlaceholderAlignment.bottom,
              child: Image.asset(
                AssetPathConstant.imageScan,
                width: 40,
                height: 40,
              ),
            ),
            TextSpan(
              text: '《隐私政策》',
              style: TextStyle(color: Colors.red),
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  ToastUtil.showToast('隐私政策');
                },
            ),
          ],
        ),
      ),
    );
上一篇下一篇

猜你喜欢

热点阅读