I have problem that create cancel button in textField.
But it’s always save changing cancel button and submit button both
If I delete the cancel button code, it’s also save the change of textField when pop alertdialog by clicking background
I find some video and document but there is not different in code
https://www.youtube.com/watch?v=TpW7nLL57uQ
Below: My AlertDialog code
Future<void> editField(String field) async {
String newValue = '';
await showDialog(
context: context,
builder: (context) => AlertDialog(
backgroundColor: Colors.grey[100],
title: Text(
"Edit $field",
style: const TextStyle(color: Colors.black),
),
content: TextField(
autofocus: true,
style: const TextStyle(color: Colors.black),
decoration: InputDecoration(
hintText: "Enter new $field",
hintStyle: const TextStyle(color: Colors.grey),
),
onChanged: (value) {
newValue = value;
},
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.of(context).pop(newValue),
child: const Text('Submit'),
),
],
),
);
if (newValue.trim().isNotEmpty) {
await userCollection.doc(currentUser.email).update({field: newValue});
}
}
2
Answers
Try this code:
Put this code in on submit as this is being executed in either condition.