I need help to understand why Rows
, Columns
and Expanded
need a finite width constraint in this kind of layout.
The layout to me seems very simple: two columns should divide equally the total device width, in every column there must be a fixed width label and a textfield which should take the remaining column width:
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Row(
children: [
Column(
children: [
Row(children: [
Text('Field 1:'),
Expanded(
child: TextField(),
),
]),
],
),
Column(
children: [
Row(children: [
Text('Field 2:'),
Expanded(
child: TextField(),
),
]),
],
),
],
),
),
),
);
}
Of course i get this error:
flutter: The following assertion was thrown during performLayout():
flutter: RenderFlex children have non-zero flex but incoming width constraints are unbounded.
flutter: When a row is in a parent that does not provide a finite width constraint, for example if it is in a
flutter: horizontal scrollable, it will try to shrink-wrap its children along the horizontal axis. Setting a
flutter: flex on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining
flutter: space in the horizontal direction.
[...]
What's the best strategy to avoid setting a fixed width letting columns and expanded do their job?
EDIT:
I changed my code wrapping Column
widgets inside an Expanded
widget.
That did the job, this code is not throwing errors:
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Row(
children: [
Expanded(
child: Column(
children: [
Row(children: [
Text('Field 1:'),
Expanded(
child: TextField(),
),
]),
],
),
),
Expanded(
child: Column(
children: [
Row(children: [
Text('Field 2:'),
Expanded(
child: TextField(),
),
]),
],
),
),
],
),
),
),
);
}