flutter_future_progress_dialog 1.3.1 flutter_future_progress_dialog: ^1.3.1 copied to clipboard
Allows to display progress dialog while the Future function is evaluated.
flutter_future_progress_dialog #
Show progress dialog with animation while waiting for Future completion and then return the result of that Future.
Features #
Getting started #
- install the library
flutter pub add flutter_future_progress_dialog
- import the library
import 'package:flutter_future_progress_dialog/flutter_future_progress_dialog.dart';
Usage #
Working example can be found in /example directory.
Here is a short example of showProgressDialog
usage.
Call the showProgressDialog
inside your function. Pass context
and future
arguments. Then handle
result.
Alternatively you can use showCupertinoProgressDialog
to show cupertino-styled dialog and showAdaptiveProgressDialog
to show dialog matching host OS.
Future<String> myFuture() async {
await Future.delayed(const Duration(seconds: 2));
return 'my string';
}
Future<void> yourFunction(BuildContext context) async {
final result = await showProgressDialog(
context: context,
future: () => myFuture(),
);
if (!mounted) {
return;
}
switch (result) {
case ResultError(error: final error):
await showDialog(
context: context,
builder: (context) {
return AlertDialog(
content: Text(
'$error',
textAlign: TextAlign.center,
),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text(
'OK',
),
),
],
);
},
);
case ResultOk<String>(value: final value):
// value variable would hold the 'my string' value here
break;
}
}
Optionally you can pass a builder
to have a custom progress dialog
Future<Result<LongRunningTaskResult>> buttonCallback({
required BuildContext context,
}) async {
return await showProgressDialog(
future: () => myLongRunningTask(),
context: context,
builder: (context) => AlertDialog(
content: Text('I am loading now'),
),
);
}