在 Flutter 中使用 Map 制作下拉菜单

arc*_*era 6 dictionary dart drop-down-menu flutter

所以我想在 flutter 中创建一个对象,该对象将 aMap<String, int>作为值列表,其中String显示在菜单中的内容,并将其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
  };
Run Code Online (Sandbox Code Playgroud)

&我目前正在使用的课程:

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(),
        ));
  }
}
Run Code Online (Sandbox Code Playgroud)

它是这样实例化的(这可能是错误的):

  @override
  Widget build(BuildContext context) {
    return new Material(
        color: Colors.blueAccent,
        child: new Container(
            child: new Form(
                key: _formKey,
                child: new Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    new DropDownFromMap(data.offenderType),
...
Run Code Online (Sandbox Code Playgroud)

更多上下文可以在 github 项目中找到:https : //github.com/chscodeforchange/icrc-app在相关文件中:dropdown.dart, off_page_one.dart, & form_data.dart

先感谢您!

Val*_*ova 10

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: (Duration? newValue) {
            if (newValue != null) {
              setState(() {
                _frequencyValue = newValue;
              });
            }
          },
        );
}
Run Code Online (Sandbox Code Playgroud)


azi*_*iza 0

我没有看到您的代码有任何问题,除了您需要删除对象中的构造函数和映射State,并且只将它们保留在其StatefulWidget自身内部。然后您可以使用以下方式访问您的地图widget.kv