如何在 OnChanged 中重置 DropdownButtonField 的值

How to reset a Value of DropdownButtonField inside OnChanged

我有一个 DropdownButtonFormField,其中最后一项是 DropdownMenuItem,用于使用对话框添加新对象。

Padding(
  padding: const EdgeInsets.only(bottom: 15),
  child: Observer(
    builder: (_){
      return DropdownButtonFormField(
        value: createdContentStore.subjectTitleSelected,
        isDense: true,
        decoration: InputDecoration(
            contentPadding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
            isDense: true,
            border: OutlineInputBorder()
        ),
        onChanged: (value) async {
          // print(value);
          if(value == 'newSubject'){
            Subject newSubject = await showDialog(
                context: context,
                builder: (_) => CreatedSubjectDialogBox(isNewContent: true,)
            );
            if(newSubject != null){
              createdContentStore.setSubjectTitleSelected(newSubject.title);
              createdContentStore.setSubject(newSubject);
            } else {
              // WHAT CAN I DO HERE TO RESET DROP'S VALUE?
            }
          } else {
              createdContentStore.setSubjectTitleSelected(value);
          }
        },
        iconSize: 30,
        hint: Text('Selecione uma matéria'),
        items: subjectStore.subjectList.map((subject) => DropdownMenuItem(
          value: subject.title,
          child: Text(subject.title),
          onTap: () {
            createdContentStore.setSubject(subject);
          },
        )).toList()..add(DropdownMenuItem(
          value: 'newSubject',
          child: Center(
            child: Text(
              'Nova Matéria'.toUpperCase(),
              style: TextStyle(color: redRevise),
            ),
          ),
        )),
      );
    },
  ),
);

当对话框显示时,用户可以创建一个新的对象,该对象将出现在下拉列表中。当用户取消对话框时,它会显示最后一项。期望的行为是改为显示提示。

有人可以帮助我吗? 谢谢!

您只需从下拉列表中删除值,

 DropdownButtonFormField(
//** REMOVE THE VALUE **
        isDense: true,
        decoration: InputDecoration(
            contentPadding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
            isDense: true,
            border: OutlineInputBorder()
        ),
        onChanged: (value) async {
          if(value == 'newSubject'){
            Subject newSubject = await showDialog(
                context: context,
                builder: (_) => CreatedSubjectDialogBox(isNewContent: true,)
            );
            if(newSubject != null){
              createdContentStore.setSubjectTitleSelected(newSubject.title);
              createdContentStore.setSubject(newSubject);
            } else {
              // WHAT CAN I DO HERE TO RESET DROP'S VALUE?
            }
          } else {
              createdContentStore.setSubjectTitleSelected(value);
          }
        },
        iconSize: 30,
        hint: Text('Selecione uma matéria'),
        items: subjectStore.subjectList.map((subject) => DropdownMenuItem(
          value: subject.title,
          child: Text(subject.title),
          onTap: () {
            createdContentStore.setSubject(subject);
          },
        )).toList()..add(DropdownMenuItem(
          value: 'newSubject',
          child: Center(
            child: Text(
              'Nova Matéria'.toUpperCase(),
              style: TextStyle(color: redRevise),
            ),
          ),
        )),
      );
    },
  ),
);