Brak opisu
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 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:fluttertoast/fluttertoast.dart';
  6. import 'package:google_fonts/google_fonts.dart';
  7. import 'package:hris_selfservice_mobile/Screens/Login/login_screen.dart';
  8. import 'package:hris_selfservice_mobile/Screens/Splash/loginstatus_post.dart';
  9. import 'package:lottie/lottie.dart';
  10. import 'package:shared_preferences/shared_preferences.dart';
  11. import '../Home/home_screen.dart';
  12. import '../Splash/background.dart';
  13. import 'package:url_launcher/url_launcher.dart';
  14. import 'dart:developer' as logDev;
  15. class Splash extends StatefulWidget {
  16. const Splash({Key? key}) : super(key: key);
  17. @override
  18. _SplashState createState() => _SplashState();
  19. }
  20. class _SplashState extends State<Splash> {
  21. @override
  22. void initState() {
  23. super.initState();
  24. Timer(Duration(seconds: 10), () => loginStatus(context));
  25. }
  26. @override
  27. Widget build(BuildContext context) {
  28. return Scaffold(
  29. resizeToAvoidBottomInset: false,
  30. body: Stack(
  31. children: <Widget>[
  32. Background(),
  33. SplashScreen(),
  34. ],
  35. ));
  36. }
  37. }
  38. class SplashScreen extends StatelessWidget {
  39. const SplashScreen({Key? key}) : super(key: key);
  40. @override
  41. Widget build(BuildContext context) {
  42. return Scaffold(
  43. body: Container(
  44. width: double.infinity,
  45. height: double.infinity,
  46. decoration: BoxDecoration(
  47. gradient: LinearGradient(
  48. begin: Alignment.topRight,
  49. end: Alignment.bottomRight,
  50. colors: [
  51. Color(0xFFD21404),
  52. Color(0xFFFD7267),
  53. ])),
  54. child: Center(
  55. child: Column(
  56. mainAxisAlignment: MainAxisAlignment.center,
  57. crossAxisAlignment: CrossAxisAlignment.center,
  58. children: [
  59. Container(
  60. /*decoration: BoxDecoration(
  61. shape: BoxShape.circle,
  62. color: Colors.white
  63. ),*/
  64. child: SizedBox(
  65. width: 250,
  66. height: 250,
  67. child: LottieBuilder.asset('assets/animation/animation_4.json',
  68. repeat: true),
  69. ),
  70. ),
  71. Text('Employee Self Service',
  72. style: GoogleFonts.knewave(fontSize: 22, color: Colors.white))
  73. ],
  74. ),
  75. ),
  76. ));
  77. }
  78. }
  79. Future<void> loginStatus(BuildContext context) async {
  80. final SharedPreferences prefs = await SharedPreferences.getInstance();
  81. String? version = prefs.getString('version');
  82. String? device = prefs.getString('device');
  83. String? session = prefs.getString('session');
  84. String? notif_token = prefs.getString('notif_token');
  85. if (session == null) {
  86. session = "";
  87. version = "";
  88. device = "";
  89. notif_token = "";
  90. }
  91. logDev.log(session, name: "SESSIONNYA");
  92. LoginStatus_Post.connectToAPI(version!, device!, session, notif_token!)
  93. .then((valueResult) async {
  94. Map<String, dynamic> object = json.decode(valueResult);
  95. logDev.log(valueResult, name: "LOGIN STATUS!");
  96. if (object.containsKey("result").toString() == "true") {
  97. String status = object['result']['status'].toString();
  98. if (status == "success") {
  99. Fluttertoast.showToast(
  100. msg: "You are already logged in",
  101. toastLength: Toast.LENGTH_SHORT,
  102. gravity: ToastGravity.CENTER,
  103. timeInSecForIosWeb: 1,
  104. textColor: Colors.white,
  105. fontSize: 16.0);
  106. Navigator.pushReplacement(
  107. context, MaterialPageRoute(builder: (context) => HomeView()));
  108. } else if (status == "failed") {
  109. String message = object['result']['message'].toString();
  110. if (message == "Not Logged In") {
  111. Fluttertoast.showToast(
  112. msg: message,
  113. toastLength: Toast.LENGTH_SHORT,
  114. gravity: ToastGravity.CENTER,
  115. timeInSecForIosWeb: 1,
  116. textColor: Colors.white,
  117. fontSize: 16.0);
  118. Navigator.pushReplacement(
  119. context, MaterialPageRoute(builder: (context) => LoginView()));
  120. } else {
  121. String title = object['result']['title'].toString();
  122. String action = object['result']['action'].toString();
  123. String block = object['result']['block'].toString();
  124. if (block == "false") {
  125. Widget okButton = TextButton(
  126. child: Text(action),
  127. onPressed: () {
  128. _launchURL(Uri.parse("https://play.google.com/store/apps"));
  129. SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  130. },
  131. );
  132. Widget noButton = TextButton(
  133. child: Text("Later"),
  134. onPressed: () {
  135. if (session == ""){
  136. Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => LoginView()));
  137. } else {
  138. Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => HomeView()));
  139. }
  140. },
  141. );
  142. // set up the AlertDialog
  143. AlertDialog alert = AlertDialog(
  144. title: Text("Employee Self Service"),
  145. content: Text(title + "\n" + message),
  146. actions: [noButton, okButton],
  147. );
  148. // show the dialog
  149. showDialog(
  150. context: context,
  151. builder: (BuildContext context) {
  152. return alert;
  153. },
  154. );
  155. } else if (block == "true") {
  156. Widget okButton = TextButton(
  157. child: Text(action),
  158. onPressed: () {
  159. if (action == "update") {
  160. _launchURL(Uri.parse("https://play.google.com/store/apps"));
  161. SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  162. } else if (action == "close") {
  163. SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  164. }
  165. },
  166. );
  167. // set up the AlertDialog
  168. AlertDialog alert = AlertDialog(
  169. title: Text("Employee Self Service"),
  170. content: Text(title + "\n" + message),
  171. actions: [okButton],
  172. );
  173. // show the dialog
  174. showDialog(
  175. context: context,
  176. builder: (BuildContext context) {
  177. return alert;
  178. },
  179. );
  180. }
  181. }
  182. }
  183. } else {
  184. alertDialogFailedResponse(context);
  185. /*Fluttertoast.showToast(
  186. msg: "Server Response Error",
  187. toastLength: Toast.LENGTH_SHORT,
  188. gravity: ToastGravity.CENTER,
  189. timeInSecForIosWeb: 1,
  190. textColor: Colors.white,
  191. fontSize: 16.0);*/
  192. }
  193. });
  194. }
  195. _launchURL(Uri url) async {
  196. if (await canLaunchUrl(url)) {
  197. await launchUrl(url);
  198. } else {
  199. throw 'Could not launch $url';
  200. }
  201. }
  202. alertDialogFailedResponse(BuildContext context){
  203. Widget okButton = TextButton(
  204. child: Text("Refresh"),
  205. onPressed: () {
  206. Navigator.of(context, rootNavigator: true).pop();
  207. Navigator.pushReplacement(context, MaterialPageRoute(
  208. builder: (context) => SplashScreen()));
  209. },
  210. );
  211. Widget noButton = TextButton(
  212. child: Text("Back"),
  213. onPressed: () {
  214. Navigator.of(context, rootNavigator: true).pop();
  215. Navigator.pop(context);
  216. },
  217. );
  218. // set up the AlertDialog
  219. AlertDialog alert = AlertDialog(
  220. title: Text("Employee Self Service"),
  221. content: Text("Server Response Error"),
  222. actions: [
  223. noButton,
  224. okButton,
  225. ],
  226. );
  227. // show the dialog
  228. showDialog(
  229. context: context,
  230. builder: (BuildContext context) {
  231. return alert;
  232. },
  233. );
  234. }