import 'dart:async'; import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:hris_selfservice_mobile/Screens/Login/login_screen.dart'; import 'package:hris_selfservice_mobile/Screens/Splash/loginstatus_post.dart'; import 'package:lottie/lottie.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../Home/home_screen.dart'; import '../Splash/background.dart'; import 'package:url_launcher/url_launcher.dart'; import 'dart:developer' as logDev; class Splash extends StatefulWidget { const Splash({Key? key}) : super(key: key); @override _SplashState createState() => _SplashState(); } class _SplashState extends State { @override void initState() { super.initState(); Timer(Duration(seconds: 10), () => loginStatus(context)); } @override Widget build(BuildContext context) { return Scaffold( resizeToAvoidBottomInset: false, body: Stack( children: [ Background(), SplashScreen(), ], )); } } class SplashScreen extends StatelessWidget { const SplashScreen({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( body: Container( width: double.infinity, height: double.infinity, decoration: BoxDecoration( gradient: LinearGradient( begin: Alignment.topRight, end: Alignment.bottomRight, colors: [ Color(0xFFD21404), Color(0xFFFD7267), ])), child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Container( /*decoration: BoxDecoration( shape: BoxShape.circle, color: Colors.white ),*/ child: SizedBox( width: 250, height: 250, child: LottieBuilder.asset('assets/animation/animation_4.json', repeat: true), ), ), Text('Employee Self Service', style: GoogleFonts.knewave(fontSize: 22, color: Colors.white)) ], ), ), )); } } Future loginStatus(BuildContext context) async { final SharedPreferences prefs = await SharedPreferences.getInstance(); String? version = prefs.getString('version'); String? device = prefs.getString('device'); String? session = prefs.getString('session'); String? notif_token = prefs.getString('notif_token'); if (session == null) { session = ""; version = ""; device = ""; notif_token = ""; } logDev.log(session, name: "SESSIONNYA"); LoginStatus_Post.connectToAPI(version!, device!, session, notif_token!) .then((valueResult) async { Map object = json.decode(valueResult); logDev.log(valueResult, name: "LOGIN STATUS!"); if (object.containsKey("result").toString() == "true") { String status = object['result']['status'].toString(); if (status == "success") { Fluttertoast.showToast( msg: "You are already logged in", toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.CENTER, timeInSecForIosWeb: 1, textColor: Colors.white, fontSize: 16.0); Navigator.pushReplacement( context, MaterialPageRoute(builder: (context) => HomeView())); } else if (status == "failed") { String message = object['result']['message'].toString(); if (message == "Not Logged In") { Fluttertoast.showToast( msg: message, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.CENTER, timeInSecForIosWeb: 1, textColor: Colors.white, fontSize: 16.0); Navigator.pushReplacement( context, MaterialPageRoute(builder: (context) => LoginView())); } else { String title = object['result']['title'].toString(); String action = object['result']['action'].toString(); String block = object['result']['block'].toString(); if (block == "false") { Widget okButton = TextButton( child: Text(action), onPressed: () { _launchURL(Uri.parse("https://play.google.com/store/apps")); SystemChannels.platform.invokeMethod('SystemNavigator.pop'); }, ); Widget noButton = TextButton( child: Text("Later"), onPressed: () { if (session == ""){ Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => LoginView())); } else { Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => HomeView())); } }, ); // set up the AlertDialog AlertDialog alert = AlertDialog( title: Text("Employee Self Service"), content: Text(title + "\n" + message), actions: [noButton, okButton], ); // show the dialog showDialog( context: context, builder: (BuildContext context) { return alert; }, ); } else if (block == "true") { Widget okButton = TextButton( child: Text(action), onPressed: () { if (action == "update") { _launchURL(Uri.parse("https://play.google.com/store/apps")); SystemChannels.platform.invokeMethod('SystemNavigator.pop'); } else if (action == "close") { SystemChannels.platform.invokeMethod('SystemNavigator.pop'); } }, ); // set up the AlertDialog AlertDialog alert = AlertDialog( title: Text("Employee Self Service"), content: Text(title + "\n" + message), actions: [okButton], ); // show the dialog showDialog( context: context, builder: (BuildContext context) { return alert; }, ); } } } } else { alertDialogFailedResponse(context); /*Fluttertoast.showToast( msg: "Server Response Error", toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.CENTER, timeInSecForIosWeb: 1, textColor: Colors.white, fontSize: 16.0);*/ } }); } _launchURL(Uri url) async { if (await canLaunchUrl(url)) { await launchUrl(url); } else { throw 'Could not launch $url'; } } alertDialogFailedResponse(BuildContext context){ Widget okButton = TextButton( child: Text("Refresh"), onPressed: () { Navigator.of(context, rootNavigator: true).pop(); Navigator.pushReplacement(context, MaterialPageRoute( builder: (context) => SplashScreen())); }, ); Widget noButton = TextButton( child: Text("Back"), onPressed: () { Navigator.of(context, rootNavigator: true).pop(); Navigator.pop(context); }, ); // set up the AlertDialog AlertDialog alert = AlertDialog( title: Text("Employee Self Service"), content: Text("Server Response Error"), actions: [ noButton, okButton, ], ); // show the dialog showDialog( context: context, builder: (BuildContext context) { return alert; }, ); }