In Flutter, you can navigate between screens by using the Navigator widget. The Navigator provides a stack-based navigation system that allows you to push and pop routes (screens) from the stack.
To navigate to a new screen, you can use the Navigator.push() method and pass in the context and the route for the new screen. To go back to the previous screen, you can use the Navigator.pop() method.
Here's an example:
// First screen (current screen) class FirstScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('First Screen')), body: Center( child: RaisedButton( child: Text('Go to second screen'), onPressed: () { Navigator.push( context, MaterialPageRoute(builder: (context) => SecondScreen()), ); }, ), ), ); } } // Second screen class SecondScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Second Screen')), body: Center( child: RaisedButton( child: Text('Go back'), onPressed: () { Navigator.pop(context); }, ), ), ); } }
MaterialPageRoute and ModalRoute with settings property.
And these arguments can be accessed via ModalRoute.of(context).settings.arguments in the new screen.
Comments
Post a Comment