首页 文章

Flutter:SimpleDialog中的ListView

提问于
浏览
4

我想在我的Flutter应用程序中使用以下代码显示带有ListView.builder的SimpleDialog:

showDialog(
  context: context,
  builder: (BuildContext context) {
    return new SimpleDialog(
      children: <Widget>[
        new FittedBox(
          child: new ListView(
            children: <Widget>[
              new Text("one"),
              new Text("two"),
            ],
          ),
        )
      ],
    );
  },
);

这给出了这个错误(抱歉,我无法将日志包装为代码,因为Stackoverflow抱怨代码太多了):

= =╡通过渲染图书馆的例外情况╞══════════════════════════════════════════════════════════════════════════════════════════════════ ════════I / flutter(4481):在performLayout()期间抛出以下断言:I / flutter(4481):RenderViewport不支持返回内部维度 . I / flutter(4481):计算内在维度需要实例化视口的每个子节点,我/ flutter(4481):击败视口的懒惰点 . I / flutter(4481):如果你只是想在主轴方向上收缩包装视口,可以考虑一个I / flutter(4481):RenderShrinkWrappingViewport渲染对象(ShrinkWrappingViewport小部件),它实现了I / flutter(4481) :没有实现内在维度API的效果 . I / flutter(4481):...我/ flutter(4481):抛出了另一个异常:RenderBox没有布局:RenderPhysicalShape#83d92 relayoutBoundary = up2 NEEDS-PAINT I / flutter(4481):抛出另一个异常:' package:flutter / src / rendering / shifted_box.dart':断言失败:第310行pos 12:'child.hasSize':不是真的 . I / flutter(4481):抛出了另一个异常:RenderBox没有布局:RenderPhysicalShape#83d92 relayoutBoundary = up2

我尝试使用具有特定高度和宽度的Container,它可以工作,但我希望ListView能够适应Dialog .

How to include a ListView in a SimpleDialog?

4 回答

  • 0

    您可以在下面为SimpleDialogOptions代码创建单独的方法方法:

    final SimpleDialog dialog = new SimpleDialog(
          title: const Text('Select assignment'),
          children: <Widget>[
            new SimpleDialogOption(
              onPressed: () { Navigator.pop(context); },
              child: const Text('Text one'),
            ),
            new SimpleDialogOption(
              onPressed: () {},
              child: const Text('Text two'),
            ),
          ],
        );
        return dialog;
    
  • 0

    我找到了一种方法......虽然它有点hacky,所以可能有更好的选择 .

    你需要这个包:

    import 'package:flutter/services.dart';
    

    创建小部件:

    class MyDialog extends StatefulWidget {
      MyDialog ({Key key}) : super(key: key);
      MyDialogState createState() => new MyDialogState();
    }
    class MyDialogState extends State<MyDialog> {
    

    如果屏幕旋转,它会将物体拧紧,因为对话框保持原始大小 . 您可以通过一些努力来解决这个问题,但我只是将其锁定以防止旋转,如下所示:

    @override
      initState() { super.initState();
        SystemChrome.setPreferredOrientations([
          DeviceOrientation.portraitUp,
          DeviceOrientation.portraitDown,
        ]);
      }
    

    然后我在最后解锁它:

    @override
      dispose() { super.dispose();
        SystemChrome.setPreferredOrientations([
          DeviceOrientation.landscapeRight,
          DeviceOrientation.landscapeLeft,
          DeviceOrientation.portraitUp,
          DeviceOrientation.portraitDown,
        ]);
      }
    

    这样就可以阻止对话框搞砸了 . 然后我在构建方法中获得屏幕的大小和宽度:

    @override
    Widget build(BuildContext context) {
      double width = MediaQuery.of(context).size.width;
      double height = MediaQuery.of(context).size.height;
    

    然后是这个布局:

    return ConstrainedBox(
        constraints: BoxConstraints(maxHeight: height, maxWidth: width),
        child: Column(
          children: <Widget>[
            Expanded(
              child: GridView.count(
                primary: false,
                padding: const EdgeInsets.all(20.0),
                crossAxisSpacing: 10.0,
                crossAxisCount: 3,
                children: _images
              )
            ),
          ]
        ),
      );
    }
    

    ..我认为这不是最好的,但到目前为止它一直在为我工作 .

  • -1

    只需将 ListView.builder 变为 Container 并设置 heightwidthContainer .

    如下!

    Widget setupAlertDialoadContainer() {
        return Container(
          height: 300.0, // Change as per your requirement
          width: 300.0, // Change as per your requirement
          child: ListView.builder(
            shrinkWrap: true,
            itemCount: 5,
            itemBuilder: (BuildContext context, int index) {
              return ListTile(
                title: Text('Gujarat, India'),
              );
            },
          ),
        );
      }
    

    并在 showDialog 中调用上述方法 .

    showDialog(
       context: context,
       builder: (BuildContext context) {
       return AlertDialog(
           title: Text('Country List'),
           content: getAllSelectedCountry(),
       );
      }
    );
    
  • 0

    首先尝试使用itemExtent属性,但这不起作用 . 如果您的项目是静态的,只需将ListView包装在具有已定义高度和宽度的Container中 .

    showDialog(
      context: context,
      builder: (BuildContext context) {
        return new SimpleDialog(
          children: <Widget>[
            new Container(
              height: 100.0,
              width: 100.0,
              child: new ListView(
                children: <Widget>[
                  new Text("one"),
                  new Text("two"),
                ],
              ),
            )
          ],
        );
      },
    );
    

相关问题