gpt4 book ai didi

flutter - Flutter:如何绘制 map 列表?

转载 作者:行者123 更新时间:2023-12-03 04:36:24 25 4
gpt4 key购买 nike

我有一个非常简单的 map list 。

List<Map<String, String>> items = [
{ 'a': 'Some Text' },
{ 'b': 'Another Text' },
];
我想将上面的列表映射到一个下拉列表。
DropdownButton<String>(
hint: Text('Select a value'),
items: items.map((item) {
return DropdownMenuItem<String>(
value: // how to get key here a, b
child: // how to get value 'Some Text', 'Another Text'
);
}).toList(),
onChanged: (String newValue) {
setState(() {
// ...
});
},
),
)
如何获取 map 的 key ,item具有 keys属性,但没有 keyvalues,但没有 value

最佳答案

您可以在下面复制粘贴运行完整代码
您需要使用DropdownButton<Map<String, String>>您可以根据自己的要求调整Text("${value.keys.first} ${value.values.first}")程式码片段

DropdownButton<Map<String, String>>(
value: dropdownValue,
icon: Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
style: TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (Map<String, String> newValue) {
setState(() {
dropdownValue = newValue;

print(
"${dropdownValue.keys.first} ${dropdownValue.values.first}");
});
},
items: items.map<DropdownMenuItem<Map<String, String>>>(
(Map<String, String> value) {
return DropdownMenuItem<Map<String, String>>(
value: value,
child: Text("${value.keys.first} ${value.values.first}"),
);
}).toList(),
),
工作演示
enter image description here
完整的代码
import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
List<Map<String, String>> items = [
{'a': 'Some Text'},
{'b': 'Another Text'},
];

Map<String, String> dropdownValue;

int _counter = 0;

void _incrementCounter() {
setState(() {
_counter++;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
DropdownButton<Map<String, String>>(
value: dropdownValue,
icon: Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
style: TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (Map<String, String> newValue) {
setState(() {
dropdownValue = newValue;

print(
"${dropdownValue.keys.first} ${dropdownValue.values.first}");
});
},
items: items.map<DropdownMenuItem<Map<String, String>>>(
(Map<String, String> value) {
return DropdownMenuItem<Map<String, String>>(
value: value,
child: Text("${value.keys.first} ${value.values.first}"),
);
}).toList(),
),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}

关于flutter - Flutter:如何绘制 map 列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64023537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com