flutter之Text组件

本文深入解析Flutter中的文本组件使用方法,包括Text与TextSpan组件的属性设置,如文本对齐、行数限制、溢出处理、字体样式调整等,以及如何通过TextSpan实现不同样式文本的组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

示例代码:

new Text(
    //文本
    'hello world',
    //文本对齐方式
	textAlign: TextAlign.center,
	//文本最多显示几行 maxLines:1,
	maxLines:1,
	//超出隐藏的样式(文字超出部分怎么处理)
	// overflow: TextOverflow.clip        直接切掉
	// overflow: TextOverflow.ellipsis  末尾多。。。
	// overflow: TextOverflow.fade      文字从上到下渐变消失
	overflow: TextOverflow.fade,
	//字体显示倍率
	textScaleFactor: 1,
	//文本其他样式
	style: TextStyle(
		fontSize:12,
		#行高
		height: 1.2,
		#字体 
	    fontFamily: "Courier",
	    #背景色
	    background: new Paint()..color=Col
		#字体颜色
		color: Color.fromARGB(255,255,100,100),
		decoration: TextDecoration.underline, //文字加横线
		decorationStyle: TextDecorationStyle.solid) //文字加横线的样式
		//文字间距调整
		letterSpacing: 4, 
	),
)

TextSpan组件:
text的所有文本内容只能按同一种样式,如果我们需要对一个Text内容的不同部分按照不同的样式显示,这时就可以使用TextSpan,它代表文本的一个“片段”。

Text.rich(TextSpan(
    children: [
     TextSpan(
       text: "Home: "
     ),
     TextSpan(
       text: "https://flutterchina.club",
       style: TextStyle(
         color: Colors.blue
       ),  
       recognizer: _tapRecognizer,
       children: <TextSpan> [
            TextSpan(text: "Lorem "),
            TextSpan(
              text: "ipsum",
              style: TextStyle(
                fontWeight: FontWeight.w300,
                fontStyle: FontStyle.italic,
                fontSize: 48,
              ),
            ),
        ],
     ),
    ]
))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值