Flutter-Text

2,599 阅读2分钟

Txet

属性 类型 说明
data String 必传参数,可以隐藏参数名
style TextStyle Text的样式设置,可以设置背景、大小、颜色等 见 TextStyle
strutStyle StrutStyle www.jianshu.com/p/631e6fc29…
textAlign TextAlign 居中,左对齐,右对齐等
textDirection TextDirection 从左到右,或者从右到左(阿拉伯语问题)
softWrap bool 是否允许换行显示
overflow TextOverflow 超出屏幕显示方式- 超出省略
textScaleFactor double 每个逻辑像素的字体像素数。
maxLines int 可选的最大文本行数,必要时可换行。不能设置0
semanticsLabel String 此文本的另一个语义标签。
textWidthBasis TextWidthBasis 测量一行或多行文本宽度的不同方法。
textHeightBehavior TextHeightBehavior

富文本 Text.rich

属性 类型 说明
textSpan TextSpan 富文本各种配置
..其他和Text一样
TextSpan
属性 类型 说明
text String 初始化数据
children List TextSpan 拼接 (拼接富文本)
style TextStyle Text的样式设置,可以设置背景、大小、颜色等 见 TextStyle
recognizer GestureRecognizer 手势事件
semanticsLabel 不介绍
TextStyle
const TextStyle({
  this.inherit = true,
  this.color,
  this.backgroundColor,
  this.fontSize,
  this.fontWeight,
  this.fontStyle,
  this.letterSpacing,
  this.wordSpacing,
  this.textBaseline,
  this.height,
  this.locale,
  this.foreground,
  this.background,
  this.shadows,
  this.fontFeatures,
  this.decoration,
  this.decorationColor,
  this.decorationStyle,
  this.decorationThickness,
  this.debugLabel,
  String fontFamily,
  List<String> fontFamilyFallback,
  String package,
})
StrutStyle
const StrutStyle({
  String fontFamily,
  List<String> fontFamilyFallback,
  this.fontSize,
  this.height, //上间距?
  this.leading, // 文本上下间距
  this.fontWeight,
  this.fontStyle,
  this.forceStrutHeight,
  this.debugLabel,
  String package,
})
TextAlign
enum TextAlign {
  left,
  right,
  center,
  justify,
  start,
  end,
}
TextOverflow
enum TextOverflow {
  /// 剪切溢出的文本以固定其容器。
  clip,

  /// 将溢出的文本淡入透明状态。
  fade,

  /// 使用省略号表示文本已溢出。
  ellipsis,

  /// 在其容器外呈现溢出的文本。
  visible,
}
代码
import 'package:flutter/material.dart';
import 'package:flutter/gestures.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => new _HomePageState();
}

class _HomePageState extends State<HomePage> {
  TapGestureRecognizer _tTapGestureRecognizer;
  LongPressGestureRecognizer longPressGestureRecognizer = new LongPressGestureRecognizer();

  @override
  void initState() {
    super.initState();
    print('dasdsadasdasdasdasdasdadas');
    _tTapGestureRecognizer = TapGestureRecognizer();
    _tTapGestureRecognizer.onTap = onTap;
  }

  void onTap() {
    print('点击了文本');
  }

  @override
  void dispose() {
    _tTapGestureRecognizer.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new ListView(
      children: [
        Text(
          '测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据',
          style: TextStyle(
            fontSize: 32,
            backgroundColor: Colors.yellow,
            color: Colors.red,
          ),
          textAlign: TextAlign.justify,
//          softWrap: false,
          overflow: TextOverflow.ellipsis,
          maxLines: 10,
          semanticsLabel: 'hahaha',
          textWidthBasis: TextWidthBasis.parent,
        ),
        Text(
          '第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段第二段',
          style: TextStyle(
            fontSize: 32,
            backgroundColor: Colors.yellow,
            color: Colors.red,
          ),
          textAlign: TextAlign.start,
          textDirection: TextDirection.rtl,
        ),
        Text.rich(
          TextSpan(
            text: '富文本数据1==',
            style: TextStyle(color: Colors.red),
            children: <TextSpan>[
              TextSpan(
                  text: '富文本数据2==',
                  recognizer: longPressGestureRecognizer..onLongPress = (){
                    print('长按了富文本222');
              },
                  style: TextStyle(color: Colors.yellow)),
              TextSpan(
                  text: '富文本数据3==',
                  style: TextStyle(color: Colors.orange)),
            ],
            recognizer: _tTapGestureRecognizer,
          ),
          style: TextStyle(fontSize: 32),
        ),
      ],
    );
  }
}
相关链接

Text StrutStyle

www.jianshu.com/p/631e6fc29…