简体   繁体   English

展开的小部件抛出“RenderBox 未布局”异常

[英]Expanded widget throws "RenderBox was not laid out" exception

Hi I'm new to Flutter and I got in trouble with the exception "RenderBox was not laid out".嗨,我是 Flutter 的新手,我遇到了异常“RenderBox 未布局”的麻烦。 I'm trying to create MyDialog which is an extension of the built-in dialog.我正在尝试创建MyDialog ,它是内置对话框的扩展。 It can be injected contents from other widget.它可以从其他小部件中注入内容。 And I wanna place Expanded widget wrapping Text widget as the contents so that all text can be seen with line breaks.我想将Expanded widget wrapping Text widget 作为内容放置,以便可以看到所有带有换行符的文本。

I intent the contents show like this:我打算这样显示内容: 在此处输入图像描述

This is my MyDialog widget.这是我的MyDialog小部件。

class MyDialog extends StatelessWidget {
  String label;
  Widget contents;
  const MyDialog({required this.label, required this.contents, Key? key}) : super(key : key)

  @override
  Widget build(BuildContext context) {
    return MyButtonTip(    // This is an extension of outlined button.
      label     : label,
      onPressed : () {
        showDialog(
          context : context,
          builder : (BuildContext context) => AlertDialog(
            insetPadding    : const EdgeInsets.all(15),
            content         : SizedBox(
              width : MediaQuery.of(context).size.width,
              child : contents
            )
          )
        );
      }
    );
  }
}

And this is the widget that uses MyDialog .这是使用MyDialog的小部件。

...
MyDialog(
  label : 'Profile',
  contents : SizedBox(
    child : Stack(
      children : [
        Column(
          children : [
            Row(
              children : [
                Column(
                  crossAxisAlignment : CrossAxisAlignment.end,
                  children           : const [
                    Text('name'),
                    Text('nickname')
                  ]
                ),
                const SizedBox(width : 10),
                Column(
                  crossAxisAlignment : CrossAxisAlignment.start,
                  children           : [
                    Text(name),
                    Expanded(
                      child : Text(nickname)
                    )
                  ]
                )
              ]
            )
          ]
        )
      ]
    ))
  )
);
...

(I'm using Stack for aiming something different, so please don't care about it.) (我正在使用Stack来瞄准不同的东西,所以请不要关心它。)

I would like to know the correct solution, as I have encountered this exception many times before and each time it has somehow been resolved.我想知道正确的解决方案,因为我之前多次遇到此异常,并且每次都以某种方式解决了。 Please let me know in detail.请详细告诉我。

Thanks,谢谢,

PS.附言。 console:安慰: 在此处输入图像描述

As my opinion you placed widget wrong please try with below code:我认为您将小部件放错了,请尝试使用以下代码:

    MyDialog(
  label : 'Profile',
  contents : Column(children: [
          Row(children: const [
            Text('name : '),
            Text("hello"),
          ]),
          const SizedBox(width: 10),
          Row(crossAxisAlignment: CrossAxisAlignment.start, children: const [
            Text('taro yamuda : '),
            Expanded(
              child: Text(
                "someting sometingsometingsometingsometingsometingsometingsometingsometingsometingsometingsometingsometing",
                style: TextStyle(),
              ),
            )
          ])
        ]);
      
      
      }

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM