I have a flutter application with a bottom navigation bar that keeps page states in the navigation (I use go_router). I’ve been trying for a long time to get a callback on my fav page as soon as I arrive on the page. But I can’t, so here’s an outline of what I’d like:
Page 1 (which manages navigation):
void _tabChanged() {
...
}
...
LazyIndexedStack(
index: _controller.index,
children: const [
Home(),
Fav(), // it should be sent the callback, linked with tabChanged()
],
),
And for the page, you’d have to receive the callback:
const Fav({Key? key, required this.callback}) : super(key: key);
...
widget.callback() {
... here I receive the callback from tab.dart
}
2
Answers
Thank you very much PurplePolyhedron, I received an error so here's how I did it in the end:
and in the Fav page:
I assume you want the navigation bar to call some method of the
State<Fav>
. You can find the state using aGlobleKey
Or you can pass a
Listenable
, subscribe to it inFav
, and trigger the callback in navigation bar.However, the best practice is to separate the logic from the widget tree instead of doing everything in
State