123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- import 'dart:async';
- import 'dart:convert';
- import 'dart:io';
- import 'package:connectivity_plus/connectivity_plus.dart';
- 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:employee_selfservice_mobile/Screens/Login/login_screen.dart';
- import 'package:employee_selfservice_mobile/Screens/Splash/loginstatus_post.dart';
- import 'package:employee_selfservice_mobile/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<Splash> {
- 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: <Widget>[
- Background(),
- SplashScreen(),
- ],
- ));
- }
-
- /*showConnectivityDialogBox() =>
- showCupertinoDialog<String>(
- context: context,
- builder: (BuildContext context) =>
- CupertinoAlertDialog(
- title: const Text('No Connection'),
- content: const Text('Please check your internet connectivity'),
- actions: <Widget>[
- 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/menu/ic_canvasing_1.png')
- /*LottieBuilder.asset('assets/animation/animation_4.json',
- repeat: true, ),*/
- ),
- ),
- /* Text(appName,
- style: GoogleFonts.carterOne(fontSize: 25, color: Colors.white))*/
- ],
- ),
- ),
- ));
- }
- }
-
- Future<void> 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<String, dynamic> 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;
- },
- );
- }
-
|