minimum_duration

Last updated:

0 purchases

minimum_duration Image
minimum_duration Images
Add to Cart

Description:

minimum duration

Minimum Duration #
A simple utility function used to force an operation to take at least a given amount of time.
This is most often useful to ensure that some "loading" animation is displayed at least long enough to be seen by the user, even if the loading operation completes very quickly.
This is a super simple implementation, but it helps me avoid writing the same code over and over in every app, and I hope it helps you too!
Getting started #
Just install the package.
dart/flutter pub add minimum_duration
copied to clipboard
Usage #
Basic Usage #
Using MinimumDuration is easy! Simply pass a function to MinimumDuration.run.
setLoading(true);

final result = await MinimumDuration.run(
() async => someAsyncOperation(),
);

setLoading(false);
copied to clipboard
In this example, the return value from someAsyncOperation will be assigned to result after at least 500ms (the default duration) has elapsed.
If your function takes longer than the specified duration, it will be returned immediately upon completion.
Custom Duration #
The default Duration is 500ms, but this can easily be overridden by providing a value to the duration parameter.
final result = await MinimumDuration.run(
() async => someAsyncOperation(),
duration: const Duration(seconds: 1),
);
copied to clipboard
Global Default Duration #
The default can also be set globally by changing the MinimumDuration.duration property.
// somewhere in your app's initialization code
MinimumDuration.duration = const Duration(seconds: 1);

// later, in your app's logic
// this will run for the "default" duration of 1 second
final result = await MinimumDuration.run(
() async => someAsyncOperation(),
);
copied to clipboard
This is useful if the default does not fit your use-case and you don't want to pass a custom duration to every call to MinimumDuration.run.

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.