Flutter Flexible不将文本换行

时间:2019-10-31 03:42:29

标签: flutter word-wrap

我正在尝试制作一个可能包含一些长文本的小部件,我希望将其换行。

我正在尝试使用“ Flexible”小部件来包装我的文本,但是它仍然溢出,我不知道出了什么问题。

这是正在发生的事情:enter image description here

这是我与列相关的代码:

Container(
  child: Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      Text(
        'My Title text',
        style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: 18.0,
            color: Colors.black),
      ),
      Text(
        'This is lower text',
        style: TextStyle(
            fontWeight: FontWeight.w200,
            fontSize: 16.0,
            color: Colors.black),
      ),
      Flexible(
        child: Text(
          'Here is some long text that I am expecting will go off of the screen.',
          style: TextStyle(
              fontWeight: FontWeight.normal,
              fontSize: 16.0,
              color: Colors.black),
        ),
      )
    ],
  ),
),

如果相关的是整个小部件

Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Material(
        color: Colors.transparent,
        child: Container(
          height: 100.0,
          child: Padding(
            padding: EdgeInsets.all(8.0),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Padding(
                  padding: EdgeInsets.all(16.0),
                  child: Icon(
                    Icons.cake,
                    size: 60.0,
                  ),
                ),
                Container(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        'My Title text',
                        style: TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: 18.0,
                            color: Colors.black),
                      ),
                      Text(
                        'This is lower text',
                        style: TextStyle(
                            fontWeight: FontWeight.w200,
                            fontSize: 16.0,
                            color: Colors.black),
                      ),
                      Flexible(
                        child: Text(
                          'Here is some long text that I am expecting will go off of the screen.',
                          style: TextStyle(
                              fontWeight: FontWeight.normal,
                              fontSize: 16.0,
                              color: Colors.black),
                        ),
                      )
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

3 个答案:

答案 0 :(得分:1)

您需要将容器放入行内展开小部件 像这样


child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Padding(
                  padding: EdgeInsets.all(16.0),
                  child: Icon(
                    Icons.cake,
                    size: 60.0,
                  ),
                ),
                Expanded(child: Container(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        'My Title text',
                        style: TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: 18.0,
                            color: Colors.black),
                      ),
                      Text(
                        'This is lower text',
                        style: TextStyle(
                            fontWeight: FontWeight.w200,
                            fontSize: 16.0,
                            color: Colors.black),
                      ),
                      Flexible(
                        child: Text(
                          'Here is some long text that I am expecting will go off of the screen.',
                          softWrap: true,
                          style: TextStyle(
                              fontWeight: FontWeight.normal,
                              fontSize: 16.0,
                              color: Colors.black),
                        ),
                      )
                    ],
                  ),
                ),
             ) ],
            ),

问题是,如果您没有将容器放入“展开”状态,则“行”小部件会将其自身扩展到水平位置,并且会溢出。

答案 1 :(得分:0)

您可以使用此处扩展。 扩展,这会强制孩子扩展以填充可用空间。您可以在此处展开​​列。

以下是列的代码段:-

Expanded(
                  child: Container(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          'My Title text',
                          style: TextStyle(
                              fontWeight: FontWeight.bold,
                              fontSize: 18.0,
                              color: Colors.black),
                        ),
                        Text(
                          'This is lower text',
                          style: TextStyle(
                              fontWeight: FontWeight.w200,
                              fontSize: 16.0,
                              color: Colors.black),
                        ),
                        Flexible(
                          child: Text(
                            'Here is some long text that I am expecting will go off of the screen.',
                            style: TextStyle(
                                fontWeight: FontWeight.normal,
                                fontSize: 16.0,
                                color: Colors.black),
                          ),
                        )
                      ],
                    ),
                  ),
                )

这是您的期望:- enter image description here

希望它会起作用。

答案 2 :(得分:0)

您可以尝试通过将容器的width放置到70%并将图像放置30%来尝试这种方法。这里不需要弹性小工具

Container(
 width:MediaQuery.of(context).size.width*0.7
  child: Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      Text(
        'Here is some long text that I am expecting will go off of the screen.',
        style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: 18.0,
            color: Colors.black),
      ),
      Text(
        'Here is some long text that I am expecting will go off of the screen.',
        style: TextStyle(
            fontWeight: FontWeight.w200,
            fontSize: 16.0,
            color: Colors.black),
      ),
      Text(
          'Here is some long text that I am expecting will go off of the screen.',
          style: TextStyle(
              fontWeight: FontWeight.normal,
              fontSize: 16.0,
              color: Colors.black
         )
    ],
  ),
),