FLUTTER button onpressed navigator push got errors - flutter

I've made a button to send data to firebase. I want when the button is pressed, the button is not only to send data to firebase, but I want the button makes users can go back to the homepage.
I used setData for sending the data, and I used Navigator.push, but the navigator push is errors.
final sdButton = Material(
elevation: 5,
borderRadius: BorderRadius.circular(5),
color: const Color(0xFFD3DEDC),
child: MaterialButton(
padding: const EdgeInsets.fromLTRB(20,15,20,15),
minWidth: MediaQuery.of(context).size.width,
onPressed: setData {
Navigator.push(context,
MaterialPageRoute(
builder:(context) => Homepage(),));
},
and this is the code for setData
setData(){
dref.child(user!.uid).set({
'user_id' : user!.uid,
'intime' : '15.17'
}
);
}

onPressed: () {
setData();
Navigator.push(context, MaterialPageRoute(
builder:(context) => Homepage(),));
},

Please update onPress method to like as below.
onPressed: (){
setData();
Navigator.push(context,
MaterialPageRoute(
builder:(context) => Homepage(),));
},

Related

How to pop out double alert message

I am new to Flutter. I made my first pop out confirmation alert dialog (Figure 1) but I want to pop another alert dialog window after.
What I am trying to achieve, it's the following: after I click Yes (Figure 2) the app would lead me to my homescreen and pop out another alert dialog.
You could create a method for the second Alert to show up, and call it when you click "YES" on the first one.
void showSecond(BuildContext context) {
return showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text("Thank you for paying with us"),
content: Icon(Icons.check_circle_outline),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('Okay'),
),
],
),
);
}
and your onPressed() of "YES" in the first alert should look something like:
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => const SuccessPay()));
showSecond(context);
},
It was a bit hard to replicate your code from an image, so if something it's not accurate let me now. For the next time, post your code in a code block instead of a picture :)
you can call showAlertDialog to show second popup.(you can create new method to show second popup as content is different)This line can be added after
Navigator.of(context).pop() of first popup
You can use the .then() method. Call it if the user pressed the "YES" button.
Add value when poping your dialog like this Navigator.of(dialogCtx).pop(true);
showDialog(
context: context,
builder: (dialogCtx) => AlertDialog(
// title:
// content:
),
actions: [
TextButton(
onPressed: () {
Navigator.of(dialogCtx).pop(false);
},
child: const Text('CANCEL'),
),
TextButton(
onPressed: () {
Navigator.of(dialogCtx).pop(true);
},
child: const Text('YES'),
),
],
),
).then(
(value) => {
if (value == true)
{
// display the next dialog with the scaffolds context
},
},
);

Flutter: How to update FloatingActionButton color on condition?

I want to update the color of the FloatingActionButton on certain conditions(after the user update his values) I manage to set the color but only if the user click on the button where should I put the setstate or how can I achieve that?
FloatingActionButton.extended(
onPressed: (){
setState(() {
if(departureCity!=null && arrivalCity!=null && bol){
bol2=true;
Navigator.push(
context,
//MaterialPageRoute(builder: (context) => const SeatScreen()),
MaterialPageRoute(builder: (context) => BusSearchResults(departureCity,arrivalCity,_date)),
);
}
});
},
label: const Text('Search'),
backgroundColor: bol2? Color(0xff5348bf):Color(0xffd3d3d3) ,
),
bol2 = !bol2;
just write this instead of (bol2 = true ;) in your setState();
Just change the value of bol2 to the opposite every time on click like given below. And try not to wrap everything in setState() since it's unnecessary.
FloatingActionButton.extended(
onPressed: (){
setState(() {
if(bol2){
bol2=false;
}
else{
bol2=true;
}
});
if(departureCity!=null && arrivalCity!=null){
bol2=true;
Navigator.push(
context,
MaterialPageRoute(builder: (context) =>BusSearchResults(departureCity,arrivalCity,_date)),
);
}
},
label: const Text('Search'),
backgroundColor: bol2? Color(0xff5348bf):Color(0xffd3d3d3),
),

Opening next page after period of time | flutter dart

i have a button which is rotating after click on it (code is shortened to be more readable).
What is the problem ?
When user clicks button, it rotates, ( function animation() does that ) and navigate to next page, but problem is when user clicks button few times before a full rotation, then after that 1000ms it opens a lot of FriendsPageEditable and he must to go back pressing navigation back button.
What i want :
i want to this "extra sites" won't show up.
Ways to resolve this problem which i can't implement bcs i dont know how:
Disable button after this first click to navigation.
Disable it off for a period of time for example that 1000 ms.
I guess stop using Future.delayed but i dont know what we could do later.
bool buttonEnabled = true;
floatingActionButton: FloatingActionButton(
onPressed: () async {
animation();
await Future.delayed(const Duration(milliseconds: 1000));
if (buttonEnabled) {
buttonEnabled = true;
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const FriendsPageEditable()));
} else {
null;
}
},
child: const Icon(
Icons.settings,
),
),
Malak try this:
bool buttonClicked = false;
floatingActionButton: FloatingActionButton(
onPressed: () async {
// Then check if the button has been clicked
if(!buttonClicked)
buttonClicked = true;
else
return;
animation();
await Future.delayed(const Duration(milliseconds: 1000), (){
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const FriendsPageEditable()));
});
buttonClicked = !buttonClicked;
},
child: const Icon(
Icons.settings,
),
),

Data not being passed in Navigator.pop

I'm trying to pass data from a screen with navigator.pop, it shows the result in the second screen, but in the first screen the data is always null. I tried some things on it but didn't work.
Here I'm calling the second screen:
onPressed: () async {
final result = await push(context, PickMaterialPage());
print("Result $result");
},
In this one is when I want to pass the data for the first screen with navigator.pop, It's an object.
Card card(int index, context) {
RequisicaoMaterial material = materiais[index];
try {
return Card(
color: Colors.grey[300],
elevation: 6.0,
margin: new EdgeInsets.symmetric(horizontal: 15.0, vertical: 10.0),
child: GestureDetector(
onTap: () => pop(context, material),
child: CustomListTile(
Icons.shopping_cart_outlined,
'${material.codMaterial} - ${material.nomeMaterial}',
'${material.codUnidade} - ${material.sigla} - ${material.classificacao}',
null,
),
));
} catch (e) {
print(e);
}
I don't want to use constructor in this one, i needed to make it simple and only get the picked data to show in the first screen.
output $result = null
PUSH THE SCREEN
String result = await Navigator.push(
context,
MaterialPageRoute(
builder: (_) => AddTenantToRoomFlyPage(),
),
);
POP THE SCREEN
Navigator.pop(context, "RETURN VALUE");
Do it like this
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (_) => PickMaterialPage(),
)).then((value) => print(value)),

How show interstitial ads by number of clicks (google_mobile_ads)

I want to show an interstitial ads in 10 clicks. Is there a way to do this? Or how I can show only 1 time at the opening of the app? I'm just trying to find how to use it.
ElevatedButton(
child: Text("Ads"),
onPressed: () {
_interstitialAd.show();
Navigator.push(context,
MaterialPageRoute(builder: (context) => PageTwo()));
},
),
You need to keep a counter in your state somewhere and and call the method to show the ad only when it's a multiple of 10.
//A field of your state class
int counter = 0;
//...somewhere in build
ElevatedButton(
child: Text("Ads"),
onPressed: () {
if(counter%10 == 0) {
_interstitialAd.show();
}
counter++;
Navigator.push(context,
MaterialPageRoute(builder: (context) => PageTwo()));
},
),