选择其中一项后,重新启动DropdownButton项目

时间:2019-07-20 10:06:22

标签: flutter

在这个简单的DropdownButton小部件中,当我选择一个项目时,刷新的项目和选择值是SessionsEntity列表项目的第一项,并且我无法选择其他项目,将其选中的原因是首先选择项目,

我认为选择项目后,是重建DropdownButton小部件的原因

  SessionsEntity sessionData;
  BarCodesBloc _barcodesBloc;

  ...

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: ApplicationAppBar(appBarTitle: sessionData!=null? ' (${sessionData.sessionName}) ':'',),
      body: BlocListener(
        bloc: _barcodesBloc,
        listener: (BuildContext context, BarCodesState state) {
          if (state is BarCodeScannedSuccessful) {
            player.play('ringtones/2.mp3');
          }
          if (state is BarCodeScannedError) {
          }
          if (state is BarCodeScannedDuplicate) {
            player.play('ringtones/1.mp3');
          }
        },
        child: BlocBuilder(
          bloc: _barcodesBloc,
          builder: (BuildContext context, BarCodesState state) {
            return FutureBuilder(
              future: globals.database.sessions.getAllSessionsFuture(),
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                if (snapshot.hasData) {
                  List<SessionsEntity> sessions = snapshot.data;
                  List<DropdownMenuItem<SessionsEntity>> _dropdownMenuItems;
                  if (sessions != null && sessions.length > 0) {
                    _dropdownMenuItems = buildDropdownMenuItems(sessions);
                    sessionData = _dropdownMenuItems[0].value;
                    return Stack(
                      children: <Widget>[
                        DropdownButtonHideUnderline(
                          child: Theme(
                            data: Theme.of(context).copyWith(
                              canvasColor: Colors.white,
                            ),
                            child: DropdownButton(
                              items: _dropdownMenuItems,
                              isDense: true,
                              value:sessionData,
                              onChanged: onChangeDropdownItem,
                              isExpanded: true,
                              hint: Text('please select item',
                                  style: Theme.of(context).textTheme.caption.copyWith(color: Colors.black, )),
                            ),
                          ),
                        ),
                      ],
                    );
                  } else {
                    return Container(
                      child: Center(
                        child: Text(
                          Fa.keywords['noAnySessions'],
                          style: Theme.of(context).textTheme.caption.copyWith(

                                color: Colors.black,
                              ),
                        ),
                      ),
                    );
                  }
                } else {
                  return Container(
                    child: Center(
                      child: Text(
                        Fa.keywords['noAnySessions'],
                        style: Theme.of(context).textTheme.caption.copyWith(
                              color: Colors.black,
                            ),
                      ),
                    ),
                  );
                }
              },
            );
          },
        ),
      ),
    );
  }

  onChangeDropdownItem(SessionsEntity selectedCompany) {
    setState(() {
      sessionData = selectedCompany;
      print(sessionData.sessionName);
    });
  }

0 个答案:

没有答案