ListView Flutter 中的文本溢出问题

Text overflow issue in ListView Flutter

我有一个带有浮动按钮、文本字段和消息列表 (ListView) 的代码。

当用户将文本写入 textField 并按下按钮时,文本将附加到 ListView。

生成textField和ListView的代码如下:

body: new Column(children: 
    <Widget>[
      new Text(
        'You have pushed the button this many times:',
      ),
      new Text(
        '$_counter',
        style: Theme.of(context).textTheme.display1,
      ),
      new Flexible(
        fit: FlexFit.tight,
        child: new ListView.builder(
          padding: new EdgeInsets.all(8.0),
          itemExtent: 20.0,
          itemBuilder: (BuildContext context, int index) {
            return new Text(_getNthValue(index), overflow: TextOverflow.clip,);
          },
          itemCount: _listTexts.length,
        ), 
      ),
      new Divider(height: 2.0,),
      new TextField(
        controller: _controller,
        onChanged: _appendText,
      )
    ],
  ),

问题出在这两者之间:textField 和 ListView。当项目数超过分隔符之前 space 的限制时,ListView 继续在其顶部呈现数据和 textField:请参见下图。

如您所见,底部最后的三 (3) 个 OK 与它们的限制重叠。在网络世界中,您可以绘制 textField 的背景,使溢出不可见,但我做不到,也没有任何其他技巧可以解决问题。

在页面顶部的 textField 之间(在照片上不可见),一切正常,如果您尝试滚动太多,反弹会在元素内停止。

在底部做什么,让它像在顶部一样遵守边界(无溢出)?

我找到了解决办法。您必须将 ClipRect 附加到 Flexible,如下所示:

      new Flexible(
        child:
          new ClipRect(
            child:
              new ListView.builder(
                padding: new EdgeInsets.all(8.0),
                itemExtent: 20.0,
                itemBuilder: (BuildContext context, int index) {
                  return new Text(_getNthValue(index));
                },
                itemCount: _listTexts.length,
              ),
        ),
    ),

Flexible 告诉 ListView 不要扩展超过屏幕,ClipRect 会注意没有太多项目会溢出文本区域。