DropdownMenuItem出现颤振问题

时间:2019-02-23 18:39:05

标签: dart flutter

我在Flutter DropdownMenuItem上遇到了麻烦,它可以完美地加载状态,但是经常会因错误而加载城市,请问我缺少什么?我已经花了好几天了。

enter image description here

enter image description here

States selectedState;
Cities selectedCity;

Widget stateDropDown(AddAddressBloc bloc) => StreamBuilder(
    stream: bloc.statesResult,
    initialData: [],
    builder: (BuildContext context, AsyncSnapshot snapshot) {
      return Container(
          decoration: BoxDecoration(
              border: Border.all(color: Colors.grey, width: 1.2),
              borderRadius: BorderRadius.all(Radius.circular(5))),
          padding: EdgeInsets.only(left: 5, right: 5),
          width: MediaQuery.of(context).size.width,
          child: DropdownButtonHideUnderline(
              child: DropdownButton(
            value: selectedState == null ? null : selectedState.id,
            items: new List.generate(snapshot.data.length, (int index) {
              return DropdownMenuItem<int>(
                value: snapshot.data[index].id,
                child: new Text(snapshot.data[index].name),
              );
            }),
            onChanged: (int id) {
              bloc.fetchStateById(id);
              List<States> s =
                  snapshot.data.where((s) => s.id == id).toList();
              setState(() {
                selectedState = s[0];
              });
            },
            hint: Text("Select State"),
            isExpanded: true,
            style: Theme.of(context).textTheme.subtitle,
          )));
    },
);

Widget cityDropDown(AddAddressBloc bloc) => StreamBuilder(
  stream: bloc.citiesResult,
  initialData: [],
  builder: (BuildContext context, AsyncSnapshot snapshot) {
    return snapshot.hasData
        ? Container(
            decoration: BoxDecoration(
                border: Border.all(color: Colors.grey, width: 1.2),
                borderRadius: BorderRadius.all(Radius.circular(5))),
            padding: EdgeInsets.only(left: 5, right: 5),
            child: DropdownButtonHideUnderline(
                child: DropdownButton<dynamic>(
              value: (selectedCity == null || selectedCity.id == null)
                  ? null
                  : selectedCity.id,
              items: new List.generate(snapshot.data.length, (int index) {
                return new DropdownMenuItem<int>(
                  value: snapshot.data[index].id,
                  child: new Text(snapshot.data[index].name),
                );
              }),
              onChanged: (id) {
                List<Cities> c =
                    snapshot.data.where((c) => c.id == id).toList();
                print(c[0].name);
                setState(() {
                  selectedCity = c[0];
                });
              },
              hint: Text("Select city"),
              isExpanded: true,
            )),
          )
        : Container();
});

0 个答案:

没有答案