I have been developping a small "days left before an event" Flutter app for myself.
The main page consists of a list of "Events" widgets and infos about them. Main page
On the right side of any "Event" there is a bin icon that simply sends an SQL query to delete the event from the DB on tap.
Unfortunately I have been encountering troubles refreshing the main page after the tap on the bin icon because I have no reference of the state of the main page in the "Event" widget.
I have resolved the problem by creating a refresh function in my main page state :
void refresh() {
setState(() {});
}
and sending a reference --> "var hp" of the state of my main page to the "Event" widget:
class Event extends StatefulWidget {
String title;
DateTime date;
int id;
String daysLeft;
Color daysColor;
var hp;
Event(this.hp, this.title, this.date, this.daysLeft, this.daysColor,
[this.id]) {
if (this.title == null) {
this.title = "No title";
}
}
...
thus, I can refresh my main page when tapping on the bin icon :
GestureDetector(
onTap: () {
db.deleteEvent(widget.id);
log("refreshing page inc...");
widget.hp.refresh();
log("refreshed");
},
child: Container(
margin: EdgeInsets.only(right: 15),
child: Icon(
Icons.delete,
size: 30,
)))
I am aware that this looks super ugly. Moreover, the state of the main page being a private class, I had to store it in a "var" which I do not like.
Is there any other way to do that kind of things ? This all looks super complicated and not really smooth to implement.
I have done some researches and it seems there are a few state management patterns (https://flutter.dev/docs/development/data-and-backend/state-mgmt/options?fbclid=IwAR2yiPjh_jPQKb9s3-2nBYRQwbQmhyv_6tjnZTqbpRbdJOZJdyDFy9HZ07E)
But is there no other way than implementing pattern to do this ?
Thanks :)
Aucun commentaire:
Enregistrer un commentaire