How to prevent navigation buttons in dart - flutter

How to disable navigation buttons on the flutter app so the user can't be redirect to another page, recent view, and home.
Using onWillPop: () async => false, only affects the back button.

Specify null for the values of onTap, onPressed, etc to disable the buttons.
onTap: (someCondition) ? null : () {...}

Related

How to go back in the previous page, upon tapping the phone's back button without showing any dialog?

// I already tried this but it wont work
return WillPopScope(
onWillPop: () { Navigator.pop(context);
},
child: Scaffold(),
error:Error: A non-null value must be returned since the return type 'Future' doesn't allow null.
'Future' is from 'dart:async'.
onWillPop: () { Navigator.pop(context);
If there is a screen in the stack then tapping the android back bottom will automatically go to the previous screen. this is the android default behavior. you don't have to wrap with WillPopScope.
If you want to show some dialog or do something by tapping the back button then wrap with WillPopScope.
onWillpop is an async function, it requires a function with a return type Future<bool>.
If you want to disable the default behavior of the android back button then you can use this.
onWillPop: () async {
// do something here
return false;
},
Otherwise,
onWillPop: () async {
// do something here
return true;
},
Returning true will not disable the default behavior of the android back button.

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 a named route while poping it self?

I have a Start screen with a pushNamed button.
How do I discard Start screen after I reached to rules Screen?
onPressed: () {
Navigator.of(context).pushNamed(
'/rules',
);
}
Refer Navigation documentation
You can use pushReplacementNamed instead of pushNamed.
By using this your first screen will be removed from the screen stack. In your case, if you use the below code your start screen will be replaced with rules screen.
onPressed: () {
Navigator.pushReplacementNamed(context, '/rules);
}

Flutter pop best practice

I have the following flow Screen 1 -> Screen 2 -> Dialog (in a separate widget).
Screen 2 displays a dialog (Close? Yes or No). If someone presses Yes, I would like to return to the Screen 1, if they press No, just close the dialog and return to Screen 2. What I currently do is when Yes is tapped, I do Navigator.pop(context) twice. Is this a good practice? Is there a way to pass the context of Screen 2 to my dialog widget so I can pop that one directly?
Personally, I think it would be better to pass the response from the dialog back to the page, and let the page handle the rest.
You can do this:
//I'm using a raised button just to call the alert as an example...
RaisedButton(
child: Text('Press me'),
//This part here is the important part
onPressed: () async {
//You can return anything when you use Navigator.pop
//In this case I'm returning a bool indicating if the page should close or not.
//You have to await this because it depends on user input.
bool shouldPopResult = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
//The content of your dialog
actions: <Widget>[
// The value you pass here in Navigator.of(context).pop
// is the value that will be stored in shouldPopResult,
// so if "Yes" is pressed, true will return...
// and if "No", false is returned.
FlatButton(
child: Text('Yes'),
onPressed: () => Navigator.of(context).pop(true),
),
FlatButton(
child: Text('No'),
onPressed: () => Navigator.of(context).pop(false),
)
],
),
);
// This is for if the user dismisses the dialog without pressing a button
// In that case shouldPopResult would be null, so I'm setting it to false.
// You can prevent the user from dismissing the dialog
// setting barrierDismissible to false in the showDialog method.
if (shouldPopResult == null) shouldPopResult = false;
// And finally with the dialog already dismissed, you can decide
// to go back or not.
if (shouldPopResult) Navigator.of(context).pop();
});
As usual you can extract the dialog as a Widget, or extract the function that handles the dialog response altogether or anything else.
You can see the example of returning data from a page in the flutter documentation here.

how can i navigate between icons in the appBar on flutter?

I am new flutter developer and I would like to browse among these icons in app bar,Any idea?
tmy.png
You can use Tab Bar, please check this : https://m.youtube.com/watch?v=r1sQTA_zPog
If you hope to do it with the hard way you can make a button for each icon and in onPressed add setState with Navigator and modified color
Check ; https://flutter.dev/docs/development/ui/navigation
You can use like the below code for every icon.
IconButton(
icon: Icon(Icons.add_box),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
},
)