import 'dart:io'; import 'package:firebase_core/firebase_core.dart'; import 'package:flare_flutter/flare_actor.dart'; import 'package:flutter/material.dart'; import 'package:local_auth/local_auth.dart'; import 'package:provider/provider.dart'; import 'package:test_sa/controllers/providers/api/user_provider.dart'; import 'package:test_sa/controllers/providers/settings/setting_provider.dart'; import 'package:test_sa/extensions/widget_extensions.dart'; import 'package:test_sa/new_views/pages/land_page/land_page.dart'; import 'package:test_sa/new_views/pages/login_page.dart'; import '../../models/size_config.dart'; class SplashPage extends StatefulWidget { static const String routeName = '/splash_page'; const SplashPage({Key? key}) : super(key: key); @override State createState() => _SplashPageState(); } class _SplashPageState extends State { late SettingProvider _settingProvider; late UserProvider _userProvider; bool loading = false; @override void initState() { Firebase.initializeApp(); // NotificationManger.initialisation((notificationDetails) { // // todo @sikander, check notifications payload, because notification model is different to need to check from backend // // SystemNotificationModel notification = SystemNotificationModel.fromJson(json.decode(notificationDetails.payload)); // // if (notification.path == null || notification.path.isEmpty) return; // // Navigator.pushNamed(context, notification.path, arguments: notification.requestId); // // isnotificationCame = true; // "initialisation:${notificationDetails?.toString()}".showToast; // // // Navigator.of(context).push(MaterialPageRoute( // // builder: (_) => ServiceRequestDetailsPage( // // serviceRequest: ServiceRequest(id: "72348"), // // ))); // // }, (id, title, body, payload) async {}); super.initState(); } void checkTokenValidity(String token) async { setState(() { loading = true; }); bool isValid = await _settingProvider.checkUserTokenValidation(token); setState(() { loading = false; }); if (isValid && _settingProvider.isLocalAuthEnable) { bool isSuccess = await checkDualAuthentication(); if (isSuccess) { _userProvider.setUser(_settingProvider.user!); Navigator.of(context).pushNamedAndRemoveUntil(LandPage.routeName, (routes) => true); } else { Navigator.of(context).pushNamedAndRemoveUntil(LoginPage.routeName, (routes) => true); } } else { Navigator.of(context).pushNamedAndRemoveUntil(LoginPage.routeName, (routes) => true); } } Future checkDualAuthentication() async { return await _settingProvider.auth.authenticate( localizedReason: Platform.isAndroid ? "Scan your fingerprint to authenticate" : "Scan with face id to authenticate", options: const AuthenticationOptions(), ); } @override Widget build(BuildContext context) { SizeConfig.init(context); _settingProvider = Provider.of(context, listen: false); _userProvider = Provider.of(context, listen: false); return Scaffold( body: Stack( alignment: Alignment.center, children: [ if (loading) const Positioned( bottom: 100, child: SizedBox( width: 36, height: 36, child: CircularProgressIndicator( color: Color(0xff4A8DB7), strokeWidth: 3, ), )), SizedBox( width: MediaQuery.of(context).size.width / 1.1, child: FlareActor( "assets/rives/atoms_splash.flr", fit: BoxFit.contain, animation: "splash", callback: (animation) async { if (_settingProvider.isLoaded && (_settingProvider.user != null)) { checkTokenValidity(_settingProvider.user!.token!); } else { Navigator.of(context).pushNamedAndRemoveUntil(LoginPage.routeName, (routes) => true); } // if (_settingProvider.isLoaded && (_settingProvider.user?.isLiveToken ?? false)) { // _userProvider.user = _settingProvider.user; // Navigator.of(context).pushNamedAndRemoveUntil(LandPage.routeName, (routes) => true); // // if(isnotificationCame) // // Navigator.of(context).push(MaterialPageRoute( // // builder: (_) => ServiceRequestDetailsPage( // // serviceRequest: ServiceRequest(id: "72348"), // // ))); // /// The below line for the new design // // Navigator.of(context).pushNamedAndRemoveUntil(LandPage.routeName, (routes) => true); // } }, ), ).center, ], ), ); } }