簡體   English   中英

Flutter:BoxConstraints 強制無限高

[英]Flutter: BoxConstraints forces an infinite height

我正在嘗試在TextFormField小部件上使用expand屬性。

我的代碼如下所示:

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 請問當文本字段的內容長度溢出時如何擴展它的高度。

嘗試為Container()提供有限的高度。 expands嘗試根據在您的情況下為null的父高度來調整TextFormField大小。

  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) 當您使用 ListView 時,它實際上有無限量的空間可以擴展,這就是出現錯誤的原因。 現在更改 ListView ==> Column 將擴展空間限制為視口的大小。 並且還用一個 Expanded 小部件包裝 ListTile,這意味着 ListTile 小部件將消耗 Column 可用的整個空間

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

解決方案 2) 現在假設您仍然需要它是一個列表視圖,因為它有多個元素並且它們是可滾動的。 現在要實現這一點,你可以做的是用一個容器包裹你的 Listtile 並給容器一些 BoxConstraints,它的 maxHeight 是你可以從 MediaQuery 獲得的視口的大小

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM