asyncstate 2.0.2 asyncstate: ^2.0.2 copied to clipboard
This package will help you to call a loading in your async methods very easily, and you don't need to worried when it's is starting or finishing.
Async State
Usage #
1 - Wrap your MaterialApp Or CurpertinoApp with the AsyncStateBuilder.2 - Get the "navigatorObserver" from the builder function and add it to your component's "navigatorObservers".
3 - If you want, you can add a widget to "CustomLoader".
Code example:
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
/// Here you need to wrap your MaterialApp with the AsyncStateBuilder
return AsyncStateBuilder(
/// Here you can customize your default loading that will show every transaction
/// Leave it and it will show a simple CircularProgress.adaptive indicator
customLoader: const MyLoading(),
builder: (navigatorObserver) => MaterialApp(
themeMode: ThemeMode.dark,
/// Here you need to pass the navigatorObserver to the MaterialApp
navigatorObservers: [navigatorObserver],
home: HomePage(),
),
);
}
}
The asyncronous loader call methods allow you to define a LoaderType, and pass the widget to be displayed. #
Future<void> loadMoreSnackBar() async {
await Future.delayed(const Duration(seconds: 10)).asyncLoader(
loaderType: LoaderType.snackBar,
customLoader: const SnackBar(
content: Text('Loading more...'),
duration: Duration(seconds: 90),
),
);
}
Future<void> loadMoreMaterialBanner() async {
await Future.delayed(const Duration(seconds: 10)).asyncLoader(
loaderType: LoaderType.materialBanner,
customLoader: const MaterialBanner(
content: Text('Loading more...'),
actions: [SizedBox.shrink()],
),
);
}
Mixin AsyncStateMixin #
Methods
#
callAsyncLoader - Automatically call your loader during async calls.showMaterialBanner - Allow you call the MaterialBanner on ScaffoldMessenger.
showSnackBar - Allow you call the SnackBar on ScaffoldMessenger.
showDialog - Allow you call the Dialog on showDialog.
showBottomSheet - Allow you call the BottomSheet on showBottomSheet.
class HomeController with AsyncStateMixin {
Future goBack(Function callback) async {
return await callAsyncLoader(
Future.delayed(
const Duration(seconds: 3),
() {
callback();
},
),
);
}
Future<void> loginError() async {
try {
await _fakeError().asyncLoader();
} catch (e) {
showMaterialBanner(
materialBanner: MaterialBanner(
actions: const [
SizedBox.shrink(),
],
content: Text(
e.toString(),
),
),
);
}
}
}
Extension anywhere (.asyncLoader()) #
class HomeController{
Future<bool> login() async {
try {
return await Future.delayed(const Duration(seconds: 3), () {
return true;
}).asyncLoader(
customLoader: const MyCustomLoadingWidget(
text: 'Sign in! Hold on!!!!',
));
} catch (e) {
return false;
}
}
}
Call in your View like this #
ElevatedButton(
onPressed: () async {
if (await controller.login()) {
///Use a Navigator to go to another page if your login is true
///You don't need to care about start of finish the loading, just what to do after or before!
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondPage(
controller: controller,
)),
);
}
},
child: const Text('Sign in!'),
)
Bugs or Requests #
If you encounter any problems feel free to open an issue. If you feel the library is missing a feature, please raise a ticket on GitHub and I'll look into it. Pull request are also welcome.
Contributors #
Leonardo Serrano |
Marcus Brasizza |
If you like what I do, maybe consider buying me a coffee/tea π₯Ίππ