Flutter/ dart -下拉列表值未更改

kgqe7b3p  于 2023-08-07  发布在  Flutter
关注(0)|答案(2)|浏览(146)

我已经创建了一个下拉菜单,并填充了值,但是,当我选择一个,所选的值不会改变。我知道这是一个常见的问题,但我已经尝试了许多解决方案,没有一个有帮助。下面是我的代码:

String selectedOccasion;

  @override
  Widget build(BuildContext context) {
    final collectionProvider = Provider.of<CollectionProvider>(context).allOccasions;
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.deepPurpleAccent,
        title: Text('Viewer'),
        actions: [
          Stack(
            children: [
              IconButton(
                  icon: Icon(Icons.add),
                  onPressed: () {
                    showDialog(
                        context: context,
                        builder: (context) {
                          return AlertDialog(
                            title: Text("Add to collection"),
                            content: DropdownButton<String>(
                              hint: Text('Select Occasion'),
                              value: selectedOccasion,
                              items: collectionProvider.map((String value) {
                                return new DropdownMenuItem<String>(
                                  value: value,
                                  child: new Text(value),
                                );
                              }).toList(),
                              onChanged: (String newValue) {
                                setState(() {
                                  selectedOccasion = newValue;
                                });
                              },),
                          );
                        });
                  })
            ],
          )
        ],
      ),
      body: Container(child: Text('Body')
    );
  }

字符串
有什么建议吗?谢啦,谢啦

ccrfmcuu

ccrfmcuu1#

您需要将DropdownButton转换为有状态的小部件。因此setState将起作用。所有其他代码都可以正常工作。

StatefulBuilder(
                  builder: (context, setState) {
                    DropdownButton()
                  })

字符串

6yoyoihd

6yoyoihd2#

这里是一个接受的响应的例子。

@override
  Widget build(BuildContext context) {
    List<String> listValues = ['Height (inch)', 'Height (cm)'];
    final borderRadiusDropdown = BorderRadius.circular(14);
    const textColorItem = Colors.black;
    String dropdownValue = listValues.first;

    return StatefulBuilder(
      builder: (context, setInnerState) => DropdownButton<String>(
        //internal state for custom dropdown button
        value: dropdownValue,
        style: TextStyle(
            color: textColorItem,
            fontSize: Theme.of(context).textTheme.bodyMedium!.fontSize),
        borderRadius: borderRadiusDropdown,
        underline: const SizedBox(),
        elevation: 9,
        onChanged: (String? value) {
          setInnerState(() {
            dropdownValue = value!;
          });
        },
        items: listValues.map<DropdownMenuItem<String>>((String value) {
          return DropdownMenuItem<String>(
            value: value,
            child: Text(value),
          );
        }).toList(),
      ),
    );
  }

字符串

相关问题