generated from glemartret/lm_labs_app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1689232
commit 92d3397
Showing
8 changed files
with
219 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
import 'package:lm_labs_utils/localization.dart'; | ||
|
||
class LLApp extends ConsumerWidget { | ||
final WidgetBuilder appBuilder; | ||
final void Function(WidgetRef, FutureProvider<void>) onRetry; // () { | ||
// ref.invalidate(onboardingRepositoryProvider); | ||
// ref.invalidate(appStartupProvider); | ||
// } | ||
final WidgetBuilder loadingBuilder; | ||
final Widget Function( | ||
BuildContext context, | ||
Object error, | ||
StackTrace? stackTrace, | ||
VoidCallback onRetry, | ||
) errorBuilder; | ||
|
||
final FutureProvider<void> initAppProvider; | ||
|
||
const LLApp({ | ||
required this.appBuilder, | ||
required this.initAppProvider, | ||
super.key, | ||
}) : onRetry = defaultOnRetry, | ||
loadingBuilder = defaultLoadingBuilder, | ||
errorBuilder = defaultErrorBuilder; | ||
|
||
const LLApp.custom({ | ||
required this.appBuilder, | ||
required this.initAppProvider, | ||
super.key, | ||
this.onRetry = defaultOnRetry, | ||
this.loadingBuilder = defaultLoadingBuilder, | ||
this.errorBuilder = defaultErrorBuilder, | ||
}); | ||
|
||
@override | ||
Widget build(BuildContext context, WidgetRef ref) => | ||
switch (ref.watch(initAppProvider)) { | ||
AsyncError(error: final error, stackTrace: final stackTrace) => | ||
errorBuilder( | ||
context, | ||
error, | ||
stackTrace, | ||
() => onRetry(ref, initAppProvider), | ||
), | ||
AsyncLoading() => loadingBuilder(context), | ||
AsyncData(value: final _) || AsyncValue() => appBuilder(context), | ||
}; | ||
|
||
@override | ||
void debugFillProperties(DiagnosticPropertiesBuilder properties) { | ||
super.debugFillProperties(properties); | ||
properties | ||
..add( | ||
ObjectFlagProperty<WidgetBuilder>.has('appBuilder', appBuilder), | ||
) | ||
..add( | ||
ObjectFlagProperty<WidgetBuilder>.has( | ||
'loadingBuilder', | ||
loadingBuilder, | ||
), | ||
) | ||
..add( | ||
ObjectFlagProperty< | ||
Widget Function( | ||
BuildContext context, | ||
Object error, | ||
StackTrace? stackTrace, | ||
VoidCallback onRetry, | ||
)>.has('errorBuilder', errorBuilder), | ||
) | ||
..add( | ||
ObjectFlagProperty< | ||
void Function( | ||
WidgetRef p1, | ||
FutureProvider<void> p2, | ||
)>.has('onRetry', onRetry), | ||
) | ||
..add( | ||
DiagnosticsProperty<FutureProvider<void>>( | ||
'initAppProvider', | ||
initAppProvider, | ||
), | ||
); | ||
} | ||
|
||
static Widget defaultErrorBuilder( | ||
BuildContext context, | ||
Object error, | ||
StackTrace? stackTrace, | ||
VoidCallback onRetry, | ||
) => | ||
_LLAppErrorWidget( | ||
message: error.toString(), | ||
onRetry: onRetry, | ||
); | ||
|
||
static Widget defaultLoadingBuilder(BuildContext context) => | ||
const _LLAppLoadingWidget(); | ||
|
||
static void defaultOnRetry( | ||
WidgetRef ref, | ||
FutureProvider<void> initAppProvider, | ||
) { | ||
ref.invalidate(initAppProvider); | ||
} | ||
} | ||
|
||
class _LLAppErrorWidget extends StatelessWidget { | ||
final String message; | ||
final VoidCallback onRetry; | ||
|
||
const _LLAppErrorWidget({ | ||
required this.message, | ||
required this.onRetry, | ||
}); | ||
|
||
@override | ||
Widget build(BuildContext context) => MaterialApp( | ||
home: Scaffold( | ||
body: Center( | ||
child: Column( | ||
mainAxisSize: MainAxisSize.min, | ||
children: [ | ||
Text(message, style: Theme.of(context).textTheme.headlineSmall), | ||
const SizedBox(height: 16), | ||
ElevatedButton( | ||
onPressed: onRetry, | ||
child: Text('Retry'.i18n), | ||
), | ||
], | ||
), | ||
), | ||
), | ||
); | ||
@override | ||
void debugFillProperties(DiagnosticPropertiesBuilder properties) { | ||
super.debugFillProperties(properties); | ||
properties | ||
..add(StringProperty('message', message)) | ||
..add(ObjectFlagProperty<VoidCallback>.has('onRetry', onRetry)); | ||
} | ||
} | ||
|
||
class _LLAppLoadingWidget extends StatelessWidget { | ||
const _LLAppLoadingWidget(); | ||
|
||
@override | ||
Widget build(BuildContext context) => const MaterialApp( | ||
home: Scaffold( | ||
body: Center( | ||
child: CircularProgressIndicator(), | ||
), | ||
), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export 'src/common_widgets/async_value_widget.dart'; | ||
export 'src/common_widgets/ll_app.dart'; | ||
export 'src/common_widgets/ll_app_bar.dart'; | ||
export 'src/common_widgets/ll_tappable.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.