I would like to change the text inside ElevatedButton when I click. For example 'Fav' to 'Unfav' and the color of the button when I do this action change also. I tried with boolean and also to change the text in Button inside setState block, it didn't help.
Demontration what I want to change
Code where I try to do this
ElevatedButton(
child: Text(title),
onPressed: () async {
String ref = ads.reference.id;
String? favuid = user != null
? user?.uid
: googleUser?.id;
if (favuid != null) {
setState(() {
title = 'UnFav';
if ((ads.data() as dynamic)['isfav$favuid'] == null || !(ads.data() as dynamic)['isfav$favuid']) {
collectionads.doc(ref).update({
'isfav$favuid': true
});
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(
duration: Duration(seconds: 2),
content: Text("Add to Favourite list"),
));
}
else {
title = 'Fav';
collectionads.doc(ref).update({
'isfav$favuid': false
});
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(
duration: Duration(seconds: 2),
content: Text(
"Deleted from Favourite list"),
));
}
});
setState(() {
!(ads.data() as dynamic)['isfav$favuid'] == (ads.data() as dynamic)['isfav$favuid'];
});
}
},
)