简体   繁体   中英

ListView rendering error on GetX's Get.defaultDialog and Get.dialog

If we use Flutter's default AlertDialog and showDialog, we don't encounter any issues with the ListView wrap.

return AlertDialog(
              title: Text("Title of Dialog"),
              content: SizedBox(
                width: double.minPositive, 
                child: ListView.builder(
                  shrinkWrap: true, 
                  ...
                  itemBuilder: (BuildContext context, int index) {
                    ...
                  },
                ),
              ),
            );

The problem comes when we use Dialog from GetX , are Get.defaultDialog or Get.dialog . While debugging, an error message appears: "Cannot hit test a render box with no size.' The error can be removed by adding a size height and width for example:

content: SizedBox(
         width: 300,
         heights: 300,
...
)

But the problem is not finished, because a lot of empty space will appear, where the contents of the ListView are smaller than all the widgets in the Dialog.

在此处输入图像描述

I managed to solve the problem using this way:

I wrapped the ListView.Builder() in Flexible and then I wrapped Flexible In Column widget with MainaxisSize property set to Minimium .

Here's the example code you can try:

Get.dialog(
        Center(
          child: Container(
              padding: const EdgeInsets.all(20),
              width: 200,
              color: Colors.amber,
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Flexible(
                      child: ListView.builder(
                        itemCount: 30,
                        shrinkWrap: true,
                        itemBuilder: (ctx, index) {
                          return Center(
                            child: Text("demo"),
                          );
                        },
                      ),
                    ),
                  ],
                ),
              ),
            ),
          );

If the itemcount is less then it will shrink the dialog too.

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