颤振:BoxConstraints强制无限高

时间:2020-01-10 04:41:04

标签: flutter dart

我正在尝试在expand小部件上使用TextFormField属性。

我的代码如下:

body: ListView(
        children: <Widget>[
          ListTile(
            leading: CircleAvatar(
              backgroundImage: CachedNetworkImageProvider(
                currentUser.photoUrl
              )
            ),
            title: Container(
              color: Colors.blue,
              width: 250.0,
              child: TextFormField(
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 20.0,
                  color: Colors.white
                ),
                cursorColor: Colors.white,
                maxLines: null,
                minLines: null,
                expands: true,
                controller: captionController,
                decoration: InputDecoration(
                  hintText: 'Write something...',
                  border: InputBorder.none,
                  hintStyle: TextStyle(
                    fontSize: 20.0,
                    fontWeight: FontWeight.bold
                  ),
                  fillColor: Colors.blue
                )
              )
            )
          ),
  ]
)

我的问题是,每次我运行代码时,都会收到错误:BoxConstraints forces an infinite height。当文本字段的内容溢出时,如何扩展文本字段的高度。

2 个答案:

答案 0 :(得分:1)

尝试为Container()提供有限的高度。 expands尝试根据您的情况下TextFormField的父高来调整null的大小。

  body: ListView(
    children: <Widget>[
      ListTile(
        leading: CircleAvatar(
          backgroundImage: CachedNetworkImageProvider(
            currentUser.photoUrl
          )
        ),
        title: Container(
          height: 200.0, // Add your own custom height here
          //Rest of the code is same
        )
      ),
    ]
  ),

答案 1 :(得分:0)

说明==>

解决方案1)使用ListView时,实际上几乎没有无限的空间可以扩展,这就是错误出现的原因。现在更改ListView ==> Column会将扩展空间限制为视口的大小。 并且还将ListTile用Expanded小部件包裹起来,这意味着ListTile小部件将占用Column可用的全部空间<​​/ p>

DartPad链接==> https://dartpad.dev/0acdc3ebef330f717813fc5163de0ccb

解决方案2)现在,假设您仍然需要将其作为列表视图,因为它具有多个元素并使它们可滚动。现在,要实现此目的,您可以用容器将Listtile包裹起来,并为容器提供一些BoxConstraints,其maxHeight是可以从MediaQuery中获取的视口的大小

DartPad链接==> https://dartpad.dev/22f30c0aaab31bff8627226cc21875f1

相关问题