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 {
try {
await loadable;
emit(state.copyWith(loaded: [...state.loaded, loadable]));
} catch (error, stackTrace) {
emit(state.copyWith(error: '$error'));
}
}).toList();
await Future.wait(all);
}

@ -8,6 +8,7 @@ class AssetsManagerState extends Equatable {
const AssetsManagerState({
required this.loadables,
required this.loaded,
this.error,
});
/// {@macro assets_manager_state}
@ -20,22 +21,26 @@ class AssetsManagerState extends Equatable {
/// List of loaded futures
final List<Future> loaded;
final String? error;
/// Returns a value between 0 and 1 to indicate the loading progress
double get progress =>
loadables.isEmpty ? 0 : loaded.length / loadables.length;
/// 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
/// updated
AssetsManagerState copyWith({
List<Future>? loadables,
List<Future>? loaded,
String? error,
}) {
return AssetsManagerState(
loadables: loadables ?? this.loadables,
loaded: loaded ?? this.loaded,
error: error ?? this.error,
);
}

@ -17,6 +17,8 @@ class AssetsLoadingPage extends StatelessWidget {
Widget build(BuildContext context) {
final l10n = context.l10n;
final headline1 = Theme.of(context).textTheme.headline1;
return BlocBuilder<AssetsManagerCubit, AssetsManagerState>(
builder: (context, state) {
return Container(
decoration: const CrtBackground(),
child: Center(
@ -35,15 +37,18 @@ class AssetsLoadingPage extends StatelessWidget {
const SizedBox(height: 40),
FractionallySizedBox(
widthFactor: 0.8,
child: BlocBuilder<AssetsManagerCubit, AssetsManagerState>(
builder: (context, state) {
return PinballLoadingIndicator(value: state.progress);
},
child: PinballLoadingIndicator(value: state.progress),
),
if (state.error != null)
Text(
state.error!,
style: Theme.of(context).textTheme.headline4,
),
],
),
),
);
},
);
}
}

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

Loading…
Cancel
Save