我想制作一個 ListView,它顯示 Map Literal 中的所有圖示及其名稱......但我不知道如何在 ListView.builder 中獲取鍵和值
是否有任何映射鍵和值的索引工具...
這是我的代碼
class _HomeScreenState extends State<HomeScreen> {
Map<String, IconData> icons = {
'ac_unit': Icons.ac_unit,
'ac_unit_sharp': Icons.ac_unit_sharp,
'ac_unit_rounded': Icons.ac_unit_rounded,
'ac_unit_outlined': Icons.ac_unit_outlined,
'access_alarm': Icons.access_alarm
};
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: icons.length,
itemBuilder: (context,index)
{
return Padding(
padding: EdgeInsets.symmetric(vertical: 10.0),
child: ListTile(
onTap: (){},
leading: CircleAvatar(
backgroundColor: Colors.blue,
child: Icon(//how to fetch Icon from map),
title: Text('How to fetch icon name from map',style: TextStyle(fontSize: 30.0,color: Colors.blue),),
),
);
}),
),
],
),
),
);
}
}
uj5u.com熱心網友回復:
dart 中的 Map 有兩個迭代器屬性,命名鍵和值
keys 包含地圖中的每個鍵
與值相同,它包含地圖內的所有值
你可以得到這樣的圖示:
//Store the icons in a list like this
var list = icons.values.toList();
//And access it individually like this
Icon(list[index]);
在您的情況下,您希望從地圖中獲取圖示名稱和圖示值。所以,你做這樣的事情:
var data = icons.entries.toList();
//Access key like this
data[index].key
//Access value like this
data[index].value
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/486628.html
