How to disable button for 3 hrs in flutter - flutter

I am working with an app in which there is a need that if button is pressed then it should be disable for 3 hours and I didn't find any solution yet. Is there any way to achieve this task ? Even the app may close or user do other task on other screens .Please help!

I think You can Use Timer method in flutter to achive this:
Timer.periodic(. // assing new timer to our variable.
Duration(
milliseconds: 500,
), (timer) {
}

Related

How to add time to Bloc timer exemple

I am beginner on flutter with bloc, do you know the easiest way to add time on the timer in example https://bloclibrary.dev/#/fluttertimertutorial ?
Add time while timer is running
Thanks you :)

Manage Timer for each List item in the background in Flutter

I am working on a feature in an flutter application. The feature is that when some entry in server (DB) occurs, I get notification through socket connection and add that item in List. There is a button on the screen which shows a dialog box on clicking and shows that list of items. We can close the box also and again open when required.
Now I want to attach Timer to each item in the List. The purpose of that timer is to delete the item from the list after 1 minute. All that functionality of removing items must keep going in the background when dialog box doesn't appear. On opening the dialog box it must shows updated List each time.
Right now I am able to add items to the list and showing them in dialog box and it is working.
I just want to find out the solution of that background running task in which items automatically remove from list after the time for that item is up.
The easiest way to perform an action after one minute might be to just await a future like this:
Future<void> deleteAfter1Minute() async {
await Future.delayed(const Duration(minutes: 1));
// delete item from list
// ... your code
print('item deleted');
}
You can also initiate a timer which fires the callback after the given duration:
final timer = Timer(const Duration(minutes: 1), () {
// delete item from list
// ... your code
print('item deleted');
timer.cancel();
});
It might also help if you attached some code to better understand where you're coming from.
Best
you can use the background service packages for that
flutter_background_service
that is very good package for background service

Flutter upgrader, prompt showing only once

I try to force users to upgrade app each time there is a new version with https://pub.dev/packages/upgrader. It works but only once, when there is a new version. When the dialog is opened and the user closes app. It won't ask again for update. Anyone has solution to that?
the default value for upgrader to remind user to update app is 3 days if you want to change this time you can use durationUntilAlertAgain parameter
upgrader: Upgrader(
durationUntilAlertAgain: const Duration(minutes: 5),
),
for exmaple:
durationUntilAlertAgain: const Duration(minutes: 5),
in the above example if the user close app at 00:00 am and re-open it at 00:03 am the alert message will not appear however if user re-open it at 00:07 am or later the alert message will appear
alternatively you can force user to update the app by adding exit function to ignore or later button here is an example solve your question :
UpgradeAlert(
upgrader: Upgrader(
showLater: false,
onIgnore: () {
SystemNavigator.pop();
throw UnsupportedError('_');
},
),
child: HomePage(),
),
How frequent do you want dialog to pop up? Upgrader widget accepts an attribute durationUntilAlertAgain and its accpetion object of Duration(). You pass whatever duration you want dialog will pop up after that duration.

In Flutter,How to automate button without clicking after a period of time?

The idea is to check sms gateway if it is ok or not by sending a sms to a specific mobile number after every period of time. If I get response then it is all ok.
The problem is:
I can’t automate the button what I have to press. I want the button automatically clicked after a specific period of time that send request to the gateway.
Because it’s not possible for me to click the button after a period of time always.
You can either use Future.delayed or Timer to do this. Depending on your code, you might need to do this in background. There are various platform-specific solutions for background code execution, which I will not be getting into.
Now, to the solution:
Future.delayed example
Future _delayedFuture = Future.delayed(
const Duration(milliseconds: 500),
() {
// Call some function after delay of 500ms
},
);
Future.delayed cannot be cancelled, while timer can be.
Timer example:
Timer _timer = Timer(
const Duration(milliseconds: 500),
() {
// Call some function after delay of 500ms
},
);
If you want to cancel timer, use _timer.cancel()
You can also run timer periodically, by using Timer.periodic();

How to cause delay before open keyboard?

I am wondering how to cause delay of 1-2 seconds before open the keyboard.
I'm using this command for hide the bottom navigation and the status bar:
SystemChrome.setEnabledSystemUIOverlays([]);
and when I tap on TextFormField it's causing to lag.
All I need is to delay keyboard for 1 second and restore the Overlays.
Several ways to handle this.
You could add a listener to your TextField's TextEditingController()
and run the code when it's appropriate.
TextEditingController usage Flutter Cookbook
Intentionally adding lag to your app may not be a great solution. Try not to create new problems by solving problems.
Try to identify why this is causing a delay.
Have you tested on a physical device? Sometimes the Emulator or Simulator can produce lag if you're running many tasks or on an older machine.
Try using a future!.
await Future.delayed(const Duration(seconds: 2)).then((_) {
// open keyboard
});