flutter 更新下拉列表中的选定用户

5sxhfpxr  于 2023-06-30  发布在  Flutter
关注(0)|答案(1)|浏览(154)

我想在Flutter中更新UI中选定的用户。我使用区块来管理应用程序的状态。我不知道如何实现它。我尝试了调试打印,它显示了当前选定的用户,但它没有在UI中更新。我也把我的代码放在这里
有什么想法吗

SingleChildScrollView(
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                Container(
                  child: BlocBuilder<DropdownBloc, DropdownState>(
                    builder: (context, state) {
                      if (state is DropdownInitial) {
                        BlocProvider.of<DropdownBloc>(context)
                            .add(SelectionEvent());
                      }
                      if (state is Loaded) {
                        List<String> list = state.data as List<String>;
                        String selectedValue = "Tara Wood";
                        return DropdownButton<String>(
                          value: selectedValue,
                          items: list
                              .map((data) => DropdownMenuItem<String>(
                                  value: data, child: Text(data.toString())))
                              .toList(),
                          onChanged: (String? item) {
                            setState(() {
                              selectedValue = item!;
                              debugPrint(
                                  "This is selected value for the dropdown $selectedValue");
                            });
                            BlocProvider.of<SessionSelectionBloc>(context)
                                .add(SessionSelectedEvent(username: item!));
                          },
                        );

我试着把下拉列表代码放在单独的文件中,但是我不知道如何从那里触发块事件。理想情况下,我想创建一个通用的下拉列表,因为代码的可重用性

k5ifujac

k5ifujac1#

你需要在你的Bloc中有一个变量,然后在UI中像这样使用这个变量:

SingleChildScrollView(
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                Container(
                  child: BlocBuilder<DropdownBloc, DropdownState>(
                    builder: (context, state) {
                      if (state is DropdownInitial) {
                        BlocProvider.of<DropdownBloc>(context)
                            .add(SelectionEvent());
                      }
                      if (state is Loaded) {
                        List<String> list = state.data as List<String>;
                        String selectedValue = "Tara Wood";
                        return DropdownButton<String>(
                          value: BlocProvider.of<SessionSelectionBloc>(context).selectedValue, <--HERE IS THE CHANGE AND YOU NEED TO MAKE THIS VARIABLE IN BLOC
                          items: list
                              .map((data) => DropdownMenuItem<String>(
                                  value: data, child: Text(data.toString())))
                              .toList(),
                          onChanged: (String? item) {
                            setState(() {
                              selectedValue = item!;
                              debugPrint(
                                  "This is selected value for the dropdown $selectedValue");
                            });
                            BlocProvider.of<SessionSelectionBloc>(context)
                                .add(SessionSelectedEvent(username: item!));
                          },
                        );

相关问题