How to make dependent multilevel DropDown in flutter?

后端 未结 2 1843
忘掉有多难
忘掉有多难 2020-12-18 09:02

I am trying to make dependent multilevel dropdown first contains states list and second contains cities list, all the data fetched from API. Initially, I load state dropdown

2条回答
  •  旧巷少年郎
    2020-12-18 09:15

    I am also facing a problem until new state data is not fetched It is showing previous state data. The approach I have used is different. I am not using future Builders. Here is My code:

                   Container(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          new Expanded(
                            child: new Container(
                              width: 450,
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.start,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    "Source",
                                    style: TextStyle(
                                        fontSize: 15, fontWeight: FontWeight.bold),
                                  ),
                                  source1 != null ? DropdownButtonFormField(
                                    isExpanded: true,
                                    validator: (value) => value == null ? 'field required' : null,
                                    hint: Text("Select Source"),
                                    items: source1.data.map((item) {
                                      // print("Item : $item");
                                      return DropdownMenuItem(
                                        value: item.descr,
                                        child: Text(item.descr),
                                      );
                                    }).toList(),
                                    onChanged: (String cat) {
                                      setState(() {
                                        subsourseStr = null;
                                        sourceStr = cat;
                                        getSubSource2(sourceStr);
                                      });
                                    },
                                    value: sourceStr,
                                  ):SizedBox(height: 10),
                                ],
                              ),
                            ),
                          )
                        ],
                      ),
                    ),
    //
                    Container(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          new Expanded(
                            child: new Container(
                              width: 450,
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.start,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    "Sub Source",
                                    style: TextStyle(
                                        fontSize: 15, fontWeight: FontWeight.bold),
                                  ),
                                  subSource2 != null ? DropdownButtonFormField(
                                    isExpanded: true,
                                    validator: (value) => value == null ? 'field required' : null,
                                    hint: Text("Select Sub Source"),
                                    items: subSource2.data.map((item) {
                                      return DropdownMenuItem(
                                        value: item.descr,
                                        child: Text(item.descr),
                                      );
                                    }).toList(),
                                    onChanged: (String cat) {
                                      setState(() {
                                        subsourseStr = cat;
                                      });
                                    },
                                    value: subsourseStr,
                                  ):SizedBox(height: 10,),
                                ],
                              ),
                            ),
                          )
                        ],
                      ),
                    ),
    

提交回复
热议问题