首页 > 解决方案 > 如何从列表中创建 SimpleDialogueOptions ?| 扑

问题描述

我想以 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 builder 试过它给了我错误。有人可以用另一种方法帮助我或解决此错误。谢谢!!

标签: flutterdart

解决方案


尝试使用这样的列。


  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;
    }
  }

推荐阅读