Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

splash_screen.dart 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'package:connectivity_plus/connectivity_plus.dart';
  5. import 'package:flutter/cupertino.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:flutter/services.dart';
  8. import 'package:fluttertoast/fluttertoast.dart';
  9. import 'package:google_fonts/google_fonts.dart';
  10. import 'package:employee_selfservice_mobile/Screens/Login/login_screen.dart';
  11. import 'package:employee_selfservice_mobile/Screens/Splash/loginstatus_post.dart';
  12. import 'package:employee_selfservice_mobile/constants.dart';
  13. import 'package:internet_connection_checker/internet_connection_checker.dart';
  14. import 'package:lottie/lottie.dart';
  15. import 'package:shared_preferences/shared_preferences.dart';
  16. import '../Home/home_screen.dart';
  17. import '../Splash/background.dart';
  18. import 'package:url_launcher/url_launcher.dart';
  19. import 'dart:developer' as logDev;
  20. class Splash extends StatefulWidget {
  21. const Splash({Key? key}) : super(key: key);
  22. @override
  23. _SplashState createState() => _SplashState();
  24. }
  25. class _SplashState extends State<Splash> {
  26. late StreamSubscription subscription;
  27. bool isDeviceConnected = false;
  28. bool isAlertSet = false;
  29. @override
  30. void initState() {
  31. //getConnectivity();
  32. //Timer(Duration(seconds: 10), () => loginStatus(context));
  33. Timer(Duration(seconds: 10), () => Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => LoginView())));
  34. super.initState();
  35. }
  36. /*getConnectivity() =>
  37. subscription = Connectivity().onConnectivityChanged.listen(
  38. (ConnectivityResult result) async {
  39. isDeviceConnected = await InternetConnectionChecker().hasConnection;
  40. if (!isDeviceConnected && isAlertSet == false) {
  41. showConnectivityDialogBox();
  42. setState(() => isAlertSet = true);
  43. }
  44. },
  45. );*/
  46. @override
  47. Widget build(BuildContext context) {
  48. return Scaffold(
  49. resizeToAvoidBottomInset: false,
  50. body: Stack(
  51. children: <Widget>[
  52. Background(),
  53. SplashScreen(),
  54. ],
  55. ));
  56. }
  57. /*showConnectivityDialogBox() =>
  58. showCupertinoDialog<String>(
  59. context: context,
  60. builder: (BuildContext context) =>
  61. CupertinoAlertDialog(
  62. title: const Text('No Connection'),
  63. content: const Text('Please check your internet connectivity'),
  64. actions: <Widget>[
  65. Column(
  66. children: [
  67. SizedBox(
  68. width: 250,
  69. height: 250,
  70. child: LottieBuilder.asset(
  71. //'assets/animation/animation_no_internet.json',
  72. 'assets/animation/animation_no_internet_3.json',
  73. repeat: true),
  74. ),
  75. ],
  76. ),
  77. TextButton(
  78. onPressed: () async {
  79. Navigator.pop(context, 'Cancel');
  80. setState(() => isAlertSet = false);
  81. isDeviceConnected =
  82. await InternetConnectionChecker().hasConnection;
  83. if (!isDeviceConnected && isAlertSet == false) {
  84. showConnectivityDialogBox();
  85. setState(() => isAlertSet = true);
  86. }
  87. },
  88. child: const Text('Retry'),
  89. ),
  90. ],
  91. ),
  92. );*/
  93. }
  94. class SplashScreen extends StatelessWidget {
  95. const SplashScreen({Key? key}) : super(key: key);
  96. @override
  97. Widget build(BuildContext context) {
  98. var size = MediaQuery.of(context).size;
  99. return Scaffold(
  100. body: Container(
  101. width: double.infinity,
  102. height: double.infinity,
  103. decoration: BoxDecoration(
  104. gradient: LinearGradient(
  105. begin: Alignment.topRight,
  106. end: Alignment.bottomRight,
  107. colors: [
  108. Color(0xFF4858A7),
  109. Color(0xFF4858A7),
  110. Color(0xFF6474C6),
  111. //Color(0xFF8C99DD),
  112. //Color(0xFFFFFFFF)
  113. ])),
  114. child: Center(
  115. child: Column(
  116. mainAxisAlignment: MainAxisAlignment.center,
  117. crossAxisAlignment: CrossAxisAlignment.center,
  118. children: [
  119. Container(
  120. /*decoration: BoxDecoration(
  121. shape: BoxShape.circle,
  122. color: Colors.white
  123. ),*/
  124. child: SizedBox(
  125. width: size.width * 0.55,
  126. //height: 250,
  127. child: Image.asset('assets/icons/menu/ic_canvasing_1.png')
  128. /*LottieBuilder.asset('assets/animation/animation_4.json',
  129. repeat: true, ),*/
  130. ),
  131. ),
  132. /* Text(appName,
  133. style: GoogleFonts.carterOne(fontSize: 25, color: Colors.white))*/
  134. ],
  135. ),
  136. ),
  137. ));
  138. }
  139. }
  140. Future<void> loginStatus(BuildContext context) async {
  141. final SharedPreferences prefs = await SharedPreferences.getInstance();
  142. String? version = prefs.getString('version');
  143. String? device = prefs.getString('device');
  144. String? session = prefs.getString('session');
  145. String? notif_token = prefs.getString('notif_token');
  146. if (session == null) {
  147. version = "1.0.0";
  148. session = "";
  149. notif_token = "";
  150. if (Platform.isAndroid){
  151. device = "android";
  152. } else if (Platform.isIOS){
  153. device = "ios";
  154. }
  155. }
  156. //logDev.log(session, name: "SESSIONNYA");
  157. LoginStatus_Post.connectToAPI(version!, device!, session, notif_token!).then((valueResult) async {
  158. Map<String, dynamic> object = json.decode(valueResult);
  159. if (object.containsKey("result").toString() == "true") {
  160. String status = object['result']['status'].toString();
  161. if (status == "success") {
  162. Fluttertoast.showToast(
  163. msg: "You are already logged in",
  164. toastLength: Toast.LENGTH_SHORT,
  165. gravity: ToastGravity.CENTER,
  166. timeInSecForIosWeb: 1,
  167. textColor: Colors.white,
  168. fontSize: 16.0);
  169. Navigator.pushReplacement(
  170. context, MaterialPageRoute(builder: (context) => HomeView()));
  171. } else if (status == "failed") {
  172. String message = object['result']['message'].toString();
  173. if (message == "Not Logged In") {
  174. Fluttertoast.showToast(
  175. msg: message,
  176. toastLength: Toast.LENGTH_SHORT,
  177. gravity: ToastGravity.CENTER,
  178. timeInSecForIosWeb: 1,
  179. textColor: Colors.white,
  180. fontSize: 16.0);
  181. Navigator.pushReplacement(
  182. context, MaterialPageRoute(builder: (context) => LoginView()));
  183. } else {
  184. String title = object['result']['title'].toString();
  185. String action = object['result']['action'].toString();
  186. String block = object['result']['block'].toString();
  187. if (block == "false") {
  188. Widget okButton = TextButton(
  189. child: Text(action),
  190. onPressed: () async {
  191. Uri url = Uri.parse(Uri.encodeFull("https://play.google.com/store/apps"));
  192. //final Uri uri = Uri(scheme: "https", host: "play.google.com");
  193. if(!await launchUrl(url, mode: LaunchMode.externalApplication)) {
  194. throw "Can not launch url";
  195. }
  196. },
  197. );
  198. Widget noButton = TextButton(
  199. child: Text("Later"),
  200. onPressed: () {
  201. if (session == ""){
  202. Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => LoginView()));
  203. } else {
  204. Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => HomeView()));
  205. }
  206. },
  207. );
  208. // set up the AlertDialog
  209. AlertDialog alert = AlertDialog(
  210. title: Text(appName),
  211. content: Text(title + "\n" + message),
  212. actions: [noButton, okButton],
  213. );
  214. // show the dialog
  215. showDialog(
  216. context: context,
  217. barrierDismissible: false,
  218. builder: (BuildContext context) {
  219. return alert;
  220. },
  221. );
  222. } else if (block == "true") {
  223. Widget okButton = TextButton(
  224. child: Text(action),
  225. onPressed: () async {
  226. if (action == "update") {
  227. _launchURL(Uri.parse("https://play.google.com/store/apps"));
  228. SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  229. Uri url = Uri.parse(Uri.encodeFull("https://play.google.com/store/apps"));
  230. //final Uri uri = Uri(scheme: "https", host: "play.google.com");
  231. if(!await launchUrl(url, mode: LaunchMode.externalApplication)) {
  232. throw "Can not launch url";
  233. }
  234. } else if (action == "close") {
  235. SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  236. }
  237. },
  238. );
  239. // set up the AlertDialog
  240. AlertDialog alert = AlertDialog(
  241. title: Text(appName),
  242. content: Text(title + "\n" + message),
  243. actions: [okButton],
  244. );
  245. // show the dialog
  246. showDialog(
  247. context: context,
  248. barrierDismissible: false,
  249. builder: (BuildContext context) {
  250. return alert;
  251. },
  252. );
  253. }
  254. }
  255. }
  256. } else {
  257. alertDialogFailedResponse(context);
  258. Fluttertoast.showToast(
  259. msg: "Server Response Error",
  260. toastLength: Toast.LENGTH_SHORT,
  261. gravity: ToastGravity.CENTER,
  262. timeInSecForIosWeb: 1,
  263. textColor: Colors.white,
  264. fontSize: 16.0);
  265. }
  266. });
  267. }
  268. _launchURL(Uri url) async {
  269. if (await canLaunchUrl(url)) {
  270. await launchUrl(url);
  271. } else {
  272. throw 'Could not launch $url';
  273. }
  274. }
  275. alertDialogFailedResponse(BuildContext context){
  276. Widget okButton = TextButton(
  277. child: Text("Refresh"),
  278. onPressed: () {
  279. Navigator.of(context, rootNavigator: true).pop();
  280. Navigator.pushReplacement(context, MaterialPageRoute(
  281. builder: (context) => SplashScreen()));
  282. },
  283. );
  284. Widget noButton = TextButton(
  285. child: Text("Back"),
  286. onPressed: () {
  287. Navigator.of(context, rootNavigator: true).pop();
  288. Navigator.pop(context);
  289. },
  290. );
  291. // set up the AlertDialog
  292. AlertDialog alert = AlertDialog(
  293. title: Text(appName),
  294. content: Text("Server Response Error"),
  295. actions: [
  296. noButton,
  297. okButton,
  298. ],
  299. );
  300. // show the dialog
  301. showDialog(
  302. context: context,
  303. barrierDismissible: false,
  304. builder: (BuildContext context) {
  305. return alert;
  306. },
  307. );
  308. }