14

I want to make my TextField height the same as my container height. Please check my code below and let me know how can I make TextField match_parent of my container. I've checked this question The equivalent of wrap_content and match_parent in flutter? but I didn't find any solution. I need to make TextField to take full height and width of my container.

 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),
                  )
              ),
            ),
          )

Please check the screenshot below. As said, I need my TextField to take full height of Container

enter image description here

Ammy Kang
  • 11,283
  • 21
  • 46
  • 68
  • Possible duplicate of [The equivalent of wrap\_content and match\_parent in flutter?](https://stackoverflow.com/questions/42257668/the-equivalent-of-wrap-content-and-match-parent-in-flutter) – Ahmet Zorer Aug 07 '18 at 06:55

4 Answers4

12

Here is my solution:

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',
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),

It works pretty good for me. And here is a screen shot.

enter image description here

Jack Sun
  • 2,012
  • 1
  • 16
  • 20
8

Answering this in 2021. There is an expands property available now. This code works:

Column(
  children: [
    Expanded(
        child: TextField(
          maxLines: null, 
          minLines: null, 
          expands: true,
        ), 
        flex: 1),
  ],
)
SayantanRC
  • 799
  • 7
  • 23
1

Let's remove a few lines in code and understand how flutter works.

  1. Why we are giving height 200 to Container. Can't the Container adjust the height based on its child (in this case SizedBox.expand)
  2. If we remove height 200, then Container occupied the entire screen because of SizedBox.expand
  3. Do we really need the SizedBox for our use case. Let's remove that also see what happens.
  4. Now our Container wraps the TextField. But there is some space above and below.
  5. Who decided that space? TextField's decoration's contentPadding. Let's remove that also. It looks like below where textField wrapped by Container. Hope this is what you want. If not, please comment, we can tweak a bit and get what you want. Cheers enter image description here

Final version of code which displays the above image

 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),
          )
      ),
    )
Dinesh Balasubramanian
  • 20,532
  • 7
  • 64
  • 57
0

Currently the only way to achieve the TextField to fill the available vertical space is:

TextField(maxLines: 1000000) //maxlines: any large int

While it is tempting to use TextField(maxLines: null), it will just set the TextField to expand with its content, until it reaches its container limit.

I think there needs to be a bool stretchVertically parameter. TextField(stretchVertically: true) would mean that the TextField will try to fill as much vertical space as it can. stretchVertically and maxLines would have to be mutually exclusive.

Alex.F
  • 5,648
  • 3
  • 39
  • 63