splash screen error handling added

This commit is contained in:
MohammadTaha Basiri 2022-01-29 13:59:20 +03:30
parent fdbbc914af
commit 0553cc75ae
1 changed files with 60 additions and 27 deletions

View File

@ -10,10 +10,12 @@ import 'package:didvan/services/app_initalizer.dart';
import 'package:didvan/services/network/request.dart'; import 'package:didvan/services/network/request.dart';
import 'package:didvan/services/storage/storage.dart'; import 'package:didvan/services/storage/storage.dart';
import 'package:didvan/utils/action_sheet.dart'; import 'package:didvan/utils/action_sheet.dart';
import 'package:didvan/widgets/didvan/button.dart';
import 'package:didvan/widgets/logos/didvan_horizontal_logo.dart'; import 'package:didvan/widgets/logos/didvan_horizontal_logo.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:universal_html/html.dart' as html; import 'package:universal_html/html.dart' as html;
@ -26,6 +28,7 @@ class Splash extends StatefulWidget {
class _SplashState extends State<Splash> { class _SplashState extends State<Splash> {
bool _isGettingThemeData = true; bool _isGettingThemeData = true;
bool _errorOccured = false;
@override @override
void initState() { void initState() {
@ -51,40 +54,70 @@ class _SplashState extends State<Splash> {
alignment: Alignment.center, alignment: Alignment.center,
padding: const EdgeInsets.all(60), padding: const EdgeInsets.all(60),
color: Theme.of(context).colorScheme.background, color: Theme.of(context).colorScheme.background,
child: const DidvanVerticalLogo(), child: Column(
children: [
const SizedBox(height: 70),
const Expanded(
child: DidvanVerticalLogo(),
),
if (!_errorOccured)
SpinKitSpinningLines(
color: Theme.of(context).colorScheme.primary,
),
if (_errorOccured) const SizedBox(height: 38),
if (_errorOccured)
DidvanButton(
width: 120,
height: 40,
title: 'تلاش مجدد',
onPressed: () {
setState(() {
_errorOccured = false;
});
_initialize();
},
),
],
),
), ),
), ),
); );
} }
Future<void> _initialize() async { Future<void> _initialize() async {
ActionSheetUtils.context = navigatorKey.currentContext!; try {
if (kIsWeb) { ActionSheetUtils.context = navigatorKey.currentContext!;
html.window.onBeforeUnload.listen((event) { if (kIsWeb) {
StorageService.webStorage html.window.onBeforeUnload.listen((event) {
.removeWhere((key, value) => key == 'image-cache'); StorageService.webStorage
.removeWhere((key, value) => key == 'image-cache');
});
}
await AppInitializer.setupServices();
final ThemeMode themeMode = await AppInitializer.initilizeSettings();
context.read<ThemeProvider>().themeMode = themeMode;
await Future.delayed(
const Duration(milliseconds: 200),
() => setState(() {
_isGettingThemeData = false;
}),
);
final userProvider = context.read<UserProvider>();
final String? token = await userProvider.setAndGetToken();
if (token != null) {
log(token);
RequestService.token = token;
await userProvider.getUserInfo();
await context.read<ServerDataProvider>().getData();
}
Navigator.of(context).pushReplacementNamed(
token == null ? Routes.authenticaion : Routes.home,
arguments: token == null ? false : null,
);
} catch (e) {
setState(() {
_errorOccured = true;
}); });
} }
await AppInitializer.setupServices();
final ThemeMode themeMode = await AppInitializer.initilizeSettings();
context.read<ThemeProvider>().themeMode = themeMode;
await Future.delayed(
const Duration(milliseconds: 200),
() => setState(() {
_isGettingThemeData = false;
}),
);
final userProvider = context.read<UserProvider>();
final String? token = await userProvider.setAndGetToken();
if (token != null) {
log(token);
RequestService.token = token;
await userProvider.getUserInfo();
await context.read<ServerDataProvider>().getData();
}
Navigator.of(context).pushReplacementNamed(
token == null ? Routes.authenticaion : Routes.home,
arguments: token == null ? false : null,
);
} }
} }