No Description
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.

login_screen.dart 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:firebase_messaging/firebase_messaging.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:flutter_udid/flutter_udid.dart';
  7. import 'package:fluttertoast/fluttertoast.dart';
  8. import 'package:google_fonts/google_fonts.dart';
  9. import 'package:employee_selfservice_mobile/Screens/ForgotPassword/forgotPassword_screen.dart';
  10. import 'package:employee_selfservice_mobile/Screens/Login/background.dart';
  11. import 'package:employee_selfservice_mobile/Screens/Login/post_result_model.dart';
  12. import 'package:employee_selfservice_mobile/Screens/Splash/splash_screen.dart';
  13. import 'package:progress_dialog_null_safe/progress_dialog_null_safe.dart';
  14. import 'package:shared_preferences/shared_preferences.dart';
  15. import '../Home/home_screen.dart';
  16. import 'inputWidget.dart';
  17. import 'dart:developer' as logDev;
  18. //import 'package:plain_notification_token/plain_notification_token.dart';
  19. class LoginView extends StatefulWidget {
  20. @override
  21. _LoginView createState() => _LoginView();
  22. }
  23. class _LoginView extends State<LoginView> {
  24. @override
  25. Widget build(BuildContext context) {
  26. return Scaffold(
  27. resizeToAvoidBottomInset: false,
  28. backgroundColor: Colors.white,
  29. body: Stack(
  30. children: <Widget>[
  31. Background(),
  32. LoginScreen(),
  33. ],
  34. ));
  35. }
  36. }
  37. class LoginScreen extends StatefulWidget {
  38. @override
  39. State<LoginScreen> createState() => _LoginScreenState();
  40. }
  41. class _LoginScreenState extends State<LoginScreen> {
  42. late LoginPostResult loginPostResult;
  43. String version = "1.0.0";
  44. String notif_token = "";
  45. late StreamSubscription onTokenRefreshSubscription;
  46. @override
  47. void initState() {
  48. super.initState();
  49. }
  50. @override
  51. Widget build(BuildContext context) {
  52. ProgressDialog loading = ProgressDialog(context);
  53. loading = ProgressDialog(context,
  54. type: ProgressDialogType.normal, isDismissible: false, showLogs: true);
  55. loading.style(
  56. message: 'Please Wait .....',
  57. borderRadius: 5,
  58. backgroundColor: Colors.white,
  59. progressWidget: CircularProgressIndicator(),
  60. elevation: 10.0,
  61. padding: EdgeInsets.all(10),
  62. insetAnimCurve: Curves.easeInOut,
  63. progress: 0.0,
  64. maxProgress: 100.0,
  65. progressTextStyle: TextStyle(
  66. color: Colors.black, fontSize: 10.0, fontWeight: FontWeight.w400),
  67. messageTextStyle: TextStyle(
  68. color: Colors.black, fontSize: 15.0, fontWeight: FontWeight.w600));
  69. return SingleChildScrollView(
  70. reverse: true,
  71. padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
  72. child: Column(
  73. children: <Widget>[
  74. Padding(
  75. padding:
  76. EdgeInsets.only(top: MediaQuery.of(context).size.height / 2.45),
  77. ),
  78. Column(
  79. children: <Widget>[
  80. Column(
  81. crossAxisAlignment: CrossAxisAlignment.center,
  82. children: <Widget>[
  83. Padding(
  84. padding: EdgeInsets.only(top: 0),
  85. child: Text(
  86. "Login",
  87. style: GoogleFonts.knewave(
  88. color: Colors.blueAccent, fontSize: 25),
  89. ),
  90. ),
  91. ],
  92. ),
  93. Column(
  94. crossAxisAlignment: CrossAxisAlignment.start,
  95. children: <Widget>[
  96. Padding(
  97. padding: EdgeInsets.only(left: 40, bottom: 5, top: 20),
  98. child: Text(
  99. "Email",
  100. style: TextStyle(fontSize: 16, color: Colors.black87),
  101. ),
  102. ),
  103. Stack(
  104. alignment: Alignment.bottomRight,
  105. children: <Widget>[
  106. InputWidgetEmail(20.0, 20.0),
  107. Padding(
  108. padding: EdgeInsets.only(right: 50),
  109. ),
  110. ],
  111. ),
  112. ],
  113. ),
  114. Column(
  115. crossAxisAlignment: CrossAxisAlignment.start,
  116. children: <Widget>[
  117. Padding(
  118. padding: EdgeInsets.only(left: 40, bottom: 5),
  119. child: Text(
  120. "Password",
  121. style: TextStyle(fontSize: 16, color: Colors.black87),
  122. ),
  123. ),
  124. Stack(
  125. alignment: Alignment.bottomRight,
  126. children: <Widget>[
  127. InputWidgetPassword(20.0, 20.0),
  128. Padding(
  129. padding: EdgeInsets.only(right: 15),
  130. child: Row(
  131. children: <Widget>[
  132. Expanded(
  133. child: Padding(
  134. padding: EdgeInsets.only(top: 0),
  135. )),
  136. InkWell(
  137. child: Container(
  138. padding: EdgeInsets.all(10),
  139. decoration: ShapeDecoration(
  140. shape: CircleBorder(),
  141. gradient: LinearGradient(
  142. colors: Gradients2,
  143. begin: Alignment.topLeft,
  144. end: Alignment.bottomRight),
  145. ),
  146. child: ImageIcon(
  147. AssetImage("assets/images/ic_forward.png"),
  148. size: 40,
  149. color: Colors.white,
  150. ),
  151. ),
  152. onTap: () async {
  153. String imei;
  154. try {
  155. imei = await FlutterUdid.udid;
  156. } on PlatformException {
  157. imei = 'Failed to get UDID.';
  158. }
  159. logDev.log(imei, name: "IMEI");
  160. if (!validateForm(context)) {
  161. return;
  162. } else {
  163. await loading.show();
  164. final notif_token = await FirebaseMessaging.instance.getToken();
  165. logDev.log(notif_token.toString(), name: "NOTIFICATION TOKEN");
  166. LoginPostResult.connectToAPI(
  167. emailController.text.toString(),
  168. passwordController.text.toString(),
  169. notif_token!,
  170. version,
  171. platform(),
  172. imei)
  173. .then((valueResult) async {
  174. Map<String, dynamic> object = json.decode(valueResult);
  175. if (object.containsKey("result").toString() == "true") {
  176. String status = object['result']['status'].toString();
  177. if (status == "success") {
  178. Fluttertoast.showToast(
  179. msg: "Login Success",
  180. toastLength: Toast.LENGTH_SHORT,
  181. gravity: ToastGravity.CENTER,
  182. timeInSecForIosWeb: 1,
  183. textColor: Colors.white,
  184. fontSize: 16.0);
  185. var prefs = await SharedPreferences.getInstance();
  186. await prefs.setString('version', version);
  187. await prefs.setString('device', object['result']['device'].toString());
  188. await prefs.setString('session', object['result']['hash'].toString());
  189. await prefs.setString('name', object['result']['name'].toString());
  190. await prefs.setString('notif_token', notif_token);
  191. emailController.clear();
  192. passwordController.clear();
  193. await loading.hide();
  194. Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => HomeView()));
  195. } else if (status == "failed") {
  196. String message = object['result']['message'].toString();
  197. Fluttertoast.showToast(
  198. msg: message,
  199. toastLength: Toast.LENGTH_SHORT,
  200. gravity: ToastGravity.CENTER,
  201. timeInSecForIosWeb: 1,
  202. textColor: Colors.white,
  203. fontSize: 16.0);
  204. await loading.hide();
  205. }
  206. } else {
  207. await loading.hide();
  208. alertDialogFailedResponse(context);
  209. }
  210. });
  211. }
  212. },
  213. )
  214. ],
  215. ),
  216. ),
  217. /*Padding(
  218. padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom)
  219. )*/
  220. ],
  221. ),
  222. ],
  223. ),
  224. Padding(
  225. padding: EdgeInsets.only(bottom: 10),
  226. ),
  227. InkWell(
  228. child:
  229. roundedRectButton("Forgot Password?", Gradients1, false),
  230. onTap: () {
  231. Navigator.push(
  232. context,
  233. MaterialPageRoute(
  234. builder: (context) => ForgotPasswordView()));
  235. })
  236. ],
  237. )
  238. ],
  239. ),
  240. );
  241. }
  242. }
  243. Widget roundedRectButton(
  244. String title, List<Color> gradient, bool isEndIconVisible) {
  245. return Builder(builder: (BuildContext mContext) {
  246. return Align(
  247. alignment: Alignment.centerLeft,
  248. child: Stack(
  249. children: <Widget>[
  250. Container(
  251. alignment: Alignment.centerRight,
  252. width: MediaQuery.of(mContext).size.width / 2.45,
  253. decoration: ShapeDecoration(
  254. shape: RoundedRectangleBorder(
  255. borderRadius: BorderRadius.only(
  256. topRight: Radius.circular(20.0),
  257. bottomRight: Radius.circular(20.0))),
  258. gradient: LinearGradient(
  259. colors: gradient,
  260. begin: Alignment.topLeft,
  261. end: Alignment.bottomRight),
  262. ),
  263. child: Text(title,
  264. style: TextStyle(
  265. decoration: TextDecoration.underline,
  266. color: Colors.white,
  267. fontSize: 15,
  268. fontWeight: FontWeight.w500)),
  269. padding: EdgeInsets.all(10),
  270. ),
  271. Visibility(
  272. visible: isEndIconVisible,
  273. child: Padding(
  274. padding: EdgeInsets.only(right: 10),
  275. child: ImageIcon(
  276. AssetImage("assets/images/ic_forward.png"),
  277. size: 30,
  278. color: Colors.white,
  279. )),
  280. ),
  281. ],
  282. ),
  283. );
  284. });
  285. }
  286. const List<Color> Gradients1 = [
  287. /*Color(0xFFFFFFFF),
  288. Color(0xFFFFFFFF),*/
  289. Color(0xFF03A0FE),
  290. Colors.pink,
  291. ];
  292. const List<Color> Gradients2 = [
  293. Color(0xFFFF9945),
  294. Color(0xFFFc6076),
  295. ];
  296. bool validateForm(BuildContext context) {
  297. bool result = true;
  298. if (emailController.text.toString().isEmpty) {
  299. /*final snackBar = SnackBar(content: Text("Email Required"));
  300. ScaffoldMessenger.of(context).showSnackBar(snackBar);*/
  301. Fluttertoast.showToast(
  302. msg: "Email Required",
  303. toastLength: Toast.LENGTH_SHORT,
  304. gravity: ToastGravity.CENTER,
  305. timeInSecForIosWeb: 1,
  306. textColor: Colors.white,
  307. fontSize: 16.0);
  308. result = false;
  309. } else if (!emailController.text.toString().contains("@")) {
  310. Fluttertoast.showToast(
  311. msg: "Incorrect email format",
  312. toastLength: Toast.LENGTH_SHORT,
  313. gravity: ToastGravity.CENTER,
  314. timeInSecForIosWeb: 1,
  315. textColor: Colors.white,
  316. fontSize: 16.0);
  317. result = false;
  318. } else if (passwordController.text.toString().isEmpty) {
  319. Fluttertoast.showToast(
  320. msg: "Password Required",
  321. toastLength: Toast.LENGTH_SHORT,
  322. gravity: ToastGravity.CENTER,
  323. timeInSecForIosWeb: 1,
  324. textColor: Colors.white,
  325. fontSize: 16.0);
  326. result = false;
  327. }
  328. return result;
  329. }
  330. alertDialogFailedResponse(BuildContext context){
  331. Widget okButton = TextButton(
  332. child: Text("Refresh"),
  333. onPressed: () {
  334. Navigator.of(context, rootNavigator: true).pop();
  335. Navigator.pushReplacement(context, MaterialPageRoute(
  336. builder: (context) => SplashScreen()));
  337. },
  338. );
  339. Widget noButton = TextButton(
  340. child: Text("Back"),
  341. onPressed: () {
  342. Navigator.of(context, rootNavigator: true).pop();
  343. Navigator.pop(context);
  344. },
  345. );
  346. // set up the AlertDialog
  347. AlertDialog alert = AlertDialog(
  348. title: Text("Employee Self Service"),
  349. content: Text("Server Response Error"),
  350. actions: [
  351. noButton,
  352. okButton,
  353. ],
  354. );
  355. // show the dialog
  356. showDialog(
  357. context: context,
  358. barrierDismissible: false,
  359. builder: (BuildContext context) {
  360. return alert;
  361. },
  362. );
  363. }