2

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.

2
  • Can you share the code when you go from page1 to page2 pls ? Commented Apr 15, 2021 at 14:07
  • I added my code to the Question @BabC Commented Apr 15, 2021 at 15:32

1 Answer 1

2

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(); }, ), 
Sign up to request clarification or add additional context in comments.

5 Comments

it did not work sadly, how would it be with the code above in the question?
use Navigator.pop, keep MaterialApp only on MyApp class
i added this to my code for animation onPressed: () { Navigator.push(context, BouncyPageRoute3(widget: Page2())); } how schould it be now?
what kind of animation do you want?
i had Animating Route Transitions

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.