How can I display my "teamName" value in the AlertDialog form while keeping the controller and not use "InitialValue" from my TextFormField? This is when I update the team name, I want the form to show it's name when the AlertDialog shows up. I am new to flutter, if you need anything else from my code just tell me.
AlertDialog alert = AlertDialog(
title: const Text(
"Modifier le nom de votre équipe"),
content: Form(
key: _thekey,
child: TextFormField(
controller: textController,
validator: (val) => val?.length == 0
? "Indiquez un nom d'équipe s'il vous plait"
: null,
onSaved: (val) =>
textController.text = val!,
),
),
actions: [
ElevatedButton(
onPressed: (() {
teamName = textController.text;
print("le nom de la teamsss est : $teamName");
setState(() {
print(
"le nom de l'équipe est: $teamName");
updateTeam(
team.idTeam,
textController,
idCoach,
context);
});
}),
child: const Text("Modifier"))
],
);
showDialog(
barrierDismissible: true,
context: context,
builder: (BuildContext ctx) {
return alert;
});
The alertDialog is supposed to get the teamName from a ListView. The prints with "$teamName" shows the right name, the update method works but I just want the field to have the initial name on it.
This is the update method if you need it as well
updateTeam(idTeam, textController, idCoach, context) async {
final statAccess = DatabaseModel();
SharedPreferences sp = await _pref;
idCoach = sp.getInt("id");
String nameControl = textController.text;
print("le nameControl est: $nameControl");
if (nameControl.isEmpty) {
alertDialog("le nom est vide", false);
} else {
Teams team =
Teams(idTeam: idTeam, nameTeam: nameControl, xidCoach: idCoach);
await statAccess.updateTeam(team).then((value) {
print(value);
if (value == 1) {
alertDialog("Mise à jour réussie", true);
Navigator.of(context, rootNavigator: true).pop();
} else {
alertDialog("la mise à jour à foiré", false);
}
}).catchError((error) {
print(error);
alertDialog("CA A VRAIMENT FOIRER", false);
});
}
}
CodePudding user response:
Use TextEditingController.fromValue(
to set initial value
TextEditingController.fromValue(
TextEditingValue(text: "MYInitial Value"));
If you like show hint use hintText
TextFormField(
decoration: InputDecoration(
hintText: "myHint text"
),
);
CodePudding user response:
Please follow the suggestions provided by yeasin sheikh also please add a stateful builder in the alert dialog for the state to be set like
showDialog(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text("Title of Dialog"),
content: Text("some content"),
actions: [
TextButton(
onPressed: () {
setState(() {
});
},
child: Text("setstateexample"),
),
],
);
},
);
},
);