Flutter: ListView in a SimpleDialog

后端 未结 9 1632
深忆病人
深忆病人 2020-12-24 01:04

I want to show a SimpleDialog with ListView.builder in my Flutter app with this code:

showDialog(
  context: context,
  builder: (BuildContext context) {
           


        
相关标签:
9条回答
  • 2020-12-24 01:48

    Wraping the ListView in a Container and giving it a width: double.maxFinite, solves the problem with iOS/Android having trouble with ListView inside a dialog:

    showDialog(
       context: context,
       builder: (BuildContext context) {
          return AlertDialog(
             content: Container(
                width: double.maxFinite,
                child: ListView(
                   children: <Widget>[]
                ),
             ),
          );
       }
    );
    

    In the case of a ListView inside a Column that is inside an AlertDialog:

    showDialog(
       context: context,
       builder: (BuildContext context) {
          return AlertDialog(
             content: Container(
                width: double.maxFinite,
                child: Column(
                   mainAxisSize: MainAxisSize.min,
                   children: <Widget>[
                      Expanded(
                         child: ListView(
                            shrinkWrap: true,
                            children: <Widget>[]
                         )
                      )
                   ]
                ),
             ),
          );
       }
    );
    
    0 讨论(0)
  • 2020-12-24 01:50

    Tried it with itemExtent property first, but that doesn't work. Simply wrap the ListView in a Container with defined height and width, if your items are static.

    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"),
                ],
              ),
            )
          ],
        );
      },
    );
    
    0 讨论(0)
  • 2020-12-24 01:58

    you can create a separate method method for SimpleDialogOptions code below:

    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 讨论(0)
提交回复
热议问题