flutter 有什么方法可以显示下拉按钮列表中的唯一值吗?

k2fxgqgv  于 2023-02-20  发布在  Flutter
关注(0)|答案(1)|浏览(152)

我的代码是:

Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text.rich(
                          TextSpan(
                            children: [
                              TextSpan(
                                text: 'Select District',
                                style: getSemiBoldStyle(
                                    color: Colormanager.lightGrey,
                                    fontSize: AppSize.s14),
                              ),
                              TextSpan(
                                text: AppStrings.star,
                                style: getSemiBoldStyle(
                                    color: Colormanager.red,
                                    fontSize: AppSize.s14),
                              ),
                            ],
                          ),
                        ),
                        const SizedBox(
                          height: AppSize.s4,
                        ),
                        DropdownSearch<CoverageArea>(
                          popupProps: PopupProps.menu(
                            showSearchBox: true,
                            // disabledItemFn: (String s) => s.startsWith('I'),
                            searchFieldProps: TextFieldProps(
                              style: getBoldStyle(
                                  color: Colormanager.black,
                                  fontSize: FontSize.s15),
                            ),
                            itemBuilder: (context, item, isSelected) =>
                            isSelected
                                ? Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Text(
                                item.district!,
                                style: getBoldStyle(
                                    color: Colormanager.primary),
                              ),
                            )
                                : Container(
                              padding: const EdgeInsets.all(8.0),
                              margin: const EdgeInsets.only(
                                  bottom: 4),
                              color: Colormanager.lightGrey
                                  .withOpacity(0.2),
                              child: Text(
                                item.district!,
                                style: getBoldStyle(
                                    color: Colormanager.black,
                                    fontSize: FontSize.s15),
                              ),
                            ),
                          ),
                          items: controller.coverageAreas,

                          dropdownDecoratorProps: DropDownDecoratorProps(
                            baseStyle:
                            getSemiBoldStyle(color: Colormanager.black),
                            dropdownSearchDecoration: const InputDecoration(
                              // labelText: "Menu mode",
                              // hintText: "country in menu mode",

                            ),
                          ),
                          onChanged: (value) {
                            controller.selectedDistrict = value;
                            controller.selectDistrict.value =
                                value!.district.toString();
                          },
                          itemAsString: (item) => item.district.toString(),

                          // selectedItem: controller.selectArea.value,
                        ),
                      ],
                    ),

然后API调用显示:

{
    "Status": true,
    "message": "Coverage Area List",
    "data": [
        {
            "id": 898,
            "inside": 0,
            "zone_name": "OSD Operation",
            "district": "Barguna",
            "area": "Barguna Sadar",
            "district_id": 35,
            "zone_id": 14,
            "post_code": null,
            "h_delivery": null,
            "oneRe": null,
            "oneUr": null,
            "plusRe": null,
            "plusUr": null,
            "cod": null,
            "insurance": null,
            "status": 0,
            "created_at": "2023-01-17 15:40:08",
            "updated_at": "2023-01-17 15:40:08"
        },
        {
            "id": 897,
            "inside": 0,
            "zone_name": "OSD Operation",
            "district": "Barguna",
            "area": "Taltoly",
            "district_id": 35,
            "zone_id": 14,
            "post_code": null,
            "h_delivery": null,
            "oneRe": null,
            "oneUr": null,
            "plusRe": null,
            "plusUr": null,
            "cod": null,
            "insurance": null,
            "status": 0,
            "created_at": "2023-01-17 15:39:49",
            "updated_at": "2023-01-17 15:39:49"
        },
        {
            "id": 896,
            "inside": 0,
            "zone_name": "OSD Operation",
            "district": "Barguna",
            "area": "Amtoly",
            "district_id": 35,
            "zone_id": 14,
            "post_code": null,
            "h_delivery": null,
            "oneRe": null,
            "oneUr": null,
            "plusRe": null,
            "plusUr": null,
            "cod": null,
            "insurance": null,
            "status": 0,
            "created_at": "2023-01-17 15:39:29",
            "updated_at": "2023-01-17 15:39:29"
        },
        {
            "id": 895,
            "inside": 0,
            "zone_name": "OSD Operation",
            "district": "Barguna",
            "area": "Betage",
            "district_id": 35,
            "zone_id": 14,
            "post_code": null,
            "h_delivery": null,
            "oneRe": null,
            "oneUr": null,
            "plusRe": null,
            "plusUr": null,
            "cod": null,
            "insurance": null,
            "status": 0,
            "created_at": "2023-01-17 15:39:05",
            "updated_at": "2023-01-17 15:39:05"
        },
    {
            "id": 891,
            "inside": 0,
            "zone_name": "OSD Operation",
            "district": "Barishal",
            "area": "Barisal Bandor Thana",
            "district_id": 33,
            "zone_id": 14,
            "post_code": null,
            "h_delivery": null,
            "oneRe": null,
            "oneUr": null,
            "plusRe": null,
            "plusUr": null,
            "cod": null,
            "insurance": null,
            "status": 0,
            "created_at": "2023-01-17 15:35:28",
            "updated_at": "2023-01-17 15:35:28"
        },

等等......
我想显示Barguna区只是一次和可靠的下拉按钮在接下来的区域。但我怎么能做到呢?
This is the output我怎么能只显示一个名字一次???

ia2d9nvy

ia2d9nvy1#

当有多个项目包含单个值时,我建议您使用String dataType而不是CoverageArea。然后您可以从数据创建一组字符串。
所以这个项目就像

final List<String> items = controller.coverageAreas.map((e)=> e.district.trim()).toSet().toList();

现在在下拉列表中使用这个items。值将作为字符串DropdownSearch<String>(返回

相关问题