flutter - 无法在行内展开

标签 flutter

我正在尝试展开放置在行中的文本小部件。但我收到错误。

代码:

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/

相关文章:

flutter - 如何在下面的代码中使用 systemNavigator.pop 在 flutter 中退出应用程序

flutter - 如何根据子项的大小定位 CompositedTransformFollower?

ios - iOS 中是否有 INTERNET 权限?

flutter - 如何在 Flutter 中测试使用 DateTime.now 的代码?

flutter - 每当我尝试 flutter 运行时,都会出现相同的错误,而且我不确定该怎么办

flutter - 如何在网络中使用 file_picker 显示选取的图像?

flutter - 将对象数组拆分为多个列表

dart - 如何在 Flutter 中为 Container 小部件自定义/旋转 BoxDecoration?

flutter - 如何在 Android 上运行风格为 iOS 的 Flutter 应用程序

firebase - 如果电子邮件和密码在 firebase flutter 中无效,如何显示错误消息?