I used to had two widgets where the first displays a list and the second is the detail page of a specific item in the list.
class WidgetA extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: _buildAppBar(context),
body ListView.builder(...)
}
}
class WidgetB extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: _buildAppBar(context),
body ....
}
}
such that WidgetA
navigates to WidgetB
.
Navigator.push(context,MaterialPageRoute(builder: (context) => WidgetB()));
But since I want a nice animation for the transition, I now use a Hero widget in both Widgets.
// Widget B
@override
Widget build(BuildContext context) {
return Hero(
tag: someObject,
);
}
But now the problem is that whenever WidgetB
wants to display a snackbar, it is not visible there. If you would however navigate back to WidgetA
before the snackbar would have disappeard, you can see it there.
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
duration: const Duration(seconds: 3), content: Text('some text'),
));
I looked at the Scaffold documentation which states
It is typically not necessary to nest Scaffolds. For example, in a tabbed UI, where the bottomNavigationBar is a TabBar and the body is a TabBarView, you might be tempted to make each tab bar view a scaffold with a differently titled AppBar. Rather, it would be better to add a listener to the TabController that updates the AppBar
So I also tried returning a Simple Column
in WidgetB which is wrapped inside a ScaffoldMessenger
but none of these solutions works... I did some lookup online and people talk about it that this is intended behaviour. But no official sources say so. How could I nonetheless display a snackbar in the detail page while still using nice Hero transitions?