Week10 2
Week10 2
TextButton(onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context)=> sroutApp()));
},
Navigator.pop()
Return to the first route using Navigator.pop() method.
Navigator.pop() method pop-up or close the second route from the
stack and return to the first route.
To return to the original route, Navigator.pop() method will be call
inside onPressed() callback method in the SecondRoute widget.
TextButton(onPressed: () {
Navigator.pop(context);
},
Navigation Route
class SecondRoute extends StatelessWidget { class FirstRoute extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar( AppBar: AppBar(
title: Text("Second Screen"), title: Text('First Screen'),
), ),
body: Center( body: Center(
child: RaisedButton( child: RaisedButton(
color: Colors.blueGrey, child: Text('Click Here'),
onPressed: () { color: Colors.orangeAccent,
Navigator.pop(context); onPressed: () {
}, Navigator.push(
child: Text('Go back'), context,
), ), ); MaterialPageRoute(builder: (context) => SecondRoute()
} } ), ); },
),), ); } }
Navigation with Route Named Route
The Navigator maintains the stack-based history of routes. If there is a
need to navigate to the same screen in many parts of the app or we
want to move from first screen to third and from third to fifth. We can
work with named routes by using the Navigator.pushNamed() function
and initialRoute properties
initialRoute and Named route
The initialRoute property defines which route the app should start with.
The routes property defines the available named routes and the widgets to
build when navigating to those routes. InitialRoute is the loaded when
MaterialApp is instantiated.
initialRoute: '/',
routes: {
// When navigating to the "/" route, build the HomeScreen widget.
'/': (context) => HomeScreen(),
// When navigating to the "/second" route, build the SecondScreen widget.
'/second': (context) => SecondScreen(),
},
Navigator.push()
The Navigator.push() method is used to navigate/switch to a new
route/page/screen. The push() method adds a page/route on the
stack.
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
}