如何从列表创建SimpleDialogueOptions? |扑

时间:2020-04-18 10:52:20

标签: flutter dart

我想以simpledialogoptions的形式创建菜单列表。我尝试的方法给我错误:

The following assertion was thrown during paint():
RenderBox was not laid out: RenderPhysicalShape#3955f relayoutBoundary=up2
'package:flutter/src/rendering/box.dart':
Failed assertion: line 1687 pos 12: 'hasSize'


Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=BUG.md

我的代码:


  List<String> category = ['Not Specified', 'Cats'];

  Future<void> showCategories() async {
    switch (await showDialog(
        context: context,
        builder: (BuildContext context) {
          return SimpleDialog(
            shape:
                RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
            title: Text(
              'Select a Category',
              style: TextStyle(color: Colors.redAccent),
            ),
            children: <Widget>[
              ListView.builder(itemBuilder: (context, categoryListIndex) {
                return SimpleDialogOption(
                  child: ListTile(
                    leading: Icon(Icons.bookmark),
                    title: Text(category[categoryListIndex]),
                  ),
                  onPressed: () {
                    Navigator.pop(context, category[categoryListIndex]);
                  },
                );
              }),
            ],
          );
        })) {
      case 'Not Specified':
        print('Option 1');
        break;
      case 'Cats':
        print('Option 2');
        break;
    }
  }

我用ListView构建器尝试过,它给我错误。有人可以帮我解决另一种方法还是解决此错误。谢谢!

2 个答案:

答案 0 :(得分:0)

尝试使用类似这样的列。


  Future<void> showCategories(context) async {
    var result = await showDialog(
      context: context,
      builder: (BuildContext context) {
        return SimpleDialog(
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
          title: Text(
            'Select a Category',
            style: TextStyle(color: Colors.redAccent),
          ),
          children: <Widget>[
            Column(
              children: List.generate(
                category.length,
                (index) {
                  return SimpleDialogOption(
                    child: ListTile(
                      leading: Icon(Icons.bookmark),
                      title: Text(category[index]),
                    ),
                    onPressed: () {
                      Navigator.pop(context, category[index]);
                    },
                  );
                },
              ),
            ),
          ],
        );
      },
    );

    switch(result){
      case 'Not Specified':
        print("Option 1");
        break;
      case 'Cats':
        print("Option 2");
        break;
    }
  }

答案 1 :(得分:0)

尝试一下,让我知道这是否有助于在您的itemCount上放置ListView.builder,但它不知道要渲染多少个小部件。

Future<void> showCategories() async {
    switch (await showDialog(
        context: context,
        builder: (BuildContext context) {
          return SimpleDialog(
            shape:
                RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
            title: Text(
              'Select a Category',
              style: TextStyle(color: Colors.redAccent),
            ),
            children: <Widget>[
              ListView.builder(
                shrinkWrap: true,
                itemCount: categoryListIndex.length,
                itemBuilder: (context, categoryListIndex) {
                return SimpleDialogOption(
                  child: ListTile(
                    leading: Icon(Icons.bookmark),
                    title: Text(category[categoryListIndex]),
                  ),
                  onPressed: () {
                    Navigator.pop(context, category[categoryListIndex]);
                  },
                );
              }),
            ],
          );
        })) {
      case 'Not Specified':
        print('Option 1');
        break;
      case 'Cats':
        print('Option 2');
        break;
    }
  }