我需要帮助来理解为什么Rows
,Columns
并且Expanded
在这种布局中需要一个有限的宽度约束。
布局对我来说似乎很简单:两列应该等分设备总宽度,在每一列中必须有一个固定宽度的标签和一个应该采用剩余列宽的文本字段:
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(),
),
]),
],
),
],
),
),
),
);
}
当然我得到这个错误:
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.
[...]
避免设置固定宽度让列和扩展完成工作的最佳策略是什么?
编辑:
Column
我在一个小部件中更改了我的代码包装小Expanded
部件。完成了这项工作,这段代码没有抛出错误:
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(),
),
]),
],
),
),
],
),
),
),
);
}