import 'dart:async'; import 'dart:convert'; import 'dart:io'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:tower_app/Screens/Login/login_screen.dart'; import 'package:tower_app/Screens/Splash/loginstatus_post.dart'; import 'package:tower_app/constants.dart'; import 'package:internet_connection_checker/internet_connection_checker.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 { late StreamSubscription subscription; bool isDeviceConnected = false; bool isAlertSet = false; @override void initState() { //getConnectivity(); //Timer(Duration(seconds: 10), () => loginStatus(context)); Timer(Duration(seconds: 10), () => Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => LoginView()))); super.initState(); } /*getConnectivity() => subscription = Connectivity().onConnectivityChanged.listen( (ConnectivityResult result) async { isDeviceConnected = await InternetConnectionChecker().hasConnection; if (!isDeviceConnected && isAlertSet == false) { showConnectivityDialogBox(); setState(() => isAlertSet = true); } }, );*/ @override Widget build(BuildContext context) { return Scaffold( resizeToAvoidBottomInset: false, body: Stack( children: [ Background(), SplashScreen(), ], )); } /*showConnectivityDialogBox() => showCupertinoDialog( context: context, builder: (BuildContext context) => CupertinoAlertDialog( title: const Text('No Connection'), content: const Text('Please check your internet connectivity'), actions: [ Column( children: [ SizedBox( width: 250, height: 250, child: LottieBuilder.asset( //'assets/animation/animation_no_internet.json', 'assets/animation/animation_no_internet_3.json', repeat: true), ), ], ), TextButton( onPressed: () async { Navigator.pop(context, 'Cancel'); setState(() => isAlertSet = false); isDeviceConnected = await InternetConnectionChecker().hasConnection; if (!isDeviceConnected && isAlertSet == false) { showConnectivityDialogBox(); setState(() => isAlertSet = true); } }, child: const Text('Retry'), ), ], ), );*/ } class SplashScreen extends StatelessWidget { const SplashScreen({Key? key}) : super(key: key); @override Widget build(BuildContext context) { var size = MediaQuery.of(context).size; return Scaffold( body: Container( width: double.infinity, height: double.infinity, decoration: BoxDecoration( gradient: LinearGradient( begin: Alignment.topRight, end: Alignment.bottomRight, colors: [ Color(0xFF4858A7), Color(0xFF4858A7), Color(0xFF6474C6), //Color(0xFF8C99DD), //Color(0xFFFFFFFF) ])), child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Container( /*decoration: BoxDecoration( shape: BoxShape.circle, color: Colors.white ),*/ child: SizedBox( width: size.width * 0.55, //height: 250, child: Image.asset('assets/icons/tower_1.png') /*LottieBuilder.asset('assets/animation/animation_4.json', repeat: true, ),*/ ), ), Container( margin: EdgeInsets.only(top: 15), child: SizedBox( child: Image.asset('assets/icons/logo_bakti_1.png', width: size.width * 0.25,) /*LottieBuilder.asset('assets/animation/animation_4.json', repeat: true, ),*/ ), ), /* Text(appName, style: GoogleFonts.carterOne(fontSize: 25, 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) { version = "1.0.0"; session = ""; notif_token = ""; if (Platform.isAndroid){ device = "android"; } else if (Platform.isIOS){ device = "ios"; } } //logDev.log(session, name: "SESSIONNYA"); LoginStatus_Post.connectToAPI(version!, device!, session, notif_token!).then((valueResult) async { Map object = json.decode(valueResult); 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: () async { Uri url = Uri.parse(Uri.encodeFull("https://play.google.com/store/apps")); //final Uri uri = Uri(scheme: "https", host: "play.google.com"); if(!await launchUrl(url, mode: LaunchMode.externalApplication)) { throw "Can not launch url"; } }, ); 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(appName), content: Text(title + "\n" + message), actions: [noButton, okButton], ); // show the dialog showDialog( context: context, barrierDismissible: false, builder: (BuildContext context) { return alert; }, ); } else if (block == "true") { Widget okButton = TextButton( child: Text(action), onPressed: () async { if (action == "update") { _launchURL(Uri.parse("https://play.google.com/store/apps")); SystemChannels.platform.invokeMethod('SystemNavigator.pop'); Uri url = Uri.parse(Uri.encodeFull("https://play.google.com/store/apps")); //final Uri uri = Uri(scheme: "https", host: "play.google.com"); if(!await launchUrl(url, mode: LaunchMode.externalApplication)) { throw "Can not launch url"; } } else if (action == "close") { SystemChannels.platform.invokeMethod('SystemNavigator.pop'); } }, ); // set up the AlertDialog AlertDialog alert = AlertDialog( title: Text(appName), content: Text(title + "\n" + message), actions: [okButton], ); // show the dialog showDialog( context: context, barrierDismissible: false, 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(appName), content: Text("Server Response Error"), actions: [ noButton, okButton, ], ); // show the dialog showDialog( context: context, barrierDismissible: false, builder: (BuildContext context) { return alert; }, ); }