简体   繁体   中英

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". I'm trying to create MyDialog which is an extension of the built-in dialog. 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.

I intent the contents show like this: 在此处输入图像描述

This is my MyDialog widget.

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(
  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.)

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(),
              ),
            )
          ])
        ]);
      
      
      }

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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