0

我有一个 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),
            ),
          ),
        )),
      );
    },
  ),
);

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

有人能帮我吗?谢谢!

4

1 回答 1

0

您所要做的就是从下拉列表中删除该值,

 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),
            ),
          ),
        )),
      );
    },
  ),
);
于 2021-04-16T19:31:34.967 回答