Example
How to open the url together with the snackbar in One button?
onTap: () => launch("http://flutter.dev"),
// when button tapped then url will be open and displaying a snackbar at a time
Discard the arrow function and write it like this
onTap: (){
launch("http://flutter.dev");
Scaffold.of(context).showSnackBar(SnackBar(content: Text("Message here"),));
},
onTap: () {
firstFunction();
secondFunction();
},
Please comment if I misunderstood your question.
Related
I want to hide the first alert dialog when I call showDialog() from it. After that when I close the second dialog I want the first dialog was visible again. How I can achieve this?
Before you call second dialog, use Navigator.of(context).pop() to close first dialog. Then, in the second one, you have functions then((value) {...}) or whenComplete(() {...}), inside that you can use it to re-open first dialog.
That's strange that you want to close first one, why don't you just leave it alone and let the second lies on it?
You can create common dialog to show data. if its already showing then just update data only.
showDialog return a future and you can pass data from dialog. The concept is here passing some flag to open the second dialog.
onPressed: () async {
final data = await showDialog(
context: context,
builder: (context) {
return AlertDialog(
content: ElevatedButton(
onPressed: () {
Navigator.of(context)
.pop(true); // true for to show second dialog
},
child: Text("open Second dialog"),
),
);
});
if (data == true) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text("Second dialog"),
);
});
}
},
i am using tabbar view with my app that have same floating action button:
floatingActionButton: FloatingActionButton(
onPressed: () async{
print("this is the current Screen :$currentScreen");
if(currentScreen==0)
{
await showInformationDialog(context);
}
else
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AddNewVehical(1),
),
);
},
but when i tap on tab it works perfectly and the currentscreen variable changes from 0 to 1 as expected but problem arises when i slide from one tab to another the currentscreen variable print always zero due to which on the other tab it also shows dialogue
can anyone tell me why this is happening? and a solution for it ?
Thanks in advance <3
You can add TabController listener in initState().
_tabController!.addListener(_handleTabSelection);
In _handleTabSelection method, get the tabController index.
currentScreen = _tabController!.index;
You can use TabController. With the index property you can get the currentIndex.
reference : https://api.flutter.dev/flutter/material/TabController-class.html
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.
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()),
);
},
)
This question already has answers here:
How to close Dialog using FlutterDriver
(2 answers)
Closed 2 years ago.
I am working with FlutterDriver, I have an IconButton defined and a key set as shown:
Center(
child: IconButton(
key: Key('moredots'),
icon: Icon(Icons.more_vert),
onPressed: () {
showDialog(
context: context,
builder: (_) => tableConfig,
);
},
),
)
The dialog is successfully shown with the following code:
await driver.tap(find.byValueKey('moredots'));
What I can't figure out is how to dismiss the dialog. I've tried:
Tapping the same value as shown above
Adding a key in Scaffold, finding the key and tapping
Adding keys in other UI elements in the hierarchy, finding and tapping
The error message I receive is:
FlutterDriver: tap message is taking a long time to complete...
I figured out that showDialog() presents a ModalBarrier to stop user input while the dialog is shown.
The trick to close the showDialog is to find by type passing in the ModalBarrier as shown here:
await driver.tap(find.byType(ModalBarrier));
Add
Navigator.of(context).pop(false);
after
await driver.tap(find.byValueKey('moredots'));