import 'dart:async'; import 'package:camera/camera.dart'; import 'package:flutter/material.dart'; import 'package:page_transition/page_transition.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:teso/Services/DynamicLinks.dart'; import 'package:teso/Services/locator.dart'; import 'package:teso/main_screen.dart'; import 'package:teso/util/SizeConfig.dart'; import 'package:teso/util/consts.dart'; import 'LandingPage.dart'; import 'package:http/http.dart' as http; class Splash extends StatefulWidget { @override _SplashState createState() => _SplashState(); } class _SplashState extends State with TickerProviderStateMixin { List connectedCameras; Animation _animation; AnimationController _controller; final DynamicLinkService _dynamicLinkService = locator(); gotoPersonnalServer() { _controller = AnimationController( duration: const Duration(milliseconds: 2000), vsync: this, value: 0.1); _animation = CurvedAnimation(parent: _controller, curve: Curves.bounceInOut); _controller.forward(); } var timeout = const Duration(seconds: 2); var ms = const Duration(milliseconds: 1); bool gone = false; startTimeout() { // return new Timer(Duration(seconds: 5), loggedIn); loggedIn(); } void handleTimeout() { changeScreen(); } changeScreen() async { Navigator.pushReplacement( context, PageTransition( type: PageTransitionType.rightToLeft, child: LandingPage( connectedCameras: connectedCameras, ), ), ); //Provider.of(context, listen: false).getFeeds(); } void loggedIn() async { await _dynamicLinkService.handleDynamicLinks(); SharedPreferences prefs = await SharedPreferences.getInstance(); String tokens = prefs.getString("tokensTeso"); if (tokens != null) { Navigator.pushReplacement( context, PageTransition( type: PageTransitionType.rightToLeft, child: MainScreens( connectedCameras: connectedCameras, ), ), ); } else { changeScreen(); } } Future apiversionCheck() async { try { SharedPreferences prefs = await SharedPreferences.getInstance(); String token = prefs.getString('tokensTeso'); Map requestHeaders = { 'Content-type': 'application/json', 'Authorization': token }; var register = serverLocation + 'api/version/current'; var client = await http.get(Uri.parse(register)).timeout( const Duration(seconds: 5), onTimeout: () { return http.Response( 'Error', 408); // Request Timeout response status code }, ); if (client.statusCode == 200) { String currentVersion = prefs.getString("api-version") != null ? prefs.getString("api-version") : ""; if (currentVersion != client.body) { try { var register = Uri.parse(serverLocation + 'api/logout'); await http.get(register, headers: requestHeaders); } catch (_) {} await prefs.clear(); await prefs.setBool("launched", true); await prefs.setString("api-version", client.body); } } return true; } catch (e) { print(e); return false; } } @override void initState() { super.initState(); gotoPersonnalServer(); availableCameras().then((value) { connectedCameras = value; }); apiversionCheck().then((t) => {startTimeout()}); } @override Widget build(BuildContext context) { SizeConfig().init(context); return Scaffold( backgroundColor: Colors.white, body: Center( child: Column( // mainAxisSize: MainAxisSize.max, crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [ Container( alignment: Alignment.center, child: ScaleTransition( scale: _animation, child: Image.asset( "assets/images/tesoCouponInsignia.png", width: 80.0, ), ), ), SizedBox( height: 20, ), Text( "${Constants.appName}", style: TextStyle( color: tesoGold, fontSize: 25, fontWeight: FontWeight.bold, ), ), ], ), ), ); } }