ModalBottomSheet 中的 CupertinoDatePicker

发布于 2025-01-09 01:49:13 字数 1488 浏览 0 评论 0原文

无法设法将 CupertinoDatePicker 放入 Flutter 的底部模式中。

获取以下信息: RenderCustomMultiChildLayoutBox 对象在布局期间被赋予无限大小。

...

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

我对 Flutter 太陌生,无法理解如何解决这个问题:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ら栖息 2025-01-16 01:49:13

问题是 CupertinoDatePicker 小部件在渲染期间获得无限大小。要解决这个问题,您可以使用 Container 小部件或 SizedBox 小部件包装该小部件,并为其指定一个 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),
              ),
            )
...

另一种方法是包装带有 Expanded 小部件的 CupertinoDatePickerExpanded 小部件使其子部件填充可用空间(而不是尝试填充无限高度)。

...

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

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),
              ),
            )
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文