Incorrect use of ParentDataWidget (No stacktrace)

Issue

I have the following layout for an alert dialog:

AlertDialog(
        title: Text('Some Text'),
        content: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
          return Container(
              height: MediaQuery.of(context).size.height,
              width: MediaQuery.of(context).size.width,
              child: ListView(
                children: [
                    ListView.builder(
                    shrinkWrap: true,
                    physics: NeverScrollableScrollPhysics(),
                    itemCount: usersWithoutBirthdays.length,
                    itemBuilder: (BuildContext context, int index) {
                      return CheckboxListTile(
                          title: Text("More Text Here"),
                          value: someValue,
                          onChanged: (bool? value) {
                            if (value != null) {
                              setState(() {
                                //Set state logic here
                              });
                            }
                          }
                        );
                      },
                    ),
                    Spacer(),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        TextButton(
                          child: Text("Cancel"),
                          onPressed: () {
                            Navigator.pop(context);
                          },
                        ),
                        TextButton(
                            child: Text("Continue"),
                            onPressed:
                            //OnPressed logic here
                        ),
                      ],
                    )
                ],
              )
            );
          }
        )
    );

In the console all I see is:

════════ Exception caught by widgets library
═══════════════════════════════════════════════════════ Incorrect use
of ParentDataWidget.

This happens every time I scroll down within the alert when it is shown.

I have looked at similar questions (here, here) and have tried the following solutions:

  • Wrapping the Container within a column and the ListView inside of an Expanded widget
  • Just wrapping the ListView within an Expanded widget

As you can see, there is no Expanded widget as part of the ListView’s layout and since the stacktrace is not informative, I don’t understand where the problem lies.

Should I have laid out the alert dialog differently? What I set to accomplish is have a layout that is scrollable and has a list of checkboxes the user can mark.

Ignoring this error, all the functionality works and there is no other issue.

Solution

Remove Spacer Widget from your code. Try below code hope its help to you.

bool variable for test checked and unchecked CheckBox

  bool someValue = false;

Your Widget:

  ElevatedButton(
        onPressed: () {
          // write your onPressed function here
          alertDialog();
          print('Button Pressed');
        },
        child: const Text('Press Me'),
      ),

Your alertDialog method:

alertDialog() {
    showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text('Some Text'),
          content: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              return Container(
                height: MediaQuery.of(context).size.height,
                width: MediaQuery.of(context).size.width,
                child: ListView(
                  children: [
                    ListView.builder(
                      shrinkWrap: true,
                      physics: NeverScrollableScrollPhysics(),
                      itemCount: 30,//usersWithoutBirthdays.length,
                      itemBuilder: (BuildContext context, int index) {
                        return CheckboxListTile(
                            title: Text("More Text Here"),
                            value: someValue,
                            onChanged: (bool value) {
                              if (value != null) {
                                setState(() {
                                  //Set state logic here
                                });
                              }
                            });
                      },
                    ),
                  ],
                ),
              );
            },
          ),
          actions: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                TextButton(
                  child: Text("Cancel"),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                ),
                TextButton(
                  child: Text("Continue"),
                  onPressed: () {},
                  //OnPressed logic here
                ),
              ],
            ),
          ],
        );
      },
    );
  }

You can test your code Dartpad

Answered By – Ravindra S. Patil

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.