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

Spi*_*der 4 word-wrap flutter

我有一个RenderFlex overflowed by 72 pixels on the right issue

当用户有很长的文本时,由第一个 Text() 小部件引起。我什至尝试使用 Textoverflow,但这也没有任何作用。

因此,我尝试按照此处建议的Flutter-wrapper text使用 Flex 来包装它,但它不起作用。知道我做错了什么吗?

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);
                      }
                    },
                  ),
                ],
              ),
            ],
          ),
Run Code Online (Sandbox Code Playgroud)

Pro*_*nou 11

您必须将文本包装到 SizedBox 中,然后还可以通过编辑 Text 小部件的 maxLines 属性来设置多行:

例子:

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,
    ),
  );
Run Code Online (Sandbox Code Playgroud)

您还可以定义文本的行数,如下所示:

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,
    ),
  );
Run Code Online (Sandbox Code Playgroud)