如何使Flutter TextField高度匹配容器的父级?

Amm*_*ang 5 dart flutter flutter-layout

我要使我的TextField高度与我的容器高度相同。请检查以下代码,让我知道如何使TextField容器的match_parent。我已经检查了这个问题,在flutter中相当于wrap_content和match_parent?但没有找到任何解决方案。我需要将TextField容器的高度和宽度全部取整。

 new Container(
            height: 200.0,
            decoration: new BoxDecoration(
                border: new Border.all(color: Colors.black)
            ),

            child: new SizedBox.expand(
              child: new TextField(
                maxLines: 2,
                style: new TextStyle(
                    fontSize: 16.0,
                   // height: 2.0,
                    color: Colors.black
                ),
                  decoration: const InputDecoration(
                    hintText: "There is no data",
                    contentPadding: const EdgeInsets.symmetric(vertical: 40.0),
                  )
              ),
            ),
          )
Run Code Online (Sandbox Code Playgroud)

请检查下面的屏幕截图,我需要TextField全力以赴Container

在此处输入图片说明

Jac*_*Sun 11

这是我的解决方案:

Container(
            height: 200,
            color: Color(0xffeeeeee),
            padding: EdgeInsets.all(10.0),
            child: new ConstrainedBox(
              constraints: BoxConstraints(
                maxHeight: 200.0,
              ),
              child: new Scrollbar(
                child: new SingleChildScrollView(
                  scrollDirection: Axis.vertical,
                  reverse: true,
                  child: SizedBox(
                    height: 190.0,
                    child: new TextField(
                      maxLines: 100,
                      decoration: new InputDecoration(
                        border: InputBorder.none,
                        hintText: 'Add your text here',
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),
Run Code Online (Sandbox Code Playgroud)

它对我来说效果很好。这是一个屏幕截图。

在此处输入图片说明


Say*_*nRC 8

2021 年回答这个问题。现在有expands房产可用。这段代码的工作原理:

Column(
  children: [
    Expanded(
        child: TextField(
          maxLines: null, 
          minLines: null, 
          expands: true,
        ), 
        flex: 1),
  ],
)
Run Code Online (Sandbox Code Playgroud)


Din*_*ian 1

让我们删除几行代码并了解 flutter 的工作原理。

  1. 为什么我们给200Container高度。无法Container根据其子项调整高度(在本例中为 SizedBox.expand)
  2. 如果我们去掉高度200,那么就Container占据了整个屏幕因为SizedBox.expand
  3. SizedBox我们的用例真的需要它吗?让我们将其删除,看看会发生什么。
  4. 现在我们的Container包装了TextField。但上面和下面都有一些空间。
  5. 谁决定了那个空间?TextField 的装饰的 contentPadding。我们也将其删除。如下所示,其中 textField 由 Container 包裹。希望这是你想要的。如果没有,请发表评论,我们可以稍微调整一下并得到您想要的。干杯 在此输入图像描述

显示上图的代码的最终版本

 new Container(
   //   height: 200.0,
      decoration: new BoxDecoration(
          border: new Border.all(color: Colors.black)
      ),

      child: new TextField(
          maxLines: 2,
          style: new TextStyle(
              fontSize: 16.0,
              // height: 2.0,
              color: Colors.black
          ),
          decoration: const InputDecoration(
            hintText: "There is no data",
//                contentPadding: const EdgeInsets.symmetric(vertical: 40.0),
          )
      ),
    )
Run Code Online (Sandbox Code Playgroud)