RenderShrinkWrappingViewport不支持返回固有尺寸。

3
我想要一个文本按钮,当点击时显示一个简单的对话框和一个listView.builder,但我不知道如何编写代码。我总是会出现错误。你能帮我吗?
这是我的代码:
TextButton(
    child: const Text('Selet instruments needed'),
    onPressed: () {
      showDialog(
          context: context,
          builder: (BuildContext context) => SimpleDialog(
                  contentPadding: const EdgeInsets.all(15),
                  title: const Text('Select instruments needed'),
                  children: [
                    ListView.builder(
                        shrinkWrap: true,
                        itemCount: 2,
                        itemBuilder: ((context, index) {
                          return ListTile(
                              title: instrumentType[index]['name'],
                              onTap: () {});
                        }))
                  ]));
    })
3个回答

3
你可以使用SizedBox将你的ListView包装起来,并使用LayoutBuilder来获取约束条件。
  TextButton(
        child: const Text('Selet instruments needed'),
        onPressed: () {
          showDialog(
            context: context,
            builder: (BuildContext context) => LayoutBuilder(
              builder: (context, constraints) => SimpleDialog(
                contentPadding: const EdgeInsets.all(15),
                title: const Text('Select instruments needed'),
                children: [
                  SizedBox(
                    height: constraints.maxHeight * .7, // 70% height
                    width: constraints.maxWidth * .9,
                    child: ListView.builder(
                      physics: NeverScrollableScrollPhysics(),
                      itemCount: 44,
                      itemBuilder: ((context, index) {
                        return ListTile(onTap: () {});
                      }),
                    ),
                  )
                ],
              ),
            ),
          );
        })

很高兴能帮忙,如果你使用AlertDialog,你可以有滚动参数,并且你可以在那里使用Column - Yeasin Sheikh

0
尝试以下代码:
void _show(BuildContext ctx) {
    showDialog(
      context: ctx,
      builder: (_) {
        return SimpleDialog(
          title: const Text('The Title'),
          children: [
            SimpleDialogOption(
              child: const Text('Option 1'),
              onPressed: () {
                // Do something
                print('You have selected the option 1');
                Navigator.of(ctx).pop();
              },
            ),
            SimpleDialogOption(
              child: const Text('Option 2'),
              onPressed: () {
                // Do something
                print('You have selected the option 2');
                Navigator.of(ctx).pop();
              },
            )
          ],
        );
      },
    );
  }

调用对话框函数:

TextButton(
          child: const Text('Show The Simple Dialog'),
          onPressed: () => _show(context),
        ),

0
  • 在尝试在对话框(Dialog)中添加Stepper()时遇到了这个问题。

  • 由于Stepper的行为类似于ListView,所以问题是相同的。

  • 简单的解决方案是将Stepper(或ListView)包装在SizedBox中。

  • 找出适合所有可能设备的正确宽度和高度是不可行的,因此我们可以使用width: double.maxFiniteheight: double.maxFinite,让Flutter自己来解决。

    showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text("测试"),
          content: SizedBox(
            width: double.maxFinite,
            height: double.maxFinite,
            child: Stepper(
              currentStep: x, // 您的Stepper索引
              steps: [
                Step(...),
                Step(...),
              ],
              .... // 其他Stepper属性
            ),
          ),
        );
      }
    );
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接