flutter app untuk unitstock
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.
 
 
 
 
 

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