flutter app untuk unitstock
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 

458 rader
22 KiB

  1. import 'dart:io';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'main.dart';
  5. import 'Util/Prefs.dart';
  6. import 'package:permission_handler/permission_handler.dart' as pHandler;
  7. class LoginPage extends StatefulWidget {
  8. LoginPage({Key key}) : super(key: key);
  9. @override
  10. _LoginPageState createState() => _LoginPageState();
  11. }
  12. class _LoginPageState extends State<LoginPage> {
  13. final userCon = new TextEditingController();
  14. final passCon = new TextEditingController();
  15. final hostAddress = new TextEditingController();
  16. List<dynamic> cabangDrop = [];
  17. String selected;
  18. Login()async{
  19. util.showLoading(context);
  20. var result = await util.JsonDataPostRaw({"User":userCon.text,"Pass":passCon.text}, '${prefs.getString(keyClass.hostAddress)}/user//login/');
  21. if(result['STATUS']==1&& result["DATA"]["status"]=='OK'){
  22. prefs.setString(keyClass.loginId, result['DATA'][keyClass.loginId.toLowerCase()]);
  23. // print("user id ${result['DATA'][keyClass.loginId.toLowerCase()]}");
  24. prefs.setString(keyClass.company, result['DATA'][keyClass.company.toLowerCase()]);
  25. prefs.setBool(keyClass.logged_in, true);
  26. if(prefs.getString(keyClass.cabang_id)!= null && prefs.getString(keyClass.cabang_id)!= ''){
  27. Navigator.pop(context);
  28. Navigator.pushNamed(context, '/home');
  29. }
  30. else{
  31. var cabangList = await util.JsonDataPostRaw({"company":prefs.getString(keyClass.company),"User":prefs.getString(keyClass.loginId)}, '${prefs.getString(keyClass.hostAddress)}/user/cabangs/');
  32. Navigator.pop(context);
  33. if(cabangList['STATUS']==1){
  34. cabangDrop = cabangList['DATA'];
  35. selected = cabangDrop[0]['RETURN_VALUE'];
  36. setState(() {
  37. });
  38. FocusScope.of(context).requestFocus(new FocusNode());
  39. }
  40. else{
  41. util.showFlushbar(context, cabangList['DATA'],color: Colors.red);
  42. }
  43. }
  44. }
  45. else{
  46. Navigator.pop(context);
  47. print(result is String);
  48. util.showFlushbar(context, (result['DATA'] is String)?result['DATA']:result['DATA']['info'],color: Colors.red);
  49. }
  50. }
  51. @override
  52. void initState() {
  53. WidgetsBinding.instance.addPostFrameCallback((_) async {
  54. await util.permissionCheck(context,pHandler.Permission.storage,()async{print("storage permit granted!");},customMessage: " untuk menyimpan data backup");
  55. });
  56. super.initState();
  57. }
  58. @override
  59. Widget build(BuildContext context) {
  60. // if(prefs.getBool(keyClass.logged_in)==null||!prefs.getBool(keyClass.logged_in)){
  61. return Scaffold(
  62. resizeToAvoidBottomInset: true,
  63. body: WillPopScope(
  64. onWillPop: ()async{
  65. if(prefs.getBool(keyClass.logged_in)==null||!prefs.getBool(keyClass.logged_in)){
  66. await showDialog(context: context,builder: (context)=>AlertDialog(
  67. content: Text('Exit the app?'),
  68. actions: <Widget>[
  69. TextButton(
  70. child: Text('Exit'),
  71. onPressed: ()async{
  72. Navigator.pop(context);
  73. await locationStream?.cancel();
  74. exit(0);
  75. },
  76. ),
  77. TextButton(
  78. child: Text('Cancel'),
  79. onPressed: (){
  80. Navigator.pop(context);
  81. },
  82. )
  83. ],
  84. ));
  85. }
  86. else{
  87. setState(() {
  88. prefs.setBool(keyClass.logged_in, false);
  89. prefs.remove(keyClass.cabang_id);
  90. prefs.remove(keyClass.company);
  91. prefs.remove(keyClass.loginId);
  92. });
  93. }
  94. return false;
  95. },
  96. child: Container(
  97. decoration: BoxDecoration(
  98. color: Colors.white
  99. ),
  100. child: Stack(
  101. children: <Widget>[
  102. Positioned.fill(
  103. child: Column(
  104. children: <Widget>[
  105. Expanded(
  106. flex: 1,
  107. child: Container(),
  108. ),
  109. Expanded(
  110. flex: 3,
  111. child: InkWell(
  112. onLongPress: ()async{
  113. hostAddress.text = prefs.getString(keyClass.hostAddress);
  114. await showDialog(context: context,builder: (context)=>AlertDialog(
  115. title: Text('Set IP Address'),
  116. content: TextField(
  117. controller: hostAddress,
  118. onSubmitted: (value){
  119. prefs.setString(keyClass.hostAddress,(value=='')?'https://tbg.thamringroup.web.id/ords/tbs/unit/v1':value);
  120. Navigator.pop(context);
  121. },
  122. ),
  123. actions: <Widget>[
  124. TextButton(
  125. child: Text('OK'),
  126. onPressed: (){
  127. prefs.setString(keyClass.hostAddress,(hostAddress.text=='')?'https://tbg.thamringroup.web.id/ords/tbs/unit/v1':hostAddress.text);
  128. Navigator.pop(context);
  129. },
  130. )
  131. ],
  132. ));
  133. },
  134. child: Column(
  135. children: <Widget>[
  136. Container(child: Icon(Icons.input,size: 150,color: Colors.indigo,),),
  137. Text('Login',style: TextStyle(fontSize: 25,color: Colors.indigo,fontWeight: FontWeight.bold),),
  138. ],
  139. ),
  140. ),
  141. ),
  142. Expanded(
  143. flex: 3,
  144. child: Column(
  145. children: <Widget>[
  146. Container(
  147. width: MediaQuery.of(context).size.width*0.8,
  148. child: TextFormField(
  149. controller: userCon,
  150. style: TextStyle(color: Colors.indigo,fontSize: 16),
  151. decoration: InputDecoration(
  152. hintText: 'User ID',
  153. contentPadding:EdgeInsets.only(left: 30,right: 30,top: 20,bottom: 20),
  154. hintStyle: TextStyle(fontSize: 16,color: Colors.indigo.withOpacity(0.7)),
  155. focusedBorder: OutlineInputBorder(
  156. borderSide: BorderSide(
  157. color: Colors.indigo,
  158. width: 2),
  159. borderRadius: BorderRadius.circular(100)
  160. ),
  161. enabledBorder: OutlineInputBorder(
  162. borderSide: BorderSide(
  163. color: Colors.indigo.withOpacity(0.5),
  164. width: 0.5),
  165. borderRadius: BorderRadius.circular(100)
  166. )
  167. ),
  168. ),
  169. ),
  170. SizedBox(height: 15,),
  171. Container(
  172. width: MediaQuery.of(context).size.width*0.8,
  173. child: TextFormField(
  174. obscureText: true,
  175. controller: passCon,
  176. style: TextStyle(color: Colors.indigo,fontSize: 16),
  177. decoration: InputDecoration(
  178. contentPadding:EdgeInsets.only(left: 30,right: 30,top: 20,bottom: 20),
  179. hintText: 'Password',
  180. hintStyle: TextStyle(fontSize: 16,color: Colors.indigo.withOpacity(0.7)),
  181. focusedBorder: OutlineInputBorder(
  182. borderSide: BorderSide(
  183. color: Colors.indigo,
  184. width: 2),
  185. borderRadius: BorderRadius.circular(100)
  186. ),
  187. enabledBorder: OutlineInputBorder(
  188. borderSide: BorderSide(
  189. color: Colors.indigo.withOpacity(0.5),
  190. width: 0.5),
  191. borderRadius: BorderRadius.circular(100)
  192. )
  193. ),
  194. ),
  195. ),
  196. SizedBox(height: 30,),
  197. TextButton(
  198. onPressed: Login,
  199. style: TextButton.styleFrom(
  200. backgroundColor: Colors.indigo,
  201. padding: EdgeInsets.all(15),
  202. shape: RoundedRectangleBorder(
  203. borderRadius: BorderRadius.circular(100.0),
  204. side: BorderSide(color: Colors.indigo)
  205. ),
  206. ),
  207. child: Container(width: MediaQuery.of(context).size.width*0.7,child: Text('Login',textAlign: TextAlign.center,style: TextStyle(color: Colors.white,fontSize: 21),)),
  208. )
  209. ],
  210. ),
  211. ),
  212. // Expanded(
  213. // flex: 3,
  214. // child: Container(color: Colors.yellow,),
  215. // ),
  216. ],
  217. ),
  218. ),
  219. (prefs.getBool(keyClass.logged_in)==null||!prefs.getBool(keyClass.logged_in))
  220. ?Container()
  221. :Positioned.fill(
  222. child: Container(
  223. color: Colors.white.withOpacity(0.9),
  224. child: Center(
  225. child: Container(
  226. decoration: BoxDecoration(
  227. color: Colors.white,
  228. boxShadow: [
  229. BoxShadow(
  230. color: Colors.grey.withOpacity(0.5),
  231. spreadRadius: 5,
  232. blurRadius: 7,
  233. offset: Offset(0, 3), // changes position of shadow
  234. ),
  235. ],
  236. borderRadius: BorderRadius.circular(5)
  237. ),
  238. height: MediaQuery.of(context).size.height/3.2,
  239. width: MediaQuery.of(context).size.width*0.75,
  240. child: Column(
  241. children: <Widget>[
  242. Flexible(
  243. flex:3,
  244. child: Container(
  245. padding: EdgeInsets.only(top:10,left: 10,right: 10),
  246. alignment: Alignment.center,
  247. decoration: BoxDecoration(
  248. // color: Colors.indigo,
  249. borderRadius: BorderRadius.only(topLeft: Radius.circular(5),topRight: Radius.circular(5))
  250. ),
  251. child: Column(
  252. mainAxisSize: MainAxisSize.min,
  253. crossAxisAlignment: CrossAxisAlignment.center,
  254. children: <Widget>[
  255. Icon(Icons.business,size: 70,color: Colors.indigo,),
  256. SizedBox(height: 5,),
  257. Text('Pilih Cabang',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 16,color: Colors.indigo),)
  258. ],
  259. ),
  260. ),
  261. ),
  262. Flexible(
  263. flex: 1,
  264. child: Container(
  265. padding: EdgeInsets.only(left: 20,right: 20),
  266. alignment: Alignment.centerLeft,
  267. child: Theme(
  268. data: ThemeData(
  269. canvasColor: Colors.white,
  270. primaryColor: Colors.indigo,
  271. accentColor: Colors.indigo,
  272. hintColor: Colors.indigo
  273. ),
  274. child: DropdownButtonFormField(
  275. style: TextStyle(color: Colors.black.withOpacity(0.6),fontWeight: FontWeight.w500,fontSize: 14,),
  276. decoration: InputDecoration(
  277. contentPadding: EdgeInsets.all(8.0),
  278. ),
  279. value: selected,
  280. onChanged: (value){
  281. setState(() {
  282. selected = value;
  283. });
  284. },
  285. items: cabangDrop.map<DropdownMenuItem<dynamic>>((item)=>DropdownMenuItem(
  286. value: item['RETURN_VALUE'],
  287. child: Text(item["DISPLAY_VALUE"]),
  288. )).toList(),
  289. ),
  290. ),
  291. ),
  292. ),
  293. Flexible(
  294. flex: 1,
  295. child: Container(
  296. child: Row(
  297. mainAxisAlignment: MainAxisAlignment.end,
  298. children: <Widget>[
  299. ButtonTheme(
  300. minWidth: 10,
  301. child: TextButton(
  302. onPressed: (){
  303. prefs.setString(keyClass.cabang_id, selected);
  304. Navigator.pushNamed(context, '/home');
  305. },
  306. child: Text('Set',style: TextStyle(color: Colors.indigo),),
  307. ),
  308. ),
  309. TextButton(
  310. onPressed: (){
  311. setState(() {
  312. prefs.setBool(keyClass.logged_in, false);
  313. prefs.remove(keyClass.cabang_id);
  314. prefs.remove(keyClass.company);
  315. prefs.remove(keyClass.loginId);
  316. });
  317. },
  318. child: Text('Cancel',style: TextStyle(color: Colors.indigo)),
  319. )
  320. ],
  321. ),
  322. ),
  323. )
  324. ],
  325. ),
  326. ),
  327. ),
  328. )
  329. ),
  330. ],
  331. ),
  332. ),
  333. ),
  334. );
  335. // }
  336. // else{
  337. // return WillPopScope(
  338. // onWillPop: ()async{
  339. // setState(() {
  340. // prefs.setBool(keyClass.logged_in, false);
  341. // prefs.remove(keyClass.cabang_id);
  342. // prefs.remove(keyClass.company);
  343. // prefs.remove(keyClass.loginId);
  344. // });
  345. // return false;
  346. // },
  347. // child: Material(
  348. // child: Container(
  349. // color: Colors.grey.withOpacity(0.7),
  350. // child: Center(
  351. // child: Container(
  352. // decoration: BoxDecoration(
  353. // color: Colors.white,
  354. // borderRadius: BorderRadius.circular(5)
  355. // ),
  356. // height: MediaQuery.of(context).size.height/3.2,
  357. // width: MediaQuery.of(context).size.width*0.75,
  358. // child: Column(
  359. // children: <Widget>[
  360. // Flexible(
  361. // flex:3,
  362. // child: Container(
  363. // padding: EdgeInsets.only(top:10,left: 10,right: 10),
  364. // alignment: Alignment.center,
  365. // decoration: BoxDecoration(
  366. //// color: Colors.indigo,
  367. // borderRadius: BorderRadius.only(topLeft: Radius.circular(5),topRight: Radius.circular(5))
  368. // ),
  369. // child: Column(
  370. // mainAxisSize: MainAxisSize.min,
  371. // crossAxisAlignment: CrossAxisAlignment.center,
  372. // children: <Widget>[
  373. // Icon(Icons.business,size: 70,color: Colors.indigo,),
  374. // SizedBox(height: 5,),
  375. // Text('Pilih Cabang',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 16,color: Colors.indigo),)
  376. // ],
  377. // ),
  378. // ),
  379. // ),
  380. // Flexible(
  381. // flex: 1,
  382. // child: Container(
  383. // padding: EdgeInsets.only(left: 20,right: 20),
  384. // alignment: Alignment.centerLeft,
  385. // child: Theme(
  386. // data: ThemeData(
  387. // canvasColor: Colors.white,
  388. // primaryColor: Colors.indigo,
  389. // accentColor: Colors.indigo,
  390. // hintColor: Colors.indigo
  391. // ),
  392. // child: DropdownButtonFormField(
  393. // style: TextStyle(color: Colors.black.withOpacity(0.6),fontWeight: FontWeight.w500,fontSize: 14,),
  394. // decoration: InputDecoration(
  395. // contentPadding: EdgeInsets.all(8.0),
  396. // ),
  397. // value: selected,
  398. // onChanged: (value){
  399. // setState(() {
  400. // selected = value;
  401. // });
  402. // },
  403. // items: cabangDrop.map<DropdownMenuItem<dynamic>>((item)=>DropdownMenuItem(
  404. // value: item['RETURN_VALUE'],
  405. // child: Text(item["DISPLAY_VALUE"]),
  406. // )).toList(),
  407. // ),
  408. // ),
  409. // ),
  410. // ),
  411. // Flexible(
  412. // flex: 1,
  413. // child: Container(
  414. // child: Row(
  415. // mainAxisAlignment: MainAxisAlignment.end,
  416. // children: <Widget>[
  417. // ButtonTheme(
  418. // minWidth: 10,
  419. // child: TextButton(
  420. // onPressed: (){
  421. // prefs.setString(keyClass.cabang_id, selected);
  422. // Navigator.pushNamed(context, '/home');
  423. // },
  424. // child: Text('Set',style: TextStyle(color: Colors.indigo),),
  425. // ),
  426. // ),
  427. // TextButton(
  428. // onPressed: (){
  429. // setState(() {
  430. // prefs.setBool(keyClass.logged_in, false);
  431. // prefs.remove(keyClass.cabang_id);
  432. // prefs.remove(keyClass.company);
  433. // prefs.remove(keyClass.loginId);
  434. // });
  435. // },
  436. // child: Text('Cancel',style: TextStyle(color: Colors.indigo)),
  437. // )
  438. // ],
  439. // ),
  440. // ),
  441. // )
  442. // ],
  443. // ),
  444. // ),
  445. // ),
  446. // ),
  447. // ),
  448. // );
  449. // }
  450. }
  451. }