Navigate back and show snackbar
112
try this:
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
' Homepage',
),
),
body: Center(
child: RaisedButton(
onPressed: () async {
await Navigator.of(context).push(
PageRouteBuilder(
pageBuilder: (c, a1, a2) => Page2(),
transitionsBuilder: (c, animation, a2, child) {
var begin = Offset(0.0, 1.0);
var end = Offset.zero;
var curve = Curves.ease;
var tween = Tween(begin: begin, end: end)
.chain(CurveTween(curve: curve));
return SlideTransition(
position: animation.drive(tween),
child: child,
);
},
transitionDuration: Duration(milliseconds: 2000),
),
);
showSnackBar();
},
child: Text('go to Page2'),
),
));
}
showSnackBar() {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("page 1")));
}
}
in page 2:
RaisedButton(
child: Text('go to homepage '),
onPressed: () {
Navigator.of(context).pop();
},
),
Author by
Mhmd Khatib
Updated on December 29, 2022Comments
-
Mhmd Khatib over 1 year
I want to show SnackBar when the page1 is showing. When the user navigates from page2 to page1.
But i works only from page1 to page2
That ist my Code
class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); } class _HomePageState extends State<HomePage> { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text( ' Homepage', ), ), body: Center( child: RaisedButton( ***onPressed: () { Navigator.push(context, BouncyPageRoute3(widget: Page2())); }*** child: Text('go to Page2'), ), ))); } } class BouncyPageRoute3 extends PageRouteBuilder { final Widget widget; BouncyPageRoute3({this.widget}) : super( transitionDuration: Duration(milliseconds: 700), transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) { animation = CurvedAnimation(parent: animation, curve: Curves.ease); return ScaleTransition( scale: animation, alignment: Alignment.center, child: child, ); }, pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) { return widget; }); }
When you use the RaisedButton on Page2 to return to HomePage, i want that a SnackBar on HomePage to show
I used also BouncyPageRoute3, that ist for the animation.
-
BabC about 3 yearsCan you share the code when you go from page1 to page2 pls ?
-
Mhmd Khatib about 3 yearsI added my code to the Question @BabC
-
-
Mhmd Khatib about 3 yearsit did not work sadly, how would it be with the code above in the question?
-
Elvis Teles about 3 yearsuse Navigator.pop, keep MaterialApp only on MyApp class
-
Mhmd Khatib about 3 yearsi added this to my code for animation onPressed: () { Navigator.push(context, BouncyPageRoute3(widget: Page2())); } how schould it be now?
-
Elvis Teles about 3 yearswhat kind of animation do you want?
-
Mhmd Khatib about 3 yearsi had Animating Route Transitions