BoxConstraints 强制无限宽度
BoxConstraints forces an infinite width
在列中添加行时出现错误。我收到以下错误:
I/flutter ( 6449): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 6449): The following assertion was thrown during performLayout():
I/flutter ( 6449): BoxConstraints forces an infinite width.
I/flutter ( 6449): These invalid constraints were provided to RenderAnimatedOpacity's layout() function
这里还有我的代码供参考:
return new Scaffold(
backgroundColor: whiteColor,
body: new Column(
children: <Widget>[
imgHeader,
lblSignUp,
txtEmail,
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
txtFirstName,
txtLastName
],
),
],
),
);
如果您在 Row
中使用 TextField
,那么您需要使用 Flexible
或 Expanded
。
此答案中提供了更多详细信息。
错误原因:
TextField
在水平方向扩展,Row
也是,所以我们需要限制TextField
的宽度,有很多方法可以做到。
使用Expanded
Row(
children: <Widget>[
Expanded(child: TextField()),
// more widgets
],
)
使用Flexible
Row(
children: <Widget>[
Flexible(child: TextField()),
// more widgets
],
)
将其包裹在Container
或SizedBox
中并提供width
Row(
children: <Widget>[
SizedBox(width: 100, child: TextField()),
// more widgets
],
)
仅当您尝试为作为父项的 Row
、Column
或 Container
定义无限 width
和 height
时才会发生这种情况想要使用所有 space.
的 TextField
要解决这个问题,您应该将 TextField
换成 Flexible
或 Expanded
。
这样做是为了解决上述及以后的问题。
Expanded(
child: txtFirstName,
),
Flexible(
child: txtLastName,
),
完整示例
Column(
children: <Widget>[
imgHeader,
lblSignUp,
txtEmail,
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Expanded(
child: txtFirstName,
),
Flexible(
child: txtLastName,
),
],
),
],
)
在列中添加行时出现错误。我收到以下错误:
I/flutter ( 6449): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 6449): The following assertion was thrown during performLayout():
I/flutter ( 6449): BoxConstraints forces an infinite width.
I/flutter ( 6449): These invalid constraints were provided to RenderAnimatedOpacity's layout() function
这里还有我的代码供参考:
return new Scaffold(
backgroundColor: whiteColor,
body: new Column(
children: <Widget>[
imgHeader,
lblSignUp,
txtEmail,
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
txtFirstName,
txtLastName
],
),
],
),
);
如果您在 Row
中使用 TextField
,那么您需要使用 Flexible
或 Expanded
。
此答案中提供了更多详细信息。
错误原因:
TextField
在水平方向扩展,Row
也是,所以我们需要限制TextField
的宽度,有很多方法可以做到。
使用
Expanded
Row( children: <Widget>[ Expanded(child: TextField()), // more widgets ], )
使用
Flexible
Row( children: <Widget>[ Flexible(child: TextField()), // more widgets ], )
将其包裹在
Container
或SizedBox
中并提供width
Row( children: <Widget>[ SizedBox(width: 100, child: TextField()), // more widgets ], )
仅当您尝试为作为父项的 Row
、Column
或 Container
定义无限 width
和 height
时才会发生这种情况想要使用所有 space.
TextField
要解决这个问题,您应该将 TextField
换成 Flexible
或 Expanded
。
这样做是为了解决上述及以后的问题。
Expanded(
child: txtFirstName,
),
Flexible(
child: txtLastName,
),
完整示例
Column(
children: <Widget>[
imgHeader,
lblSignUp,
txtEmail,
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Expanded(
child: txtFirstName,
),
Flexible(
child: txtLastName,
),
],
),
],
)