fix: loading screen

pull/438/head
Jorge Coca 3 years ago
parent 11c076c386
commit 632c2dc95e

@ -31,8 +31,12 @@ class AssetsManagerCubit extends Cubit<AssetsManagerState> {
), ),
); );
final all = state.loadables.map((loadable) async { final all = state.loadables.map((loadable) async {
await loadable; try {
emit(state.copyWith(loaded: [...state.loaded, loadable])); await loadable;
emit(state.copyWith(loaded: [...state.loaded, loadable]));
} catch (error, stackTrace) {
emit(state.copyWith(error: '$error'));
}
}).toList(); }).toList();
await Future.wait(all); await Future.wait(all);
} }

@ -8,6 +8,7 @@ class AssetsManagerState extends Equatable {
const AssetsManagerState({ const AssetsManagerState({
required this.loadables, required this.loadables,
required this.loaded, required this.loaded,
this.error,
}); });
/// {@macro assets_manager_state} /// {@macro assets_manager_state}
@ -20,22 +21,26 @@ class AssetsManagerState extends Equatable {
/// List of loaded futures /// List of loaded futures
final List<Future> loaded; final List<Future> loaded;
final String? error;
/// Returns a value between 0 and 1 to indicate the loading progress /// Returns a value between 0 and 1 to indicate the loading progress
double get progress => double get progress =>
loadables.isEmpty ? 0 : loaded.length / loadables.length; loadables.isEmpty ? 0 : loaded.length / loadables.length;
/// Only returns false if all the assets have been loaded /// Only returns false if all the assets have been loaded
bool get isLoading => progress != 1; bool get isLoading => progress != 1 && error == null;
/// Returns a copy of this instance with the given parameters /// Returns a copy of this instance with the given parameters
/// updated /// updated
AssetsManagerState copyWith({ AssetsManagerState copyWith({
List<Future>? loadables, List<Future>? loadables,
List<Future>? loaded, List<Future>? loaded,
String? error,
}) { }) {
return AssetsManagerState( return AssetsManagerState(
loadables: loadables ?? this.loadables, loadables: loadables ?? this.loadables,
loaded: loaded ?? this.loaded, loaded: loaded ?? this.loaded,
error: error ?? this.error,
); );
} }

@ -17,33 +17,38 @@ class AssetsLoadingPage extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
final l10n = context.l10n; final l10n = context.l10n;
final headline1 = Theme.of(context).textTheme.headline1; final headline1 = Theme.of(context).textTheme.headline1;
return Container( return BlocBuilder<AssetsManagerCubit, AssetsManagerState>(
decoration: const CrtBackground(), builder: (context, state) {
child: Center( return Container(
child: Column( decoration: const CrtBackground(),
mainAxisSize: MainAxisSize.min, child: Center(
children: [ child: Column(
Padding( mainAxisSize: MainAxisSize.min,
padding: const EdgeInsets.symmetric(horizontal: 20), children: [
child: Assets.images.loadingGame.ioPinball.image(), Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Assets.images.loadingGame.ioPinball.image(),
),
const SizedBox(height: 40),
AnimatedEllipsisText(
l10n.loading,
style: headline1,
),
const SizedBox(height: 40),
FractionallySizedBox(
widthFactor: 0.8,
child: PinballLoadingIndicator(value: state.progress),
),
if (state.error != null)
Text(
state.error!,
style: Theme.of(context).textTheme.headline4,
),
],
), ),
const SizedBox(height: 40), ),
AnimatedEllipsisText( );
l10n.loading, },
style: headline1,
),
const SizedBox(height: 40),
FractionallySizedBox(
widthFactor: 0.8,
child: BlocBuilder<AssetsManagerCubit, AssetsManagerState>(
builder: (context, state) {
return PinballLoadingIndicator(value: state.progress);
},
),
),
],
),
),
); );
} }
} }

@ -111,7 +111,7 @@ class PinballGame extends PinballForge2DGame
children: [ children: [
ZCanvasComponent( ZCanvasComponent(
children: [ children: [
if (!platformHelper.isMobile) ArcadeBackground(), ArcadeBackground(),
BoardBackgroundSpriteComponent(), BoardBackgroundSpriteComponent(),
Boundaries(), Boundaries(),
Backbox( Backbox(

Loading…
Cancel
Save