TextField inside of Row causes layout exception: Unable to calculate size
Solution 1:
(I assume you're using a Row
because you want to put other widgets beside the TextField
in the future.)
The Row
widget wants to determine the intrinsic size of its non-flexible children so it knows how much space that it has left for the flexible ones. However, TextField
doesn't have an intrinsic width; it only knows how to size itself to the full width of its parent container. Try wrapping it in a Flexible
or Expanded
to tell the Row
that you're expecting the TextField
to take up the remaining space:
new Row(
children: <Widget>[
new Flexible(
child: new TextField(
decoration: const InputDecoration(helperText: "Enter App ID"),
style: Theme.of(context).textTheme.body1,
),
),
],
),
Solution 2:
You get this error because TextField
expands in horizontal direction and so does the Row
, so we need to constrain the width of the TextField
, there are many ways of doing it.
-
Use
Expanded
Row( children: <Widget>[ Expanded(child: TextField()), OtherWidget(), ], )
-
Use
Flexible
Row( children: <Widget>[ Flexible(child: TextField()), OtherWidget(), ], )
-
Wrap it in
Container
orSizedBox
and providewidth
Row( children: <Widget>[ SizedBox(width: 100, child: TextField()), OtherWidget(), ], )
Solution 3:
you should use Flexible to use a Textfield inside a row.
new Row(
children: <Widget>[
new Text("hi there"),
new Container(
child:new Flexible(
child: new TextField( ),
),//flexible
),//container
],//widget
),//row
Solution 4:
As @Asif Shiraz mentioned I had same issue and solved this by Wrapping Column in a Flexible, here like this,,
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new Scaffold(
body: Row(
children: <Widget>[
Flexible(
child: Column(
children: <Widget>[
Container(
child: TextField(),
)
//container
],
))
],
mainAxisAlignment: MainAxisAlignment.spaceBetween,
),
));
}
}
Solution 5:
The solution is to wrap your Text()
inside one of the following widgets:
Either Expanded
or Flexible
. So, your code using Expanded will be like:
Expanded(
child: TextField(
decoration: InputDecoration(
hintText: "Demo Text",
hintStyle: TextStyle(fontWeight: FontWeight.w300, color: Colors.red)
),
),
),