'CupertinoDatePicker in ModalBottomSheet

Can't manage to put the CupertinoDatePicker into the bottom modal in Flutter.

Getting the following: RenderCustomMultiChildLayoutBox object was given an infinite size during layout.

...

          Padding(
              padding: const EdgeInsets.symmetric(horizontal: 2.0),
              child: IconButton(
                onPressed: () {
                  showModalBottomSheet(
                      context: context,
                      builder: (context) {
                        return Column(
                          children: [
                            CupertinoDatePicker(
                              mode: CupertinoDatePickerMode.date,
                              initialDateTime: DateTime.now(),
                              onDateTimeChanged: (DateTime dateTime) {
                                // tbd
                              },
                            ),
                            ElevatedButton(onPressed: (){}, child: Text("save"))
                          ],
                        );
                      });
                },
                icon: const FaIcon(FontAwesomeIcons.calendar, size:30, color: Colors.orange),
              ),
            )
...

I too new to Flutter to understand how I work around this problem :

This probably means that it is a render object that tries to be as big as possible, but it was put inside another render object that allows its children to pick their own size.

...

The relevant error-causing widget was: 
  CupertinoDatePicker CupertinoDatePicker



Solution 1:[1]

The problem is the CupertinoDatePicker widget gets an infinite size during rendering. To solve that you could wrap the widget with a Container widget, or a SizedBox widget, and giving it a height:

...

          Padding(
              padding: const EdgeInsets.symmetric(horizontal: 2.0),
              child: IconButton(
                onPressed: () {
                  showModalBottomSheet(
                      context: context,
                      builder: (context) {
                        return Column(
                          children: [
                            Container(
                              height: 300, // Just as an example
                              child: CupertinoDatePicker(
                                mode: CupertinoDatePickerMode.date,
                                initialDateTime: DateTime.now(),
                                onDateTimeChanged: (DateTime dateTime)   {
                                // tbd
                                },
                              ),
                            ),
                            ElevatedButton(onPressed: (){}, child: Text("save"))
                          ],
                        );
                      });
                },
                icon: const FaIcon(FontAwesomeIcons.calendar, size:30, color: Colors.orange),
              ),
            )
...

An alternative is to wrap the CupertinoDatePicker with an Expanded widget. An Expanded widget makes its child fill the available space (instead of trying to fill an infinite height).

...

          Padding(
              padding: const EdgeInsets.symmetric(horizontal: 2.0),
              child: IconButton(
                onPressed: () {
                  showModalBottomSheet(
                      context: context,
                      builder: (context) {
                        return Column(
                          children: [
                            Expanded(
                              child: CupertinoDatePicker(
                                mode: CupertinoDatePickerMode.date,
                                initialDateTime: DateTime.now(),
                                onDateTimeChanged: (DateTime dateTime)   {
                                // tbd
                                },
                              ),
                            ),
                            ElevatedButton(onPressed: (){}, child: Text("save"))
                          ],
                        );
                      });
                },
                icon: const FaIcon(FontAwesomeIcons.calendar, size:30, color: Colors.orange),
              ),
            )
...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1