小部件的卡上的高度高度颤动

发布于 2025-02-13 12:13:42 字数 1051 浏览 0 评论 0原文

我想根据其中的小部件的高度设置卡的高度。 我使用globalKey并将其设置在我的小部件中,但返回null。

谁能帮我怎么做?

 final GlobalKey _Key = GlobalKey();


 showModalBottomSheet(
                        context: context,
                        useRootNavigator: true,
                        isScrollControlled: true,
                        isDismissible: true,
                        enableDrag: true,
                        builder: (context) {
                          return Container(
                            key : _key,
                            child: ListView(
                              shrinkWrap: true,
                              padding: EdgeInsets.zero,
                              children: [
                                PressWidget(),
                                PaddingButtonCard(),
                              ],
                            ),
                          );
                        },
                      );

当我打印_key.currentContext?.findRenderObject()。size时,返回null。

I want to set the height for my cards based on the height of the widgets in it.
I use globalKey and set it in my widget but return null.

can anyone help me how can I do that?

 final GlobalKey _Key = GlobalKey();


 showModalBottomSheet(
                        context: context,
                        useRootNavigator: true,
                        isScrollControlled: true,
                        isDismissible: true,
                        enableDrag: true,
                        builder: (context) {
                          return Container(
                            key : _key,
                            child: ListView(
                              shrinkWrap: true,
                              padding: EdgeInsets.zero,
                              children: [
                                PressWidget(),
                                PaddingButtonCard(),
                              ],
                            ),
                          );
                        },
                      );

when I print _Key.currentContext?.findRenderObject().size , return null.

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

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

发布评论

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

评论(1

成熟的代价 2025-02-20 12:13:43

诸如卡之类的小部件应自动包装它的内容,因此,除非内容物的高度不受限制(例如listView展开的 widget),否则不需要测量或这样的测量实现这一目标的技术。

在您的card> card中,尝试,作为测试,将容器放置一个 ,并观察卡的自动自动尺寸:

Card(
  child: Container(
    height: 40,
    width: double.maxFinite,
    color: Colors.purple,
    child: Text('Purple Card'),
  ),
),

A Widget like a card should automatically wrap it's contents, so unless the contents have an unconstrained height (like a ListView or Expanded widget), you shouldn't need measuring or such techniques to achieve this.

Try, as a test, putting a Container with a set height inside your Card, and observe how the card automatically sizes itself:

Card(
  child: Container(
    height: 40,
    width: double.maxFinite,
    color: Colors.purple,
    child: Text('Purple Card'),
  ),
),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文