【问题标题】:is there any way to control dropdown items based on another dropdown's value? [flutter]有没有办法根据另一个下拉菜单的值来控制下拉项目? [扑]
【发布时间】:2022-07-19 22:59:48
【问题描述】:

我的无状态类包含带有 4 个选项卡的表单,其中一个选项卡包含 3 个下拉列表,用于为国家/地区选择地址,并根据选择的值,我希望第二个下拉列表(用于城市)查看其项目。 下拉列表的所有项目都来自本地 sqlite 数据库。 问题是第二个下拉菜单没有查看其项目,但是当我使用调试器时,我发现列表成功来自数据库,但无状态类上的列表值更新没有发生。 有什么办法解决吗??

DropdownButtonFormField<Region>(
                                decoration: InputDecoration(
                                  isDense: true,
                                  floatingLabelBehavior:
                                  FloatingLabelBehavior.auto,
                                  labelStyle: TextStyle(fontSize: 22),
                                  contentPadding:
                                  EdgeInsets.symmetric(vertical: 9),
                                ),
                                value: regionList.isEmpty
                                    ? region
                                    : helRegion.getReg(
                                    user.regionAdresse, regionList),
                                //icon: Icon(Icons.arrow_downward),
                                iconSize: 24,
                                elevation: 16,
                                onChanged: (selectedRegion) {
                                   onRegionSelected(context, user, 
                       region, province, provinceList, selectedRegion);
                                },
                                items: regionList
                                    .map((project) => DropdownMenuItem(
                                  child: Text(project.Name),
                                  value: project,
                                ))

【问题讨论】:

  • 欢迎来到 SO!请问edit你的问题并复制粘贴onRegionSelected函数吗?

标签: flutter dart


【解决方案1】:

首先,您应该使用有状态的类,这样当您选择第一个下拉菜单时,将更新第二个下拉菜单中出现的信息,依此类推。 如果您的想法是保持格式无状态,您还可以将来自数据库的响应保存在流中并将下拉列表包装在 streamBuiler 中,以便在更新每个下拉列表指向的新数据时更新它们。 我将此作为答案留下,因为我没有将其添加为评论的声誉。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    相关资源
    最近更新 更多