简体   繁体   English

如何使用颤振文本小部件进行文本换行?

[英]How do I text wrap with flutter text widget?

I have a RenderFlex overflowed by 72 pixels on the right issue RenderFlex overflowed by 72 pixels on the right issue

Caused by the first Text() widget, when users have really long texts.当用户有很长的文本时,由第一个 Text() 小部件引起。 I even tried to use Textoverflow, but that didn't do anything either.我什至尝试使用 Textoverflow,但这也没有做任何事情。

So I tried to wrap it with a Flexible as suggested here Flutter- wrapping text however it's not working.因此,我尝试按照Flutter-wrapping text的建议使用 Flexible 包装它,但它不起作用。 Any idea what I'm doing wrong?知道我做错了什么吗?

Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                widget.p.description,
                style: TextStyle(
                  fontWeight: FontWeight.w800,
                ),
                overflow: TextOverflow.ellipsis,
              ),
              SizedBox(height: 4.0),
              Row(
                children: [
                  Text(
                    timeago.format(widget.p.timestamp.toDate()),
                    style: TextStyle(),
                  ),
                  SizedBox(width: 3.0),
                  StreamBuilder(
                    stream: lRef
                        .where('pId', isEqualTo: widget.p.pId)
                        .snapshots(),
                    builder:
                        (context, AsyncSnapshot<QuerySnapshot> snapshot) {
                      if (snapshot.hasData) {
                        QuerySnapshot snap = snapshot.data;
                        List<DocumentSnapshot> docs = snap.docs;
                        return buildCount(context, docs?.length ?? 0);
                      } else {
                        return buildCount1(context, 0);
                      }
                    },
                  ),
                ],
              ),
            ],
          ),

you have to wrap your text into a SizedBox and then you can also set multiple lines by editing the maxLines property of the Text widget:您必须将文本包装到 SizedBox 中,然后您还可以通过编辑 Text 小部件的 maxLines 属性来设置多行:

Example:例子:

 SizedBox( //You can define in as your screen's size width, //or you can choose a double //ex: //width: 100, width: MediaQuery.of(context).size.width, //this is the total width of your screen child: Text( widget.p.description, style: TextStyle( fontWeight: FontWeight.w800, ), overflow: TextOverflow.ellipsis, ), );

Also you can define how many lines the text should be as you can see below:您还可以定义文本应该有多少行,如下所示:

 SizedBox( //You can define in as your screen's size width, //or you can choose a double //ex: //width: 100, width: MediaQuery.of(context).size.width, //this is the total width of your screen child: Text( widget.p.description, maxLines: 5, style: TextStyle( fontWeight: FontWeight.w800, ), overflow: TextOverflow.ellipsis, ), );

You can also use ConstrainedBox to set max lenght of text, so it will by multiline if it needed:您还可以使用ConstrainedBox设置文本的最大长度,因此如果需要,它将通过多行:

ConstrainedBox(
  constraints: BoxConstraints(maxWidth: 300),
  child: Text('Any loooooooong text.............................',)
),

Look this official documentation if you interest: ConstrainedBox class如果您有兴趣,请查看此官方文档: ConstrainedBox 类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM