是否有可能在Flutter中获得DropDown项目的索引

时间:2020-04-04 06:42:10

标签: flutter

这是下拉菜单的项目列表。更改/选择项目时是否可以检索索引?这是通过API调用从Future方法接收数据的小部件。数据以显示的有益JSON文件的形式接收。

  Widget _buildList() {
    Size size = MediaQuery.of(context).size;
    return FutureBuilder<Parameters>(
      future: _futureParams,
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return ListView.builder(
            shrinkWrap: true,
            itemCount: 1,
            itemBuilder: (context, i) {
              return Form(
                key: formKey,
                child: Column(
                  children: <Widget>[
                    Container(
                      padding: EdgeInsets.fromLTRB(40, 0, 40, 0),
                      height: 50.0,
                      child: DropdownButtonFormField<String>(
                        decoration: InputDecoration(
                            contentPadding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                            filled: true,
                            fillColor: Theme.Colors.color_7,
                            hintText: 'Select a Profession',
                            hintStyle: TextStyle(fontWeight: FontWeight.bold)),
                        value: _listdownValue,
                        icon: Icon(Icons.arrow_drop_down),
                        iconSize: 24,
                        elevation: 16,
                        style: TextStyle(color: Colors.black),
                        onChanged: (String newValue) {
                          setState(() {
                            _listdownValue = newValue;
                            // int j =
                            //     listAsMap.indexOf(listAsMap.where(listAsMap[i].service== newValue));
                            // price = listAsMap[j].price.match();
                            // print(price);
                          });
                        },
                        validator: (value) {
                          if (value == null) {
                            return "Select a Profession";
                          }
                          return null;
                        },
                        items: snapshot.data.services.map((item) {
                          return new DropdownMenuItem(
                            child: new Text(item.service),
                            value: item.service.toString(),
                          );
                        }).toList(),
                      ),
                    ),
                    SizedBox(
                      height: size.width * 0.05,
                    ),
                    Container(
                      padding: EdgeInsets.fromLTRB(60, 0, 60, 0),
                      height: 50.0,
                      child: DropdownButtonFormField<String>(
                        decoration: InputDecoration(
                            contentPadding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                            filled: true,
                            fillColor: Theme.Colors.color_7,
                            hintText: 'Select an Area',
                            hintStyle: TextStyle(fontWeight: FontWeight.bold)),
                        validator: (value) {
                          if (value == null) {
                            return "Select your Area";
                          }
                          print(i);
                          return null;
                        },
                        value: _dropdownValue,
                        icon: Icon(Icons.arrow_drop_down),
                        iconSize: 24,
                        elevation: 16,
                        style: TextStyle(color: Colors.black),
                        onChanged: (String newValue) {
                          setState(() {
                            _dropdownValue = newValue;
                          });
                        },
                        items: snapshot.data.locations.map((item) {
                          return new DropdownMenuItem(
                            child: new Text(item.subCounty ?? ''),
                            value: item.subCounty.toString(),
                          );
                        }).toList(),
                      ),
                    ),
                    SizedBox(
                      height: size.width * 0.05,
                    ),
                    RaisedButton(
                      elevation: 5.0,
                      shape: new RoundedRectangleBorder(
                          borderRadius: new BorderRadius.circular(0.0)),
                      color: Theme.Colors.color_8,
                      child: Container(
                        height: 40.0,
                        width: 80,
                        child: Center(
                          child: const Text('Search',
                              style: TextStyle(
                                  fontSize: 20,
                                  fontWeight: FontWeight.bold,
                                  color: Theme.Colors.color_7)),
                        ),
                      ),
                      onPressed: () async {
                        setState(() {
                          if (formKey.currentState.validate()) {
                            _futureSearch = createSearch(
                                this._dropdownValue.toString(),
                                this._listdownValue.toString(),
                                0);
                            saveSearch(searchP);
                            state = true;
                          }
                        });

                        print(this._listdownValue + "\ " + this._dropdownValue);
                      },
                    ),
                    SizedBox(
                      height: size.width * 0.05,
                    ),
                  ],
                ),
              );
            },
          );
        }
        return Container(
          height: 150,
        );
      },
    );
  }

我希望在用户选择服务时获得价格。我不能直接将值设置为price,因为我还需要service的字符串值。

{
        "services": [
            {
                "price": 3,
                "service": "Caregiver"
            },
            {
                "price": 5,
                "service": "Driver"
            },
            {
                "price": 0,
                "service": "Tipper Driver"
            }
        ],
        "locations": [
            {
                "sub_county": "All of NY"
            },
            {
                "sub_county": "Brkln"
            }
        ]
    }

1 个答案:

答案 0 :(得分:0)

  getDropDownMenuItems(snapshot) {
    Map<int, dynamic> mapWithIndex = snapshot.data.services.asMap;
    List<DropdownMenuItem> _items = [];
    mapWithIndex.forEach((index, item) {
      _items.add(
        DropdownMenuItem<int>(
          value: index,
          child: Text(item.service),
        ),
      );
    });
    return _items;
  }

通过传递这样的快照来调用此方法

items: getDropDownMenuItems(snapshot)