首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >颤振:如何在颤振中限制用户选择项目

颤振:如何在颤振中限制用户选择项目
EN

Stack Overflow用户
提问于 2022-11-11 11:01:16
回答 1查看 42关注 0票数 1

我将flutter_multi_select_items用于从JSON格式的项目数组中选择的多个项。它以前是工作的,但我想限制用户只从右到左顺序选择项目,而不是跳转到任何项目来选择下一个。例如,如果我有一个数组[1,2,3,4],我希望用户只按顺序选择1->2->3->4,而不是选择1->3->2->4等。

我的代码:

代码语言:javascript
运行
复制
MultiSelectContainer(
    itemsPadding: const EdgeInsets.all(10),
    textStyles: const MultiSelectTextStyles(
    textStyle: TextStyle(fontFamily: 'Montserrat', 
     fontWeight:FontWeight.bold,)),
     showInListView: true,
     listViewSettings: ListViewSettings( scrollDirection: Axis.horizontal,
     separatorBuilder: (_, __) => const SizedBox(
      width: 10,)),
     items: List.generate(dataSchedule == null ? 0 : dataSchedule.length,
     (index) => MultiSelectCard(value: dataSchedule[index]['time_slot'], label: dataSchedule[index]['time_slot'],)),
onChange: (allSelectedItems, selectedItem) { })

下面是我的数组返回的样子:

代码语言:javascript
运行
复制
["16:00","17:00","18:00","19:00","20:00","21:00"]

我希望选择来自16:00 to 17:00 to 18:00 to 19:00 to 20:00 to 21:00

从上面的代码来看,allSelectedItems用于所选项的数组,而selectedItem仅用于所选的单个项。如有任何部分需要澄清,请提前通知我,谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-14 07:56:43

所以我可以用一些逻辑来解决这个问题。由于无法将index包含在onchanged中,所以我将索引添加到value:中,以便获得每个select的索引值,然后从值中删除额外的值,只留下index值,然后得到前面的select值和当前选定的值。最后,我使用if语句检查当前select减去1是否等于前一个select,然后它是否正确。我的最后代码如下:

代码语言:javascript
运行
复制
MultiSelectContainer(
                                                    controller: controllerMultipleSelect,
                                                    itemsPadding: const EdgeInsets.all(10),
                                                   
                                                    showInListView: true,
                                                    listViewSettings: ListViewSettings(
                                                        scrollDirection: Axis.horizontal,
                                                        separatorBuilder: (_, __) => const SizedBox(
                                                          width: 10,
                                                        )),

                                                    items: List.generate(dataSchedule == null ? 0 : dataSchedule.length,
                                                            (index) => MultiSelectCard(
                                                              value: index.toString() +"-"+ dataSchedule[index]['time_slot'], label: dataSchedule[index]['time_slot'],)),
                                                    onChange: (allSelectedItems, selectedItem) {


                                                      if(allSelectedItems.length>1) {

                                                        var lst = allSelectedItems[allSelectedItems.length - 2];
            

                                                        var pos = lst.toString().lastIndexOf('-');

        String previousResult = (pos != -1)? lst.substring(0, pos): lst;
        var pos1 = selectedItem.toString().lastIndexOf('-');

        String currResult = (pos1 != -1)? selectedItem.substring(0, pos1): selectedItem;

                                                       if(int.parse(currResult)-1  == int.parse(previousResult)){
                                                         debugPrint("Correct order");
                                                       }else{


                                                         debugPrint("Incorrect order");


                                                       }

                                                      }


                                                    })
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74401534

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档