3 min read

In Flutter the TextField widget is one of the important widgets for building any app. The TextField widget is used to allow users to input text and ultimately submit the form data. Sometimes it may happen that based on your design, you may need to add a couple of TextField inside Row. But as you do so you get an error called An InputDecorator, which is typically created by a TextField, cannot have an unbounded width. So in this tutorial, we’ll see how to fix this error.

Here’s what we’ll cover:

  1. What Does the Error Look Like?
  2. Why Did you Get this Error?
  3. Ways to Fix Inputdecorator Which Is Typically Created by a Textfield Cannot Have an Unbounded Width
    1. Using SizedBox
    2. Using Expanded (Recommended)

What Does the Error Look Like?

This is what the error looks like when you try to add the TextField inside Row widget in Flutter.

 Inputdecorator Which Is Typically Created by a Textfield Cannot Have an Unbounded Width error

Why Did you Get this Error?

You got this error because you have used the TextField inside Row widget. The problem of using the TextField directly inside the Row is that by default, The TextField takes all the available space in a horizontal direction and the Row widget doesn’t add any constraint on its children’s width. This makes it difficult for the Flutter to calculate the size of TextField.

textfield inside row

Ways to Fix Inputdecorator Which Is Typically Created by a Textfield Cannot Have an Unbounded Width

To fix the InputDecorator, which is typically created by a TextField, cannot have an unbounded width error, you can add constraints on TextField by wrapping it inside the SizedBox or Expanded widget.

Using SizedBox

Wrap your TextField widget inside the SizedBox widget. Add the width parameter to the SizedBox and give it a fixed width. This will ensure that the TextField is displayed in a limited portion instead of infinite size.

Code Example

Row(
  children: const <Widget>[
    SizedBox(
      width: 300, // <-- SEE HERE
      child: TextField(
        decoration: InputDecoration(helperText: "Enter your name"),
      ),
    ),
  ],
)

Output

textfield inside row fixed using sizedbox

Using Expanded (Recommended)

The main point here is to tell the TextField widget about how much width it can take. So wrapping the TextField widget inside the Expanded widget lets the TextField widget occupy all the available space in the horizontal direction.

Code Example

Row(
  children: const <Widget>[
    Expanded( // <-- SEE HERE
      child: TextField(
        decoration: InputDecoration(helperText: "Enter your name"),
      ),
    ),
  ],
)

Output

textfield inside row fixed using expanded widget

Conclusion

In this tutorial, we learned how to fix the InputDecorator, which is typically created by a TextField, cannot have an unbounded width error. Basically, you can use either the SizedBox widget or Expanded widget to wrap the TextField widget. Doing so will help Flutter to determine the size of the TextField. As a result, your issue will be fixed.

Would you like to check other interesting Flutter tutorials?