首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何限制TextSpan小部件的文本长度

如何限制TextSpan小部件的文本长度
EN

Stack Overflow用户
提问于 2019-08-28 05:53:58
回答 6查看 46.6K关注 0票数 16

我是新来的。如何在使用TextSpan小部件时限制文本?

我的代码

代码语言:javascript
复制
Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8.0),
      child: Row(
        children: <Widget>[
          Expanded(
            flex: 2,
            child: Row(
              children: <Widget>[
                Stack(
                  children: <Widget>[
                    ClipRRect(
                      borderRadius: BorderRadius.all(Radius.circular(8)),
                      child: Image.asset(
                        lastPlayedGame.imagePath,
                        height: 60,
                        width: 45,
                        fit: BoxFit.cover,
                      ),
                    ),
                    Positioned(
                      left: 8,
                      right: 8,
                      top: 0,
                      bottom: 0,
                      child: Container(
                        decoration: BoxDecoration(
                          shape: BoxShape.circle,
                          color: Colors.white,
                        ),
                        child: Icon(
                          Icons.play_arrow,
                          color: Colors.red,
                        ),
                      ),
                    ),
                  ],
                ),
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 12),
                  child: RichText(
                    text: TextSpan(children: [
                      TextSpan(text: lastPlayedGame.name, style: headingTwoTextStyle,),
                      TextSpan(text: '\n'),
                      TextSpan(text: "${lastPlayedGame.hoursPlayed} hours played", style: bodyTextStyle),
                    ]),
                  ),
                )
              ],
            ),
          ),
          Expanded(
            child: GameProgressWidget(screenWidth: screenWidth, gameProgress: gameProgress),
          ),
        ],
      ),
    );
  }
}

在我的Android设备上运行时,我会得到一个错误:

RenderFlex在右边溢出15个像素。

如何限制文本长度?也许检查文本是否是屏幕的最大值,是否会显示Assasin's Creed... (可能用圆点?)

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2019-08-28 06:13:36

如果要在RichText中使用中的Widget并使用省略号防止溢出,则首先必须将其包装在灵活中。FlexibleRow显示RichText可以缩小。

在将RichText包装到Flexible中之后,只需将overflow: TextOverflow.ellipsis添加到RichText中即可。这里是一个最小的例子,其中有一个RichTextFlexible中,一个在Row中。

代码语言:javascript
复制
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
            child: Container(
              padding: EdgeInsets.all(4.0),
          color: Colors.lime,
          width: 200.0,
          child: Row(
            children: <Widget>[
              Flexible(
                child: RichText(
                  overflow: TextOverflow.ellipsis,
                  strutStyle: StrutStyle(fontSize: 12.0),
                  text: TextSpan(
                      style: TextStyle(color: Colors.black),
                      text: 'A very long text :)'),
                ),
              ),
              Container(
                width: 100.0,
                height: 100.0,
                color: Colors.orangeAccent,
              )
            ],
          ),
        )),
      ),
    );
  }
}
票数 42
EN

Stack Overflow用户

发布于 2020-02-10 12:22:44

不需要使用RichText。只需添加文本( overflow: TextOverflow.ellipsis的一个参数),然后用Flexible包装文本小部件

示例

代码语言:javascript
复制
 Row(
            children: <Widget>[
              Icon(Icons.location_on, color: Colors.grey),
              Flexible(
                  child: Text(propertyModel.propertyAddress, style: AppTextStyle.headerSmall2(context),
                 overflow: TextOverflow.ellipsis),
              )
            ],
          ),
票数 19
EN

Stack Overflow用户

发布于 2021-06-21 05:05:02

如果您想要自定义它,也可以尝试这样做:

代码语言:javascript
复制
Text(
  _name.length > 10 ? _name.substring(0, 10)+'...' : _name,
   style: TextStyle(
     color: Colors.white,
     fontSize: 22,
     fontWeight: FontWeight.w500,
   ),
 ),

如果文本的ellipsis长度超过10,则显示。

票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57685855

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档