flutter - 如何从列表中创建 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 试过它给了我错误。有人可以用另一种方法帮助我或解决此错误。谢谢!!
解决方案
尝试使用这样的列。
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;
}
}
推荐阅读
- swift - 从一个信号发送 voip 推送通知
- c++ - 如何在不使用 pthread_join() 的情况下同步线程?
- swift - SwiftUI - 如何仅在一个屏幕上自定义导航栏?
- c# - 无法序列化 ExtensionDataObject
- python - 我正在尝试从 espn 中提取一些数据作为表格并将其作为列表
- node.js - 在 Node.js 中定位线程泄漏
- java - 使用假设角色同时访问属于不同帐户的两个 dynamodb 表不起作用
- java - 在 plugin.yml 中找不到主类
- visual-studio-code - 如何在 Visual Studio 代码中为 C++ 设置默认代码?
- java - 异常 MyExc3 永远不会在相应的 try 语句的主体中抛出