Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.91 KB | None | 0 0
  1. import 'dart:convert';
  2.  
  3. import 'package:SAKTI.Link/Login/ResetAkun/reset-akun.dart';
  4. import 'package:SAKTI.Link/home/menu-tab.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter/services.dart';
  7. import 'package:flutter_spinkit/flutter_spinkit.dart';
  8. import 'package:global_configuration/global_configuration.dart';
  9. import 'package:http/http.dart' as http;
  10. import 'package:shared_preferences/shared_preferences.dart';
  11.  
  12. class Loginpage extends StatefulWidget {
  13. static String tag = 'login-page';
  14.  
  15. @override
  16. _LoginPageState createState() => _LoginPageState();
  17. }
  18.  
  19. class SlideRightRoute extends PageRouteBuilder {
  20. final Widget page;
  21.  
  22. SlideRightRoute({this.page})
  23. : super(
  24. pageBuilder: (
  25. BuildContext context,
  26. Animation<double> animation,
  27. Animation<double> secondaryAnimation,
  28. ) =>
  29. page,
  30. transitionsBuilder: (
  31. BuildContext context,
  32. Animation<double> animation,
  33. Animation<double> secondaryAnimation,
  34. Widget child,
  35. ) =>
  36. SlideTransition(
  37. position: Tween<Offset>(
  38. begin: const Offset(1, 0),
  39. end: Offset.zero,
  40. ).animate(animation),
  41. child: child,
  42. ),
  43. );
  44. }
  45.  
  46. class SlideLeftRoute extends PageRouteBuilder {
  47. final Widget page;
  48.  
  49. SlideLeftRoute({this.page})
  50. : super(
  51. pageBuilder: (
  52. BuildContext context,
  53. Animation<double> animation,
  54. Animation<double> secondaryAnimation,
  55. ) =>
  56. page,
  57. transitionsBuilder: (
  58. BuildContext context,
  59. Animation<double> animation,
  60. Animation<double> secondaryAnimation,
  61. Widget child,
  62. ) =>
  63. SlideTransition(
  64. position: Tween<Offset>(
  65. begin: const Offset(-1, 0),
  66. end: Offset.zero,
  67. ).animate(animation),
  68. child: child,
  69. ),
  70. );
  71. }
  72.  
  73. class _LoginPageState extends State<Loginpage> {
  74. TextEditingController hpController = TextEditingController();
  75. TextEditingController sandiController = TextEditingController();
  76. TextEditingController usernameController = TextEditingController();
  77. TextEditingController passwordController = TextEditingController();
  78. String _tokenfcm = '';
  79. String version = '';
  80. String slImgSplash = '';
  81. String slKoperasi = '';
  82. String slUrl = '';
  83. String slUrlSplash = '';
  84. String slHp = '';
  85. String slFingerprint = '';
  86. String slSimid = '';
  87. String slToken = '';
  88. String slTokenKey = '';
  89. String nama = '';
  90. String noAnggota = '';
  91. String generateQR = '';
  92.  
  93. String tipeReset = '';
  94. String img = "";
  95. String koperasi = '';
  96.  
  97. bool _obscureText = false;
  98.  
  99. void showLoading() {
  100. showDialog(
  101. context: context,
  102. barrierDismissible: false,
  103. builder: (BuildContext context) {
  104. return AlertDialog(
  105. elevation: 0,
  106. backgroundColor: Colors.transparent,
  107. content: SpinKitThreeBounce(color: Colors.white, size: 35.0));
  108. });
  109. }
  110.  
  111. getLocalStorage() async {
  112. final prefs = await SharedPreferences.getInstance();
  113. setState(() {
  114. koperasi = prefs.getString('koperasi') != null
  115. ? prefs.getString('koperasi')
  116. : "";
  117. img = prefs.getString('img');
  118. });
  119. }
  120.  
  121.  
  122.  
  123. void initState() {
  124. super.initState();
  125. getLocalStorage();
  126. }
  127.  
  128. void login() async {
  129. showLoading();
  130. final prefs = await SharedPreferences.getInstance();
  131.  
  132. var body = {
  133. 'hp': usernameController.text,
  134. 'version': version,
  135. 'tokenfcm': _tokenfcm,
  136. };
  137. var url =
  138. GlobalConfiguration().getString("sl_urlSKK") + "api/saktilink/getURL";
  139.  
  140. http
  141. .post(
  142. url,
  143. body: body,
  144. )
  145. .then((response) {
  146. Navigator.of(context).pop();
  147. var res = json.decode(response.body);
  148.  
  149. if (res['status']['response'] == 200) {
  150. setState(() {
  151. slImgSplash = res['data']['img_splash'];
  152. slKoperasi = res['data']['koperasi'];
  153. slUrl = res['data']['url'];
  154. slUrlSplash = res['data']['url_splash'];
  155.  
  156. prefs.setString('img_splash', slImgSplash);
  157. prefs.setString('koperasi', slKoperasi);
  158. prefs.setString('url', slUrl);
  159. prefs.setString('url_splash', slUrlSplash);
  160. prefs.setString('version', res['version']);
  161. prefs.setString('sl_username', usernameController.text);
  162.  
  163. var body = {
  164. 'username': usernameController.text,
  165. 'password': passwordController.text,
  166. 'hp': slHp,
  167. 'fingerprint': slFingerprint,
  168. 'simid': slSimid,
  169. };
  170. var url = slUrl + "apiklien/loginClient";
  171.  
  172. http
  173. .post(
  174. url,
  175. body: body,
  176. )
  177. .then((response) {
  178. var res = json.decode(response.body);
  179.  
  180. if (res['st'] == 1) {
  181. setState(() {
  182. slSimid = "";
  183. noAnggota = res['no_anggota'];
  184. slToken = res['token_id'];
  185. slTokenKey = res['token_key'];
  186. generateQR = res['qr_code'];
  187. prefs.setString('sl_simid', slSimid);
  188. prefs.setString('sl_hpLogin', slHp);
  189. prefs.setString('sl_tokenid', slToken);
  190. prefs.setString('sl_tokenkey', slTokenKey);
  191. prefs.setString('nama', res['nama']);
  192. prefs.setString('no_anggota', noAnggota);
  193. prefs.setString(
  194. 'img', slUrl + "public/images/anggota/" + res['img']);
  195. prefs.setString('qr_code', generateQR);
  196. prefs.setBool('cek_login', true);
  197.  
  198. Navigator.pushAndRemoveUntil(
  199. context,
  200. SlideRightRoute(page: MenuTab()),
  201. (Route<dynamic> route) => false);
  202. });
  203. } else if (res['st'] == 404) {
  204. showDialog(
  205. context: context,
  206. builder: (context) {
  207. return AlertDialog(
  208. title: Text("Galat!"),
  209. content: Text(
  210. "Akun SAKTI.Link Anda masih aktif di perangkat lain. Silahkan reset akun untuk dapat masuk kembali di perangkat ."),
  211. actions: <Widget>[
  212. Row(
  213. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  214. children: <Widget>[
  215. FlatButton(
  216. child: Text("LAIN KALI"),
  217. onPressed: () {
  218. Navigator.of(context).pop();
  219. },
  220. ),
  221. FlatButton(
  222. child: Text("RESET AKUN"),
  223. onPressed: () {
  224. Navigator.of(context).pop();
  225. showDialog(
  226. context: context,
  227. builder: (context) => AlertDialog(
  228. title: Text("Pilih Metode Reset"),
  229. content: Text(
  230. "Anda dapat mereset akun via nomor HP atau pun Email. Pastikan Nomor HP/Email yang akan digunakan dalam keadaan aktif. "),
  231. actions: <Widget>[
  232. Row(
  233. mainAxisAlignment:
  234. MainAxisAlignment.spaceBetween,
  235. children: <Widget>[
  236. FlatButton(
  237. child: Text("VIA SMS"),
  238. onPressed: () {
  239. tipeReset = 'sms';
  240. Navigator.push(
  241. context,
  242. SlideRightRoute(
  243. page: ResetAkunPage(
  244. tipeReset)));
  245. },
  246. ),
  247. FlatButton(
  248. child: Text("VIA EMAIL"),
  249. onPressed: () {
  250. tipeReset = 'email';
  251. Navigator.push(
  252. context,
  253. SlideRightRoute(
  254. page: ResetAkunPage(
  255. tipeReset)));
  256. },
  257. ),
  258. ],
  259. )
  260. ],
  261. ));
  262. },
  263. ),
  264. ],
  265. )
  266. ],
  267. );
  268. },
  269. );
  270. } else if (res['st'] == 0) {
  271. showDialog(
  272. context: context,
  273. builder: (context) {
  274. return AlertDialog(
  275. title: Text("Perhatian"),
  276. content: Text("Nomer Ponsel atau sandi anda salah"),
  277. actions: <Widget>[
  278. FlatButton(
  279. child: Text("Ok"),
  280. onPressed: () {
  281. Navigator.of(context).pop();
  282. },
  283. )
  284. ],
  285. );
  286. },
  287. );
  288. } else if (res['st'] == 2) {
  289. showDialog(
  290. context: context,
  291. builder: (context) {
  292. return AlertDialog(
  293. title: Text("Perhatian"),
  294. content: Text(
  295. "Silahkan masuk kembali menggunakan kata sandi anda"),
  296. actions: <Widget>[
  297. FlatButton(
  298. child: Text("Ok"),
  299. onPressed: () {
  300. Navigator.of(context).pop();
  301. },
  302. )
  303. ],
  304. );
  305. },
  306. );
  307. } else {
  308. showDialog(
  309. context: context,
  310. builder: (context) {
  311. return AlertDialog(
  312. title: Text("Perhatian"),
  313. content: Text(
  314. "Jaringan anda sedang bermasalah, mohon coba lagi"),
  315. actions: <Widget>[
  316. FlatButton(
  317. child: Text("Ok"),
  318. onPressed: () {
  319. Navigator.of(context).pop();
  320. },
  321. )
  322. ],
  323. );
  324. },
  325. );
  326. }
  327. });
  328. });
  329. } else {
  330. showDialog(
  331. context: context,
  332. builder: (context) {
  333. return AlertDialog(
  334. content: Text(res['status']['message']),
  335. actions: <Widget>[
  336. FlatButton(
  337. child: Text("Ok"),
  338. onPressed: () {
  339. Navigator.of(context).pop();
  340. },
  341. )
  342. ],
  343. );
  344. },
  345. );
  346. }
  347. });
  348. }
  349.  
  350. @override
  351. Widget build(BuildContext context) {
  352. final formnohp = Padding(
  353. padding: EdgeInsets.only(top: 25, left: 40, right: 40, bottom: 18),
  354. child: TextFormField(
  355. keyboardType: TextInputType.number,
  356. cursorColor: Colors.black,
  357. controller: usernameController,
  358. decoration: InputDecoration(
  359. fillColor: Colors.transparent,
  360. prefixIcon: Icon(
  361. Icons.smartphone,
  362. color: Color(0xFFFFF03A51),
  363. ),
  364. filled: true,
  365. hintText: 'Nomor ponsel',
  366. enabledBorder: UnderlineInputBorder(
  367. borderSide: BorderSide(
  368. color: Color(0xFFFFF03A51),
  369. ),
  370. ),
  371. focusedBorder: UnderlineInputBorder(
  372. borderSide: BorderSide(
  373. color: Color(0xFFFFF03A51),
  374. )),
  375. contentPadding: EdgeInsets.fromLTRB(20.0, 17.0, 20.0, 10.0)),
  376. ),
  377. );
  378.  
  379. final formsandi = Padding(
  380. padding: EdgeInsets.only(top: 1, left: 40, right: 40),
  381. child: TextFormField(
  382. keyboardType: TextInputType.text,
  383. cursorColor: Colors.black,
  384. controller: passwordController,
  385. obscureText: !_obscureText,
  386. decoration: InputDecoration(
  387. fillColor: Colors.transparent,
  388. prefixIcon: Icon(
  389. Icons.lock,
  390. color: Color(0xFFFFF03A51),
  391. ),
  392. filled: true,
  393. hintText: 'Sandi',
  394. enabledBorder: UnderlineInputBorder(
  395. borderSide: BorderSide(
  396. color: Color(0xFFFFF03A51),
  397. ),
  398. ),
  399. focusedBorder: UnderlineInputBorder(
  400. borderSide: BorderSide(
  401. color: Color(0xFFFFF03A51),
  402. )),
  403. suffixIcon: GestureDetector(
  404. onTap: () {
  405. setState(() {
  406. _obscureText = !_obscureText;
  407. });
  408. },
  409. child: Icon(
  410. _obscureText ? Icons.visibility : Icons.visibility_off,
  411. color: Color(0xFFFFF03A51),
  412. semanticLabel: _obscureText ? 'show password' : 'hide password',
  413. ),
  414. ),
  415. contentPadding: EdgeInsets.fromLTRB(20.0, 17.0, 20.0, 10.0)),
  416. ),
  417. );
  418.  
  419. final image = Padding(
  420. padding: EdgeInsets.only(top: 20, bottom: 25),
  421. child: Image(
  422. image: AssetImage("lib/assets/main-logo.png"),
  423. width: 150,
  424. height: 150,
  425. ));
  426.  
  427.  
  428. // final simpan = Container(
  429. // margin: EdgeInsets.all(10),
  430. // child: Material(
  431. // child: MaterialButton(
  432. // shape: RoundedRectangleBorder(
  433. // borderRadius: BorderRadius.all(Radius.circular(5.0)),
  434. // ),
  435. // minWidth: 344.0,
  436. // height: 47.0,
  437. // materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
  438. // color: Color(0xFFFFF03A51),
  439. // child: Text('Simpan',
  440. // style: TextStyle(color: Colors.white, fontSize: 18.0)),
  441. // onPressed: () {
  442. // login();
  443. // },
  444. // ),
  445. // ),
  446. // );
  447.  
  448.  
  449.  
  450.  
  451. return Scaffold(
  452. backgroundColor: Colors.white,
  453. resizeToAvoidBottomPadding: false,
  454. body: Stack(
  455. children: <Widget>[
  456. ListView(
  457. children: <Widget>[
  458. image,
  459. Center(
  460. child: Text(
  461. koperasi,
  462. style: TextStyle(
  463. color: Color(0xFFFFF03A51),
  464. ),
  465. ),
  466. ),
  467. formnohp,
  468. formsandi,
  469. Align(
  470. alignment: Alignment.bottomCenter,
  471. child: Image.asset("lib/assets/login-background.png"))
  472. ],
  473. ),
  474.  
  475. ],
  476. ),
  477. floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
  478. floatingActionButton: Padding(
  479. padding: EdgeInsets.all(20),
  480. child: Container(
  481. width: double.infinity,
  482. child: OutlineButton(
  483. borderSide: BorderSide(color: Colors.white),
  484. shape: RoundedRectangleBorder(
  485. borderRadius: BorderRadius.all(Radius.circular(5.0)),
  486. ),
  487. child: Text(
  488. "Login",
  489. style: TextStyle(fontSize: 12, color: Colors.white),
  490. ),
  491. onPressed: () {
  492. login();
  493. },
  494. ),
  495. )
  496. )
  497. );
  498. }
  499. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement