我正在尝试展开放置在行中的文本小部件。但我收到错误。
代码:
class MainContainer extends StatelessWidget
{
@override
Widget build(BuildContext context)
{
return Column (
children: <Widget>[
Container(
),
Container(
child: Row(
children: <Widget>[
Row(
children: <Widget>[
Text("label1"),
Expanded(child: Text("label2")) // Problem is here
],)
],),
),
Container(
child: Row(children: <Widget>[
Text("Some Text")
],),
),
],
);
}
}
错误:
I/flutter ( 5480): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════ I/flutter ( 5480): The following assertion was thrown during performLayout(): I/flutter ( 5480): RenderFlex children have non-zero flex but incoming width constraints are unbounded. I/flutter ( 5480): When a row is in a parent that does not provide a finite width constraint, for example if it is in a I/flutter ( 5480): horizontal scrollable, it will try to shrink-wrap its children along the horizontal axis. Setting a I/flutter ( 5480): flex on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining I/flutter ( 5480): space in the horizontal direction. I/flutter ( 5480): These two directives are mutually exclusive. If a parent is to shrink-wrap its child, the child I/flutter ( 5480): cannot simultaneously expand to fit its parent. I/flutter ( 5480): Consider setting mainAxisSize to MainAxisSize.min and using FlexFit.loose fits for the flexible I/flutter ( 5480): children (using Flexible rather than Expanded). This will allow the flexible children to size I/flutter ( 5480): themselves to less than the infinite remaining space they would otherwise be forced to take, and I/flutter ( 5480): then will cause the RenderFlex to shrink-wrap the children rather than expanding to fit the maximum I/flutter ( 5480): constraints provided by the parent.
最佳答案
问题是你的 Text
约束是无界的,因为 Expanded
所以你不能把一个无界的 widget 放在一个有界的(或非零弹性的)里面)
解决方案是简单地将父 Row
放入 flex 小部件(Flexible/Expanded)
Container(
child: Row(
children: <Widget>[
Flexible(
child: Row(
children: <Widget>[
Text("label1"),
Expanded(child: Text("label2"))
],
),
)
],
),
)
关于flutter - 无法在行内展开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57306058/