How to run drawer code form an action's item? - flutter

As far as drawer code is intended to run from scaffold and if I use actions then drawer code does not work - is it possible to run drawer code from the action code?
appBar: AppBar(
title: age == Type.old ? const Text('Old') : const Text('New'),
actions: [
IconButton(icon: Icon(Icons.arrow_back_ios_outlined), onPressed: () {}),
IconButton(icon: Icon(Icons.arrow_forward_ios_outlined), onPressed: () {}),
IconButton(
icon: Icon(Icons.home),
onPressed: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => AllCars()),
);
}),
IconButton(icon: Icon(Icons.menu), onPressed: () {
// how to run drawer code from here?
}),
],
),

You could use below method to open drawer of same scaffold
Scaffold.of(context).openDrawer();

Related

Flutter: How to change the icon returned by navigation?

I am new in Flutter. I want to change the icon returned by navigation.
Screen1 Code:
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Screen2(),
),
);
Screen2 Code:
Scaffold(
appBar: AppBar(),
body: Container(),
),
The AppBar() widget has a leading property where you can customize the navigation icon.
Here's an example:
AppBar(
leading: IconButton(
onPressed: () {
Navigator.pop(context);
},
icon: const Icon(
Icons.menu_rounded,
),
),
),
Check this helpful documentation for Material App Bars: https://material.io/components/app-bars-top/flutter

flutter, how to return from List<Widget> for AppBar actions

im learning flutter, could you please tell me how to return a Widget from List for two actions of Appbar, thanks a lot!
List<Widget> buildViewingActions(BuildContext context, Event event){
IconButton(
icon: const Icon(Icons.edit),
onPressed: (){
Get.to(EventCreatingPage(event: event,));
},
);
IconButton(
icon: const Icon(Icons.delete),
onPressed: (){
Get.offAll(CalendarPage());
},
);
//what should I write here?
}
In order to return the two actions as a list:
List<Widget> buildViewingActions(BuildContext context, Event event){
return [
IconButton(
icon: const Icon(Icons.edit),
onPressed: (){
Get.to(EventCreatingPage(event: event,));
},
),
IconButton(
icon: const Icon(Icons.delete),
onPressed: (){
Get.offAll(CalendarPage());
},
),
];
}
We just surround the items with [] to make them into a list and then we put a return statement before the list

Why does this FAB not animate to an extended one?

So I want to animate a fab between extended and normal like Gmail
but my FABs refuse to animate this just change immediately
This is my code:
return Scaffold(
appBar: appBar,
body: Center(
<Code of no interest>
),
floatingActionButton: isFABOpen
? FloatingActionButton.extended(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => AddQuote()),
);
},
label: Text('Add Quote'),
icon: Icon(Icons.add_rounded),
)
: FloatingActionButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => AddQuote()),
);
},
child: Icon(Icons.add_rounded),
),
floatingActionButtonAnimator: FloatingActionButtonAnimator.scaling,
);

How to open an AlertDialog in Flutter by Button-Press?

after Button-press, i want to open an AlertDialog, but only if the variable bool showAlert is true.
This is my code until now:
FlatButton(
child: Text("HIER"),
onPressed: () {
return AlertDialog(
title: Text("HI"),
content: Text("Are you there?"),
actions: [
FlatButton(child: Text("Yes"), onPressed: () {},),
FlatButton(child: Text("No"), onPressed: () {},)
],
elevation: 24,
);
},
),
For my question (opening the alert if bool is true), the problem is, the AlertDialog is not opening.
Any solutions? Thanks
To show an AlertDialog you need a showDialog, so the code results like this :
FlatButton(
child: Text("HIER"),
onPressed: () {
if(showAlert){
showDialog(
//if set to true allow to close popup by tapping out of the popup
barrierDismissible: false,
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text("HI"),
content: Text("Are you there?"),
actions: [
FlatButton(child: Text("Yes"), onPressed: () {},),
FlatButton(child: Text("No"), onPressed: () {},)
],
elevation: 24,
),
);
}
},
),
Use this code to show native diolog depends on platform:
FlatButton(
child: Text("HIER"),
onPressed: () {
if (Platform.isIOS) {
showCupertinoDialog(
context: context,
builder: (context) {
return CupertinoAlertDialog(
title: Text("HI"),
content: Text("Are you there?"),
actions: [
CupertinoDialogAction(
child: Text("Yes"),
onPressed: () {},
),
CupertinoDialogAction(
child: Text("No"),
onPressed: () {},
)
],
);
},
);
} else {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text("HI"),
content: Text("Are you there?"),
actions: [
FlatButton(
child: Text("Yes"),
onPressed: () {},
),
FlatButton(
child: Text("No"),
onPressed: () {},
)
],
elevation: 24,
);
},
);
}
},
);
Check out this code, Hope it will help you
openDialog(bool showAlert, BuildContext context) async {
if(!showAlert) return;
var result = await showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text("HI"),
content: Text("Are you there?"),
actions: [
FlatButton(child: Text("Yes"), onPressed: () {
Navigator.pop(context, true);
},),
FlatButton(child: Text("No"), onPressed: () {
Navigator.pop(context, false);
},)
],
elevation: 24,
),
);
if(!(result ?? false)) {
// Yes click
} else {
// No click
}
}
Alternative
GetX Package Get.dialog()
And for many other scaffold components.
Dialog, Bottosheet, Snackbar, StateManagent Getx and Obx builder, and much more.
Visit pub.dev and search for GetX

How to confirm app exit when appbar back button is pressed

I want to confirm exiting the app when the back button on the appbar is pressed.
appBar: AppBar(
leading: IconButton(
icon: Icon(
Icons.arrow_back_ios,
color: Colors.white,
),
onPressed: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('Save and Exit?'),
content: Text('are you sure you want to save and exit?'),
actions: [
FlatButton(
onPressed: () => Navigator.pop(context, false),
child: Text('No'),
),
FlatButton(
onPressed: () => Navigator.pop(context, true),
child: Text('Yes'),
),
],
);
},
);
// Navigator.pop(context);
},
),
I tried this but didn't work.
I have found some answers on how to do it when the system back button is pressed using WillPopScope but none of them work in my case.
Help me out
You can check it with Navigator.canPop(context) i guess. It will return true or false.
in onPressed you can check it, if it's true you can do Navigator.pop(context) otherwise call showDialog.
there is link of doc
https://api.flutter.dev/flutter/widgets/Navigator/canPop.html