Dictionary 使用地图在颤振中制作下拉菜单

Dictionary 使用地图在颤振中制作下拉菜单,dictionary,drop-down-menu,dart,flutter,Dictionary,Drop Down Menu,Dart,Flutter,因此,我想在flatter中创建一个对象,它将贴图作为一个值列表,其中字符串显示在菜单中,而int存储在类中,以便在选择某个项后稍后使用 我不知道怎么做。我找不到返回int值的部分 地图结构是这样写的: final Map<String, int> discBasis = { 'Age': 1, 'Ancestry': 2, 'Color': 3, 'Disability': 4, 'Race': 5, 'Religion': 6,

因此,我想在flatter中创建一个对象,它将
贴图
作为一个值列表,其中
字符串
显示在菜单中,而
int
存储在类中,以便在选择某个项后稍后使用

我不知道怎么做。我找不到返回
int
值的部分

地图结构是这样写的:

  final Map<String, int> discBasis = {
    'Age': 1,
    'Ancestry': 2,
    'Color': 3,
    'Disability': 4,
    'Race': 5,
    'Religion': 6,
    'Sex': 7,
    'Familial Status': 8,
    'National Origin': 9
  };
final Map discBasis={
"年龄":1,,
"血统":2,,
“颜色”:3,
“残疾”:4,
“种族”:5,
"宗教":6,,
"性":7,,
“家庭状况”:8,
‘国籍’:9
};
&我当前使用的类:

final double formMarginHoriz = 10.0;
final double formMarginVert = 5.0;

class DropDownFromMap extends StatefulWidget {
  Map<String, int> kv;
  DropDownFromMap(this.kv);

  @override
  DropDownFromMapState createState() => new DropDownFromMapState(kv);
}

class DropDownFromMapState extends State<DropDownFromMap> {
  Map<String, int> kv;
  DropDownFromMapState(this.kv);

  int selectedVal;

  @override
  Widget build(BuildContext context) {
    return new Container(
        margin: EdgeInsets.only(
            top: formMarginVert,
            bottom: formMarginVert,
            left: formMarginVert,
            right: formMarginHoriz),
        child: new DropdownButton<int>(
          hint: new Text("Select an option"),
          value: selectedVal,
          onChanged: (String newVal) {
            setState(() {
              selectedVal = kv[newVal];
            });
          },
          items: kv.map((String k, _) {
            return new DropdownMenuItem<String>(
              value: k,
              child: new Text(k),
            );
          }).toList(),
        ));
  }
}
final double formMarginHoriz=10.0;
最终双反拱=5.0;
类DropDownFromMap扩展StatefulWidget{
Map kv;
DropDownFromMap(该.kv);
@凌驾
DropDownFromMapState createState()=>新的DropDownFromMapState(kv);
}
类DropDownFromMapState扩展了状态{
Map kv;
MapState的压降(单位:千伏);
int-selectedVal;
@凌驾
小部件构建(构建上下文){
退回新货柜(
页边距:仅限边距(
顶部:formMarginVert,
底部:formMarginVert,
左:FormMarg反转,
右:formMarginHoriz),
孩子:新的下拉按钮(
提示:新文本(“选择选项”),
值:selectedVal,
onChanged:(字符串newVal){
设置状态(){
selectedVal=kv[newVal];
});
},
项目:千伏图(串k,U3){
返回新的DropdownMenuItem(
值:k,
儿童:新文本(k),
);
}).toList(),
));
}
}
它是这样实例化的(这可能是错误的):

@覆盖
小部件构建(构建上下文){
退回新材料(
颜色:Colors.blueAccent,
子容器:新容器(
儿童:新表格(
键:_formKey,
子:新列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
新的DropDownFromMap(data.offerType),
...
可以在github项目中找到更多上下文:在相关文件中:
下拉列表.dart
关闭页面.dart
,&
表单数据.dart


提前谢谢你!

我看不出你的代码有任何错误,只是你需要删除
State
对象中的构造函数和映射,而只将它们留在
StatefulWidget
本身。然后你可以使用
widget.kv
访问你的映射

用这个代替地图

最终列表=[
DropdownMenuItem(值:“”,子:容器(…您的小部件样式),子:文本(“选择”),
DropdownMenuItem(值:“value1”、child:Container((…您的小部件样式)、child:Text(“title1”),
DropdownMenuItem(值:“value2”、child:Container((…您的小部件样式)、child:Text(“title2”))
];
下拉按钮(
值:“”,
项目:清单,
一旦更改:(值){},
),
有了这个,你可以使用你自己的变体。我认为这是我最好的方式,因为我必须硬编码只有两个或三个项目才能显示在下拉列表中。你可以创建一个单独的模型,并使用这个想法生成任何类型的带有map的下拉列表。

静态常量map frequencyOptions={
static const Map<String, Duration> frequencyOptions = {
    "30 seconds": Duration(seconds: 30),
    "1 minute": Duration(minutes: 1),
    "2 minutes": Duration(minutes: 2),
  };

Duration _frequencyValue = Duration(seconds: 30);

@override
  Widget build(BuildContext context) {
    return DropdownButton<Duration>(
          items: frequencyOptions
              .map((description, value) {
                return MapEntry(
                    description,
                    DropdownMenuItem<Duration>(
                      value: value,
                      child: Text(description),
                    ));
              })
              .values
              .toList(),
          value: _frequencyValue,
          onChanged: (newValue) {
             setState(() {
                _frequencyValue = newValue;
             });
          },

        );
}
“30秒”:持续时间(秒数:30), “1分钟”:持续时间(分钟:1), “2分钟”:持续时间(分钟数:2), }; 持续时间_frequencyValue=持续时间(秒:30); @凌驾 小部件构建(构建上下文){ 返回下拉按钮( 项目:频率选项 .map((说明、值){ 返回映射条目( 描述 下拉菜单项( 价值:价值, 子:文本(描述), )); }) 价值观 .toList(), 值:_frequencyValue, 一旦更改:(newValue){ 设置状态(){ _frequencyValue=新值; }); }, ); }
final List<DropdownMenuItem> list = [
DropdownMenuItem(value: "", child: Container((...your widget style), child: Text("Select"))),
DropdownMenuItem(value: "value1", child: Container((...your widget style), child: Text("title1"))),
DropdownMenuItem(value: "value2", child: Container((...your widget style), child: Text("title2")))
];

DropdownButton(
              value: "",
              items: list,
              onChanged: (value) {},
            ),
static const Map<String, Duration> frequencyOptions = {
    "30 seconds": Duration(seconds: 30),
    "1 minute": Duration(minutes: 1),
    "2 minutes": Duration(minutes: 2),
  };

Duration _frequencyValue = Duration(seconds: 30);

@override
  Widget build(BuildContext context) {
    return DropdownButton<Duration>(
          items: frequencyOptions
              .map((description, value) {
                return MapEntry(
                    description,
                    DropdownMenuItem<Duration>(
                      value: value,
                      child: Text(description),
                    ));
              })
              .values
              .toList(),
          value: _frequencyValue,
          onChanged: (newValue) {
             setState(() {
                _frequencyValue = newValue;
             });
          },

        );
}