Navigation in in flutter - flutter

I have a request from a client who wants to skip one page in opening under some conditions and immediately open another page. However the problem is that when it goes back it wants to always show 2 pages. So page 1 opens page 3 and when I go back from page 3 it goes to page 2 and then station 1. I'm new to flutter and I don't know how to do it.

Use pushReplacement for such Navigation
// In Screen 1
Navigator.pushReplacement(context, Screen3()) // Or Screen2()
// In Screen 3
Navigator.pushReplacement(context, Screen2())
// In Screen 2
Navigator.pushReplacement(context, Screen1())

From page 1 to page 3 your can use
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const Page3()),
Then if you want to go to Page 2 from page 3 use WillPopScope in page 3
return WillPopScope(
onWillPop: (){},
child: Scaffold(
body: Column(
children: [],
),
),
);
Add the above code in page 3. Add condition to check if it should go to Page 1 or 2 in onWillPop and again use navigator.pop or navigator.pushReplacement

Related

Flutter Drawer Navigation

I have a list of Screens in a custom drawer.
Screen 1,
Screen 2,
Screen 3
What is the proper way of navigating from Screen 1 to Screen 2 on push of a button. Currently, I am losing the hamburger (the drawer button) option when I push or push replace.
The code I have was not written by me and I do not have access to the person who wrote the code.
Any thoughts?
Just use the standard navigation:
TextButton(
onPressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context) => Screen2()));
}
child:
//child goes here
)

How to navigate to previous page using device back button - Flutter

When I press the device back button, it navigate to the homepage but not to the previous page.
I would like to know how to navigate to the previous page using the the device back button.
Thank you
You might have used Navigator.pushReplacement which destroys the previous page and creates the new page.However if you use Navigator.push pressing the back button will navigate you to the previous screen.
Example:
Container(
child: Center(
child: TextButton(child: Text("Next page"),onPressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context) => Page2(),)); // back button will navigate to previous screen
//Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => Page2(),)); // back button will not navigate to previous page
},),),),
It was a Materialapp widget problem.
I solved the problem by deleting the Materialapp widget that was in one of the pages.
This occurs because the previous navigation was not saved in the route stack.
What you should do is this:
Widget A
Navigator.push(context, WidgetB());
Inside Widget B, press the device button and you will return to widget A.
It is important that in widget A you are not using replacement or another that removes widget A from the stack.
You can use:
Navigator.pop(context);
or
Navigator.pushNamed(context, '/routeName');

Navigation in Flutter App - does not work as intended

I try to build a flutter app and want to navigate trough the pages.
I build a drawer with a menu with several items to open new pages - that works fine.
onTap: () {
Navigator.of(context).pop();
Navigator.push(
context,
new MaterialPageRoute(
builder: (BuildContext context) => new Page1(),
),
);
},
By pressing on the Page1 menuitem page1 opens fine.
Inside page1 I have a slidable list with "edit" and "details". I open this with
onTap: () {
Navigator.of(context).pop();
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => new DetailsPage(),
),
);
},
In the details Page I see all the things i wanna see, but when I press the Back button in the appBar i am back at home screen with closed drawer.
I tried to put onPressed: () => Navigator.pop(context); inside the appBar section but this doesn´t work either.
Now I got what I want by using always Navigator.push but I am sure that this is not right, so that navigator stack gets bigger and bigger.
What I do wrong? Why .pop does not bring me to the last page?
Any ideas?
Thx in advance
Patrick
Just remove the line with the Navigator.of(context).pop();in the Page1 onTap.
you can use pushReplacement in any cases you need to remove current page from stack and route to new page.
Replace the current route of the navigator that most tightly encloses
the given context by pushing the given route and then disposing the
previous route once the new route has finished animating in.
here is the documentation

appBar back button/update List in other class

In my project I move from the current page (2) to the previous one (1) an updated a List, through the constructor, with the back button in the appBar
onPressed: () => Navigator.pushReplacement (
            context,
            new MaterialPageRoute (
                builder: (context) => new PagesListPage (
                      pagesList: list Pages,
                    )),
          )
The data is updated correctly on the previous page
but Flutter rightly 'duplicates' the previous page (1) in the stack, that is:
on page 1 - tap on button to go to page 2 ---> I see page 2
on page 2 - tap on button back in appBar (navigator.pushReplacement) ---> I see page 1
on page 1 - tap on button back in appBar ---> I see again page 1
How can I eliminate the double occurrence of page 1 in the stack?
Or alternatives to update the List on page 1 from page 2.
I'm starting out with Flutter, and I've also tried with Provider, but if I understand correctly it can only update a 'value', I couldn't get it to update a 'List'!
Thanks for any valuable suggestions
On page 1 navigator:
buttonPressPage1() async {
var updatedData = await Navigator.pushReplacement(
context, new MaterialPageRoute(builder: (context) => page1));
setState(() {
oldData = updatedData;
});
}
On page 2 navigator:
buttonPressPage2() {
Navigator.of(context).pop(updatedData);
}
Using await to you can update the data.

Now i want Page D pop to Page B How to do it

My business scenario is that within a page, I need to go to the multi-level page in succession, and select the last level page, then return the parameters to the previous page. What should I do?
Page A Navigator
push to Page B
Page B Navigator
push to Page C
Page C Navigator
push to Page D
Now i want Page D pop to Page B
How to do it?
Use popUntil. Replace your Page B with '/login' example.
Navigator.popUntil(context, ModalRoute.withName('/login'));
//a.dart
onTap: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => BPage(),
settings: RouteSettings( // set route settings
name: "bpage" // you will use this string in popUntil
)
),
);
},
//d.dart
child: GestureDetector(
onTap: () {
Navigator.popUntil(context, ModalRoute.withName('bpage')); // here
},
child: Text('return A'),
),