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.
 
 
 
 
 

1180 rader
57 KiB

  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:flutter/rendering.dart';
  5. import 'package:flutter/widgets.dart';
  6. import 'package:in_app_update/in_app_update.dart';
  7. import 'package:package_info/package_info.dart';
  8. import 'package:path/path.dart';
  9. import 'package:flutter/cupertino.dart';
  10. import 'package:flutter/material.dart';
  11. import 'package:path_provider/path_provider.dart';
  12. import 'Util/DBHelper.dart';
  13. import 'Util/Util.dart';
  14. import 'Util/download_Upload_Handler.dart';
  15. import 'package:intl/intl.dart';
  16. import 'main.dart';
  17. import 'Util/Prefs.dart';
  18. import 'Util/UnitModel.dart';
  19. class HomePage extends StatefulWidget {
  20. // HomePage({Key key}) : super(key: key);
  21. final String title;
  22. HomePage({this.title});
  23. @override
  24. _HomePageState createState() => _HomePageState();
  25. }
  26. class _HomePageState extends State<HomePage> {
  27. Util util = new Util();
  28. StreamSubscription progressDLStream,progressULStream;
  29. String lastDownload = '';
  30. String lastUpload = '';
  31. double progressDL,progressUL;
  32. String timeString = '';
  33. bool isLoading = false;
  34. String state = '';
  35. final hostAddress = new TextEditingController();
  36. clearData(context)async{
  37. setState(() {
  38. progressDL = null;
  39. isLoading = false;
  40. });
  41. String errMsg;
  42. bool result = await showDialog(context: context,builder: (context)=>WillPopScope(
  43. onWillPop: ()async{
  44. Navigator.pop(context,false);
  45. return false;
  46. },
  47. child: AlertDialog(
  48. title: Text('Clear Data ?'),
  49. content: Text('Proceed to clear any remaining units data on this device?'),
  50. actions: <Widget>[
  51. TextButton(
  52. child: Text('Proceed'),
  53. onPressed: ()async{
  54. util.showLoading(context);
  55. await Future.sync(()async{
  56. await prefs.remove(keyClass.lastDownload);
  57. await prefs.remove(keyClass.lastUpload);
  58. await prefs.remove(keyClass.targetProccess);
  59. await prefs.remove(keyClass.submitProccess);
  60. await prefs.remove(keyClass.stock_id);
  61. setState(() {
  62. lastUpload = '';
  63. lastDownload = '';
  64. timeString = '';
  65. });
  66. // var result = await DBHelper.database.deleteAll();
  67. // if(result!=null) {
  68. // await DBHelper.database.closeDb();
  69. // if(result!=null){
  70. try{
  71. Directory documentsDirectory = await getExternalStorageDirectory();
  72. String path = join(documentsDirectory.path, "UnitStocking.db");
  73. File db = File(path);
  74. if(db.existsSync()){
  75. db.deleteSync();
  76. }
  77. }
  78. catch(e){
  79. print(e);
  80. util.showFlushbar(context, 'Failed to delete database file',color: Colors.red);
  81. }
  82. // }
  83. // else{
  84. // errMsg = 'Failed to clear unit data';
  85. // }
  86. // }
  87. // else{
  88. // errMsg = 'Failed to clear unit data';
  89. // }
  90. });
  91. Navigator.pop(context);
  92. Navigator.pop(context,true);
  93. util.showFlushbar(context,errMsg??'Data Cleared');
  94. },
  95. ),
  96. TextButton(
  97. child: Text('Cancel'),
  98. onPressed: (){Navigator.pop(context,false);},
  99. )
  100. ],
  101. ),
  102. ));
  103. return result;
  104. }
  105. loadState()async{
  106. String company = prefs.getString(keyClass.company);
  107. if(prefs.getString(keyClass.stock_id) == null)
  108. {
  109. valueTab value = await DBHelper.database.getValue(keyClass.stock_id);
  110. if(value != null)await prefs.setString(keyClass.stock_id, value.value);
  111. }
  112. String stock_taking_id = prefs.getString(keyClass.stock_id);
  113. if(company!=null&&stock_taking_id!=null){
  114. var result = await util.JsonDataPostRaw({"company":company,"stockTakingId":stock_taking_id}, '${prefs.getString(keyClass.hostAddress)}/stock_taking/state/');
  115. if(result['STATUS']==1){
  116. setState(() {
  117. state = result['DATA'];
  118. });
  119. }
  120. }
  121. }
  122. @override
  123. void initState() {
  124. // TODO: implement initState
  125. super.initState();
  126. loadState();
  127. lastDownload = prefs.getString(keyClass.lastDownload)??'';
  128. lastUpload = prefs.getString(keyClass.lastUpload)??'';
  129. WidgetsBinding.instance.addPostFrameCallback((_) async {
  130. await _check_Update();
  131. });
  132. }
  133. _check_Update()async{
  134. if(defaultTargetPlatform == TargetPlatform.android){
  135. try{
  136. final PackageInfo info = await PackageInfo.fromPlatform();
  137. AppUpdateInfo _updateInfo = await InAppUpdate.checkForUpdate();
  138. String currentVersion = info.version.trim();
  139. String latestversion = '0.0.0';
  140. var result = await util.getMinAppVer();
  141. if(result['STATUS']==1){
  142. latestversion = result['DATA'];
  143. }
  144. var current = currentVersion.split('.');
  145. var latest = latestversion.split('.');
  146. if(int.parse(current[0])==int.parse(latest[0])){
  147. if(int.parse(current[1])==int.parse(latest[1])){
  148. if(int.parse(current[2])<int.parse(latest[2])) {
  149. await util.updateDialog(context);
  150. }
  151. else{
  152. if(_updateInfo?.updateAvailability == 2) {
  153. await InAppUpdate.startFlexibleUpdate();
  154. await InAppUpdate.completeFlexibleUpdate();
  155. }
  156. }
  157. }
  158. else if(int.parse(current[1])<int.parse(latest[1])) {
  159. await util.updateDialog(context);
  160. // await InAppUpdate.performImmediateUpdate();
  161. }
  162. else{
  163. if(_updateInfo?.updateAvailability == 2) {
  164. await InAppUpdate.startFlexibleUpdate();
  165. await InAppUpdate.completeFlexibleUpdate();
  166. }
  167. }
  168. }
  169. else if(int.parse(current[0])<int.parse(latest[0])) {
  170. await util.updateDialog(context);
  171. // await InAppUpdate.performImmediateUpdate();
  172. }
  173. else{
  174. if(_updateInfo?.updateAvailability == 2 ) {
  175. await InAppUpdate.startFlexibleUpdate();
  176. await InAppUpdate.completeFlexibleUpdate();
  177. }
  178. }
  179. }
  180. catch(e){
  181. print("error Update $e");
  182. }
  183. }
  184. }
  185. changeCabang(context)async{
  186. util.showLoading(context);
  187. var result = await util.JsonDataPostRaw({"company":prefs.getString(keyClass.company),"User":prefs.getString(keyClass.loginId)}, '${prefs.getString(keyClass.hostAddress)}/user/cabangs/');
  188. Navigator.pop(context);
  189. if(result['STATUS']==1){
  190. var selected = prefs.getString(keyClass.cabang_id)??result['DATA'][0]["RETURN_VALUE"];
  191. return await showDialog(context: context,builder: (context)=>StatefulBuilder(
  192. builder:(context,setState)=>
  193. Material(
  194. color: Colors.white.withOpacity(0.9),
  195. child: Center(
  196. child: Container(
  197. decoration: BoxDecoration(
  198. color: Colors.white,
  199. boxShadow: [
  200. BoxShadow(
  201. color: Colors.grey.withOpacity(0.5),
  202. spreadRadius: 2,
  203. blurRadius: 2,
  204. offset: Offset(0, 0), // changes position of shadow
  205. ),
  206. ],
  207. borderRadius: BorderRadius.circular(5)
  208. ),
  209. // height: MediaQuery.of(context).size.height/3.2,
  210. height:220,
  211. width: MediaQuery.of(context).size.width*0.75,
  212. child: Column(
  213. children: <Widget>[
  214. Flexible(
  215. flex:3,
  216. child: Container(
  217. padding: EdgeInsets.only(top:10,left: 10,right: 10),
  218. alignment: Alignment.center,
  219. decoration: BoxDecoration(
  220. // color: Colors.indigo,
  221. borderRadius: BorderRadius.only(topLeft: Radius.circular(5),topRight: Radius.circular(5))
  222. ),
  223. child: Column(
  224. mainAxisSize: MainAxisSize.min,
  225. crossAxisAlignment: CrossAxisAlignment.center,
  226. children: <Widget>[
  227. Icon(Icons.business,size: 70,color: Colors.indigo,),
  228. SizedBox(height: 5,),
  229. Text('Set Cabang',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 16,color: Colors.indigo),)
  230. ],
  231. ),
  232. ),
  233. ),
  234. Flexible(
  235. flex: 1,
  236. child: Container(
  237. padding: EdgeInsets.only(left: 20,right: 20),
  238. alignment: Alignment.centerLeft,
  239. child: Theme(
  240. data: ThemeData(
  241. canvasColor: Colors.white,
  242. primaryColor: Colors.indigo,
  243. accentColor: Colors.indigo,
  244. hintColor: Colors.indigo
  245. ),
  246. child: DropdownButtonFormField(
  247. style: TextStyle(color: Colors.black.withOpacity(0.6),fontWeight: FontWeight.w500,fontSize: 14,),
  248. decoration: InputDecoration(
  249. contentPadding: EdgeInsets.all(8.0),
  250. ),
  251. value: selected,
  252. onChanged: (value){
  253. setState(() {
  254. selected = value;
  255. });
  256. },
  257. items: result['DATA'].map<DropdownMenuItem<dynamic>>((item){
  258. return DropdownMenuItem(
  259. value: item['RETURN_VALUE'],
  260. child: Text(item["DISPLAY_VALUE"]),
  261. );
  262. }).toList(),
  263. ),
  264. ),
  265. ),
  266. ),
  267. Flexible(
  268. flex: 1,
  269. child: Container(
  270. child: Row(
  271. mainAxisAlignment: MainAxisAlignment.end,
  272. children: <Widget>[
  273. TextButton(
  274. child: Text('OK',style: TextStyle(color: Colors.indigo),),
  275. onPressed: ()async{
  276. if(selected!=prefs.getString(keyClass.cabang_id)){
  277. prefs.setString(keyClass.cabang_id,selected);
  278. await Future.sync(()async{
  279. await prefs.remove(keyClass.lastDownload);
  280. await prefs.remove(keyClass.lastUpload);
  281. await prefs.remove(keyClass.targetProccess);
  282. await prefs.remove(keyClass.submitProccess);
  283. lastUpload = '';
  284. lastDownload = '';
  285. timeString = '';
  286. try{
  287. Directory documentsDirectory = await getExternalStorageDirectory();
  288. String path = join(documentsDirectory.path, "UnitStocking.db");
  289. File db = File(path);
  290. if(db.existsSync()){
  291. db.deleteSync();
  292. }
  293. }
  294. catch(e){
  295. print(e);
  296. util.showFlushbar(context, 'Failed to delete database file',color: Colors.red);
  297. }
  298. });
  299. Navigator.pop(context,true);
  300. }
  301. else{
  302. Navigator.pop(context,false);
  303. }
  304. },
  305. ),
  306. ],
  307. ),
  308. ),
  309. )
  310. ],
  311. ),
  312. ),
  313. ),
  314. ),
  315. // AlertDialog(
  316. // title: Text('Set Cabang'),
  317. // content: DropdownButtonFormField(
  318. // value: selected,
  319. // onChanged: (value){
  320. // setState(() {
  321. // selected = value;
  322. // });
  323. // },
  324. // items: result['DATA'].map<DropdownMenuItem<dynamic>>((item){
  325. // return DropdownMenuItem(
  326. // value: item['RETURN_VALUE'],
  327. // child: Text(item["DISPLAY_VALUE"]),
  328. // );
  329. // }).toList(),
  330. // ),
  331. // actions: <Widget>[
  332. // TextButton(
  333. // child: Text('OK'),
  334. // onPressed: ()async{
  335. // if(selected!=prefs.getString(keyClass.cabang_id)){
  336. // prefs.setString(keyClass.cabang_id,selected);
  337. // await Future.sync(()async{
  338. // await prefs.remove(keyClass.lastDownload);
  339. // await prefs.remove(keyClass.lastUpload);
  340. // await prefs.remove(keyClass.targetProccess);
  341. // await prefs.remove(keyClass.submitProccess);
  342. // lastUpload = '';
  343. // lastDownload = '';
  344. // timeString = '';
  345. //// var result = await DBHelper.database.deleteAll();
  346. //// if(result!=null) {
  347. //// await DBHelper.database.closeDb();
  348. //// if(result!=null){
  349. // try{
  350. // Directory documentsDirectory = await getExternalStorageDirectory();
  351. // String path = join(documentsDirectory.path, "UnitStocking.db");
  352. // File db = File(path);
  353. // if(db.existsSync()){
  354. // db.deleteSync();
  355. // }
  356. // }
  357. // catch(e){
  358. // print(e);
  359. // util.showFlushbar(context, 'Failed to delete database file',color: Colors.red);
  360. // }
  361. //// }
  362. //// else{
  363. //// errMsg = 'Failed to clear unit data';
  364. //// }
  365. //// }
  366. //// else{
  367. //// errMsg = 'Failed to clear unit data';
  368. //// }
  369. // });
  370. // Navigator.pop(context,true);
  371. // }
  372. // else{
  373. // Navigator.pop(context,false);
  374. // }
  375. //// util.showLoading(context);
  376. //
  377. //// Navigator.pop(context);
  378. // },
  379. // )
  380. // ],
  381. // ),
  382. ));
  383. }
  384. else{
  385. util.showFlushbar(context, result['DATA'],color: Colors.red);
  386. return false;
  387. }
  388. }
  389. @override
  390. Widget build(BuildContext context) {
  391. return WillPopScope(
  392. onWillPop: ()async{
  393. await showDialog(context: context,builder: (context)=>
  394. Material(
  395. color: Colors.white.withOpacity(0.9),
  396. child: Center(
  397. child: Container(
  398. decoration: BoxDecoration(
  399. color: Colors.white,
  400. boxShadow: [
  401. BoxShadow(
  402. color: Colors.grey.withOpacity(0.5),
  403. spreadRadius: 2,
  404. blurRadius: 2,
  405. offset: Offset(0, 0), // changes position of shadow
  406. ),
  407. ],
  408. borderRadius: BorderRadius.circular(5)
  409. ),
  410. // height: MediaQuery.of(context).size.height/4.8,
  411. height:220,
  412. width: MediaQuery.of(context).size.width*0.75,
  413. child: Column(
  414. children: <Widget>[
  415. Flexible(
  416. flex:3,
  417. child: Container(
  418. padding: EdgeInsets.only(top:10,left: 10,right: 10),
  419. alignment: Alignment.center,
  420. decoration: BoxDecoration(
  421. // color: Colors.indigo,
  422. borderRadius: BorderRadius.only(topLeft: Radius.circular(5),topRight: Radius.circular(5))
  423. ),
  424. child: Column(
  425. mainAxisSize: MainAxisSize.min,
  426. crossAxisAlignment: CrossAxisAlignment.center,
  427. children: <Widget>[
  428. Icon(Icons.exit_to_app,size: 70,color: Colors.indigo,),
  429. ],
  430. ),
  431. ),
  432. ),
  433. Flexible(
  434. flex: 1,
  435. child: Container(
  436. padding: EdgeInsets.only(left: 20,right: 20),
  437. alignment: Alignment.centerLeft,
  438. child: Text('Keluar dari aplikasi?',style: TextStyle(fontWeight: FontWeight.w500,fontSize: 14,color: Colors.black.withOpacity(0.6)),)
  439. ),
  440. ),
  441. Flexible(
  442. flex: 1,
  443. child: Container(
  444. padding: EdgeInsets.only(bottom:10),
  445. child: Row(
  446. mainAxisAlignment: MainAxisAlignment.end,
  447. children: <Widget>[
  448. TextButton(
  449. child: Text('Exit',style: TextStyle(color: Colors.indigo),),
  450. onPressed: ()async{
  451. Navigator.pop(context);
  452. await locationStream?.cancel();
  453. exit(0);
  454. },
  455. ),
  456. TextButton(
  457. child: Text('Cancel',style: TextStyle(color: Colors.indigo),),
  458. onPressed: (){
  459. Navigator.pop(context);
  460. },
  461. )
  462. ],
  463. ),
  464. ),
  465. )
  466. ],
  467. ),
  468. ),
  469. ),
  470. ),
  471. );
  472. return false;
  473. },
  474. child: Scaffold(
  475. body: Column(
  476. crossAxisAlignment: CrossAxisAlignment.end,
  477. children: <Widget>[
  478. Container(
  479. padding: EdgeInsets.only(bottom: 0,left: 15,right: 10,top: 10),
  480. height: MediaQuery.of(context).size.height/13,
  481. width: MediaQuery.of(context).size.width,
  482. alignment: Alignment.bottomLeft,
  483. child: Row(
  484. crossAxisAlignment: CrossAxisAlignment.end,
  485. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  486. children: <Widget>[
  487. Text('Home',style: TextStyle(color: Colors.indigo.withOpacity(0.8),fontSize: 18,fontWeight: FontWeight.bold),),
  488. PopupMenuButton(
  489. child: Padding(
  490. padding: const EdgeInsets.only(right:2.0),
  491. child: Icon(Icons.menu,color: Colors.indigo.withOpacity(0.8),),
  492. ),
  493. itemBuilder: (context){
  494. return [
  495. PopupMenuItem(
  496. value:'hostChange',
  497. child: Row(
  498. children: <Widget>[
  499. Icon(Icons.network_wifi,color: Colors.black,),
  500. SizedBox(width: 10,),
  501. Text('Change Ip Address')
  502. ],
  503. ),
  504. ),
  505. PopupMenuItem(
  506. value:'cabangChange',
  507. child: Row(
  508. children: <Widget>[
  509. Icon(Icons.business,color: Colors.black,),
  510. SizedBox(width: 10,),
  511. Text('Change Cabang')
  512. ],
  513. ),
  514. ),
  515. PopupMenuItem(
  516. value:'restore',
  517. child: Row(
  518. children: <Widget>[
  519. Icon(Icons.sync,color: Colors.black,),
  520. SizedBox(width: 10,),
  521. Text('Restore Backup')
  522. ],
  523. ),
  524. ),
  525. PopupMenuItem(
  526. value:'logout',
  527. child: Row(
  528. children: <Widget>[
  529. Icon(Icons.exit_to_app,color: Colors.black,),
  530. SizedBox(width: 10,),
  531. Text('Logout')
  532. ],
  533. ),
  534. ),
  535. ];
  536. },
  537. onSelected: (value)async{
  538. if(value == 'hostChange'){
  539. hostAddress.text = prefs.getString(keyClass.hostAddress);
  540. await showDialog(context: context,builder: (context)=>
  541. Material(
  542. color: Colors.white.withOpacity(0.9),
  543. child: Center(
  544. child: Container(
  545. decoration: BoxDecoration(
  546. color: Colors.white,
  547. boxShadow: [
  548. BoxShadow(
  549. color: Colors.grey.withOpacity(0.5),
  550. spreadRadius: 2,
  551. blurRadius: 2,
  552. offset: Offset(0, 0), // changes position of shadow
  553. ),
  554. ],
  555. borderRadius: BorderRadius.circular(5)
  556. ),
  557. height:220,
  558. // height: MediaQuery.of(context).size.height/3.2,
  559. width: MediaQuery.of(context).size.width*0.75,
  560. child: Column(
  561. children: <Widget>[
  562. Flexible(
  563. flex:3,
  564. child: Container(
  565. padding: EdgeInsets.only(top:10,left: 10,right: 10),
  566. alignment: Alignment.center,
  567. decoration: BoxDecoration(
  568. // color: Colors.indigo,
  569. borderRadius: BorderRadius.only(topLeft: Radius.circular(5),topRight: Radius.circular(5))
  570. ),
  571. child: Column(
  572. mainAxisSize: MainAxisSize.min,
  573. crossAxisAlignment: CrossAxisAlignment.center,
  574. children: <Widget>[
  575. Icon(Icons.network_wifi,size: 70,color: Colors.indigo,),
  576. SizedBox(height: 5,),
  577. Text('Set Ip Address',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 16,color: Colors.indigo),)
  578. ],
  579. ),
  580. ),
  581. ),
  582. Flexible(
  583. flex: 1,
  584. child: Container(
  585. padding: EdgeInsets.only(left: 20,right: 20),
  586. alignment: Alignment.centerLeft,
  587. child: Theme(
  588. data: ThemeData(
  589. canvasColor: Colors.white,
  590. primaryColor: Colors.indigo,
  591. accentColor: Colors.indigo,
  592. hintColor: Colors.indigo
  593. ),
  594. child: TextField(
  595. controller: hostAddress,
  596. decoration: InputDecoration(
  597. contentPadding: EdgeInsets.all(8),
  598. ),
  599. onSubmitted: (value){
  600. prefs.setString(keyClass.hostAddress,(value=='')?'https://tbg.thamringroup.web.id/ords/tbs/unit/v1':value);
  601. Navigator.pop(context);
  602. },
  603. ),
  604. ),
  605. ),
  606. ),
  607. Flexible(
  608. flex: 1,
  609. child: Container(
  610. child: Row(
  611. mainAxisAlignment: MainAxisAlignment.end,
  612. children: <Widget>[
  613. TextButton(
  614. child: Text('OK',style: TextStyle(color: Colors.indigo),),
  615. onPressed: (){
  616. prefs.setString(keyClass.hostAddress,(hostAddress.text=='')?'https://tbg.thamringroup.web.id/ords/tbs/unit/v1':hostAddress.text);
  617. Navigator.pop(context);
  618. },
  619. )
  620. ],
  621. ),
  622. ),
  623. )
  624. ],
  625. ),
  626. ),
  627. ),
  628. )
  629. // AlertDialog(
  630. // title: Text('Set IP Address'),
  631. // content: TextField(
  632. // controller: hostAddress,
  633. // onSubmitted: (value){
  634. // prefs.setString(keyClass.hostAddress,(value=='')?'https://unitstocksbackend.thamringroup.web.id':value);
  635. // Navigator.pop(context);
  636. // },
  637. // ),
  638. // actions: <Widget>[
  639. // TextButton(
  640. // child: Text('OK'),
  641. // onPressed: (){
  642. // prefs.setString(keyClass.hostAddress,(hostAddress.text=='')?'https://unitstocksbackend.thamringroup.web.id':hostAddress.text);
  643. // Navigator.pop(context);
  644. // },
  645. // )
  646. // ],
  647. // )
  648. );
  649. }
  650. if(value=="cabangChange") {
  651. var res = await changeCabang(context);
  652. if(res??false){
  653. setState(() {
  654. });
  655. }
  656. }
  657. if(value == 'logout'){
  658. prefs.remove(keyClass.cabang_id);
  659. prefs.remove(keyClass.company);
  660. prefs.remove(keyClass.loginId);
  661. prefs.setBool(keyClass.logged_in, false);
  662. Navigator.pushNamed(context, '/login');
  663. }
  664. if(value == 'restore'){
  665. await showDialog(context: context,builder: (context)=>AlertDialog(
  666. title: Text('Restore Data ?'),
  667. content: Text('Restore data akan menghapus dan mengganti data unit yang ada dengan data unit backup.'),
  668. actions: <Widget>[
  669. TextButton(
  670. child: Text('Proceed'),
  671. onPressed: ()async{
  672. util.showLoading(context);
  673. var result = await DBHelper.database.restoreDb();
  674. if(result["STATUS"]==1){
  675. await prefs.remove(keyClass.lastDownload);
  676. await prefs.remove(keyClass.lastUpload);
  677. await prefs.remove(keyClass.targetProccess);
  678. await prefs.remove(keyClass.submitProccess);
  679. setState(() {
  680. lastUpload = '';
  681. lastDownload = '';
  682. timeString = '';
  683. });
  684. valueTab value = await DBHelper.database.getValue(keyClass.tgl_start);
  685. if(value != null)await prefs.setString(keyClass.lastDownload, value.value);
  686. }
  687. util.showFlushbar(context, result["MSG"]);
  688. Navigator.pop(context);
  689. Navigator.pop(context);
  690. },
  691. ),
  692. TextButton(
  693. child: Text('Cancel'),
  694. onPressed: ()=>Navigator.pop(context),
  695. )
  696. ],
  697. ));
  698. }
  699. },
  700. ),
  701. ],
  702. ),
  703. ),
  704. (prefs.getString(keyClass.stock_id)==null)?Container():Padding(
  705. padding: const EdgeInsets.only(top: 8,right: 8),
  706. child: Container(
  707. decoration: BoxDecoration(
  708. color: Colors.indigo.withOpacity(0.9),
  709. borderRadius: BorderRadius.circular(8)
  710. // border: Border.all(),
  711. ),
  712. padding: EdgeInsets.all(8),
  713. child: Row(
  714. mainAxisSize: MainAxisSize.min,
  715. mainAxisAlignment: MainAxisAlignment.end,
  716. crossAxisAlignment: CrossAxisAlignment.center,
  717. children: <Widget>[
  718. Text('Status : ',style: TextStyle(color: Colors.white),),
  719. Text(state??'-',style: TextStyle(fontWeight: FontWeight.bold,color: Colors.white),),
  720. ],
  721. ),
  722. ),
  723. ),
  724. Expanded(
  725. child: Container(
  726. child:
  727. GridView.count(
  728. padding: EdgeInsets.all(10),
  729. crossAxisSpacing: 10,
  730. mainAxisSpacing: 10,
  731. crossAxisCount: 2,
  732. children: <Widget>[
  733. AbsorbPointer(
  734. absorbing: isLoading,
  735. child: InkWell(
  736. splashColor: Colors.black,
  737. onTap: ()async{
  738. bool getData = await showDialog(
  739. context: context,
  740. builder: (context)=>AlertDialog(
  741. title: Text("Get Data Units?"),
  742. content: Text('Fetch data unit for stocking'),
  743. actions: <Widget>[
  744. TextButton(
  745. child: Text('Proceed'),
  746. onPressed: (){
  747. Navigator.pop(context,true);
  748. },
  749. ),
  750. TextButton(
  751. child: Text('Cancel'),
  752. onPressed: (){
  753. Navigator.pop(context,false);
  754. },
  755. )
  756. ],
  757. )
  758. );
  759. if(getData??false){
  760. setState(() {
  761. isLoading = true;
  762. });
  763. var a = Stopwatch();
  764. a.start();
  765. await Future.delayed(Duration(milliseconds: 200));
  766. bool isclear;
  767. Directory documentsDirectory = await getExternalStorageDirectory();
  768. String path = join(documentsDirectory.path, "UnitStocking.db");
  769. File db = File(path);
  770. if(db.existsSync()){
  771. isclear = await clearData(context);
  772. }
  773. else{
  774. isclear = true;
  775. }
  776. if(isclear??false){
  777. print('fetching');
  778. util.showLoading(context);
  779. file_Trans_Handler trans = new file_Trans_Handler();
  780. // trans.downloadFile('UnitStocking.db',"http://172.16.1.8:14002/getSqliteDbtest/TBS/02");
  781. trans.downloadFile('UnitStocking.db',"${prefs.getString(keyClass.hostAddress)}/stock_taking/get_units/${prefs.getString(keyClass.company)}/${prefs.getString(keyClass.cabang_id)}");
  782. progressDLStream = trans.progress.listen((value)async {
  783. if(progressDL==null) {
  784. // print(['test',value]);
  785. if(value!=null) Navigator.pop(context);
  786. }
  787. setState(() {
  788. progressDL = (value!=-1.0)?value:null;
  789. });
  790. if(value!=null&&value >= 1.0) {
  791. progressDLStream.cancel();
  792. progressDL = null;
  793. setState(() {
  794. lastDownload = DateTime.now().toIso8601String();
  795. });
  796. await prefs.setString(keyClass.lastDownload, lastDownload);
  797. await DBHelper.database.insertUpdateValue(new valueTab(name: 'TGL_START',value: DateFormat('dd-MM-yyyy HH:mm:ss').format(DateTime.parse(lastDownload))));
  798. await DBHelper.database.insertUpdateValue(new valueTab(name: 'TGL_STOCK_TAKING',value: DateFormat('dd-MM-yyyy').format(DateTime.parse(lastDownload))));
  799. await DBHelper.database.closeDb();
  800. util.showFlushbar(context,'Data downloaded');
  801. loadState();
  802. a.stop();
  803. if(a.elapsed.inSeconds<3){
  804. await Future.delayed(Duration(seconds: 3-a.elapsed.inSeconds));
  805. }
  806. setState(() {
  807. isLoading = false;
  808. });
  809. }
  810. if(value==-1.0){
  811. setState(() {
  812. isLoading = false;
  813. });
  814. util.showFlushbar(context,trans.error??'Data download error',color: Colors.red);
  815. }
  816. });
  817. // Navigator.popUntil(context, ModalRoute.withName('/home'));
  818. }
  819. }
  820. },
  821. child: Container(
  822. padding: EdgeInsets.all(5),
  823. decoration: BoxDecoration(
  824. boxShadow: [
  825. BoxShadow(
  826. color: Colors.grey.withOpacity(0.5),
  827. spreadRadius: 2,
  828. blurRadius: 2,
  829. offset: Offset(0, 0), // changes position of shadow
  830. ),
  831. ],
  832. borderRadius: BorderRadius.all(Radius.circular(5.0)),
  833. color: Colors.green.withAlpha(220).withOpacity(0.7),
  834. ),
  835. child: Stack(
  836. children: <Widget>[
  837. Container(
  838. alignment: (progressDL!=null)?Alignment.bottomCenter:Alignment.bottomLeft,
  839. padding: EdgeInsets.only(left: 10,right: 10,top: 10,bottom: 15),
  840. child: (progressDL!=null)?Column(
  841. mainAxisSize: MainAxisSize.min,
  842. children: <Widget>[
  843. LinearProgressIndicator(
  844. value: progressDL,
  845. ),
  846. Text('${(progressDL*100).floor()}%',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold),)
  847. ],
  848. ):(lastDownload=='')?null:Text('Last download : ${DateFormat('dd MMM yyyy HH:mm').format(DateTime.parse(lastDownload))}',style: TextStyle(color: Colors.white,fontSize: 11,fontWeight: FontWeight.bold)),
  849. ),
  850. Positioned.fill(
  851. child: Column(
  852. mainAxisAlignment: MainAxisAlignment.center,
  853. children: <Widget>[
  854. Icon(Icons.file_upload,size: MediaQuery.of(context).size.width/6,color: Colors.white,),
  855. SizedBox(height: 5,),
  856. Text('Get Data Master',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 16),),
  857. ],
  858. ),
  859. ),
  860. ],
  861. ),
  862. ),
  863. ),
  864. ),
  865. InkWell(
  866. splashColor: Colors.black,
  867. onTap: ()async{
  868. await Future.delayed(Duration(milliseconds: 200));
  869. if(lastDownload!=''){
  870. Navigator.pushNamed(context, '/stocking');
  871. }
  872. else{
  873. util.showFlushbar(context, 'Data Master tidak ditemukan. Get data master dulu.');
  874. }
  875. },
  876. child: Container(
  877. padding: EdgeInsets.all(5),
  878. decoration: BoxDecoration(
  879. boxShadow: [
  880. BoxShadow(
  881. color: Colors.grey.withOpacity(0.5),
  882. spreadRadius: 2,
  883. blurRadius: 2,
  884. offset: Offset(0, 0), // changes position of shadow
  885. ),
  886. ],
  887. borderRadius: BorderRadius.all(Radius.circular(5.0)),
  888. color: Colors.blueGrey.withAlpha(230).withOpacity(0.8),
  889. ),
  890. child: Column(
  891. mainAxisAlignment: MainAxisAlignment.center,
  892. children: <Widget>[
  893. Icon(Icons.receipt,size: MediaQuery.of(context).size.width/6,color: Colors.white,),
  894. SizedBox(height: 5,),
  895. Text('Stocking',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 16),)
  896. ],
  897. ),
  898. ),
  899. ),
  900. InkWell(
  901. splashColor: Colors.black,
  902. onTap: ()async{
  903. if(lastDownload!=''){
  904. await DBHelper.database.insertUpdateValue(new valueTab(name: 'TGL_SELESAI',value: DateFormat('dd-MM-yyyy HH:mm:ss').format(new DateTime.now())));
  905. await DBHelper.database.closeDb();
  906. file_Trans_Handler trans = new file_Trans_Handler();
  907. bool popped = false;
  908. util.showLoading(context,onwillpop:()async{
  909. await trans.cancel();
  910. popped = true;
  911. return true;
  912. });
  913. var upload = await trans.uploadFile('UnitStocking.db',"${prefs.getString(keyClass.hostAddress)}/stock_taking/upload/",prefs.getString(keyClass.company),prefs.getString(keyClass.cabang_id));
  914. if(!popped){
  915. Navigator.pop(context);
  916. util.showFlushbar(context, upload['DATA'],color:(upload['STATUS']!=1)?Colors.red:Colors.grey);
  917. if(upload['STATUS']==1){
  918. setState(() {
  919. lastUpload = new DateTime.now().toIso8601String();
  920. });
  921. prefs.setString(keyClass.lastUpload, lastUpload);
  922. prefs.setString(keyClass.targetProccess, upload[keyClass.targetProccess]);
  923. }
  924. }
  925. }
  926. else{
  927. util.showFlushbar(context, "Belum ada data yang disimpan.",color: Colors.red);
  928. }
  929. },
  930. child: Container(
  931. padding: EdgeInsets.all(5),
  932. decoration: BoxDecoration(
  933. boxShadow: [
  934. BoxShadow(
  935. color: Colors.grey.withOpacity(0.5),
  936. spreadRadius: 2,
  937. blurRadius: 2,
  938. offset: Offset(0, 0), // changes position of shadow
  939. ),
  940. ],
  941. borderRadius: BorderRadius.all(Radius.circular(5.0)),
  942. color: Colors.indigo.withAlpha(220).withOpacity(0.7),
  943. ),
  944. child: Stack(
  945. children: <Widget>[
  946. Container(
  947. alignment: (progressUL!=null)?Alignment.bottomCenter:Alignment.bottomLeft,
  948. padding: EdgeInsets.only(left: 10,right: 10,top: 10,bottom: 0),
  949. child: (progressUL!=null)?Column(
  950. mainAxisSize: MainAxisSize.min,
  951. children: <Widget>[
  952. LinearProgressIndicator(
  953. value: progressUL,
  954. ),
  955. Text('${(progressUL*100).floor()}%',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold),)
  956. ],
  957. ):(lastUpload=='')?null:Text('Last upload : ${DateFormat('dd MMM yyyy HH:mm').format(DateTime.parse(lastUpload))}\n${(timeString!='')?'Duration: $timeString':''}',style: TextStyle(color: Colors.white,fontSize: 11,fontWeight: FontWeight.bold)),
  958. ),
  959. Positioned.fill(
  960. child: Column(
  961. mainAxisAlignment: MainAxisAlignment.center,
  962. children: <Widget>[
  963. Icon(Icons.file_upload,size: MediaQuery.of(context).size.width/6,color: Colors.white,),
  964. SizedBox(height: 5,),
  965. Text('Send Data Stokan',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 16),),
  966. ],
  967. ),
  968. ),
  969. ],
  970. ),
  971. ),
  972. ),
  973. (prefs.getString(keyClass.targetProccess)==null&&prefs.getBool(keyClass.submitProccess)==null)?InkWell(
  974. splashColor: Colors.black,
  975. onTap: ()async{
  976. await Future.delayed(Duration(milliseconds: 200));
  977. await clearData(context);
  978. },
  979. child: Container(
  980. padding: EdgeInsets.all(5),
  981. decoration: BoxDecoration(
  982. boxShadow: [
  983. BoxShadow(
  984. color: Colors.grey.withOpacity(0.5),
  985. spreadRadius: 2,
  986. blurRadius: 2,
  987. offset: Offset(0, 0), // changes position of shadow
  988. ),
  989. ],
  990. borderRadius: BorderRadius.all(Radius.circular(5.0)),
  991. color: Colors.red.withAlpha(200).withOpacity(0.7),
  992. ),
  993. child: Column(
  994. mainAxisAlignment: MainAxisAlignment.center,
  995. children: <Widget>[
  996. Icon(Icons.restore_from_trash,size: MediaQuery.of(context).size.width/6,color: Colors.white,),
  997. SizedBox(height: 5,),
  998. Text('Clear Data',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 16),)
  999. ],
  1000. ),
  1001. ),
  1002. ):
  1003. (prefs.getString(keyClass.targetProccess)!=null)?InkWell(
  1004. onTap: ()async{
  1005. bool result = await showDialog(context: context,builder: (context)=>WillPopScope(
  1006. onWillPop: ()async{
  1007. Navigator.pop(context,false);
  1008. return false;
  1009. },
  1010. child: AlertDialog(
  1011. title: Text('Process Data ?'),
  1012. content: Text('Proceed to unpack the uploaded data.'),
  1013. actions: <Widget>[
  1014. TextButton(
  1015. child: Text('Proceed'),
  1016. onPressed: ()async{
  1017. Navigator.pop(context,true);
  1018. },
  1019. ),
  1020. TextButton(
  1021. child: Text('Cancel'),
  1022. onPressed: (){Navigator.pop(context,false);},
  1023. )
  1024. ],
  1025. ),
  1026. ));
  1027. if(result){
  1028. util.showLoading(context);
  1029. var unpack = await util.JsonDataPutRaw({"userId":prefs.getString(keyClass.loginId),"cabangId":prefs.getString(keyClass.cabang_id),"company":prefs.getString(keyClass.company),"dbPath":prefs.getString(keyClass.targetProccess)}, '${prefs.getString(keyClass.hostAddress)}/stock_taking/add_collection/');
  1030. Navigator.pop(context);
  1031. util.showFlushbar(context, unpack['DATA'],color:(unpack['STATUS']!=1)?Colors.red:Colors.grey);
  1032. if(unpack['STATUS']==1){
  1033. prefs.remove(keyClass.targetProccess);
  1034. prefs.setBool(keyClass.submitProccess,true);
  1035. }
  1036. setState(() {
  1037. });
  1038. }
  1039. },
  1040. child: Container(
  1041. padding: EdgeInsets.all(5),
  1042. decoration: BoxDecoration(
  1043. boxShadow: [
  1044. BoxShadow(
  1045. color: Colors.grey.withOpacity(0.5),
  1046. spreadRadius: 2,
  1047. blurRadius: 2,
  1048. offset: Offset(0, 0), // changes position of shadow
  1049. ),
  1050. ],
  1051. borderRadius: BorderRadius.all(Radius.circular(5.0)),
  1052. color: Colors.orangeAccent.withAlpha(230).withOpacity(0.7),
  1053. ),
  1054. child: Column(
  1055. mainAxisAlignment: MainAxisAlignment.center,
  1056. children: <Widget>[
  1057. Icon(Icons.arrow_forward,size: MediaQuery.of(context).size.width/6,color: Colors.white,),
  1058. SizedBox(height: 5,),
  1059. Text('Process Units',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 16),)
  1060. ],
  1061. ),
  1062. ),
  1063. ):
  1064. InkWell(
  1065. onTap: ()async{
  1066. bool result = await showDialog(context: context,builder: (context)=>WillPopScope(
  1067. onWillPop: ()async{
  1068. Navigator.pop(context,false);
  1069. return false;
  1070. },
  1071. child: AlertDialog(
  1072. title: Text('Submit Data ?'),
  1073. content: Text('Submit the uploaded data.'),
  1074. actions: <Widget>[
  1075. TextButton(
  1076. child: Text('Submit'),
  1077. onPressed: ()async{
  1078. Navigator.pop(context,true);
  1079. },
  1080. ),
  1081. TextButton(
  1082. child: Text('Cancel'),
  1083. onPressed: (){Navigator.pop(context,false);},
  1084. )
  1085. ],
  1086. ),
  1087. ));
  1088. if(result){
  1089. util.showLoading(context);
  1090. if(prefs.getString(keyClass.stock_id) == null)
  1091. {
  1092. valueTab value = await DBHelper.database.getValue(keyClass.stock_id);
  1093. await prefs.setString(keyClass.stock_id, value.value);
  1094. }
  1095. var submits = await util.JsonDataPostRaw({"stockTakingId":prefs.getString(keyClass.stock_id),"company":prefs.getString(keyClass.company),"user_id":prefs.getString(keyClass.loginId)}, '${prefs.getString(keyClass.hostAddress)}/stock_taking/submit/');
  1096. Navigator.pop(context);
  1097. util.showFlushbar(context, submits['DATA'],color:(submits['STATUS']!=1)?Colors.red:Colors.grey);
  1098. if(submits['STATUS']==1){
  1099. loadState();
  1100. prefs.remove(keyClass.submitProccess);
  1101. }
  1102. setState(() {
  1103. });
  1104. }
  1105. },
  1106. child: Container(
  1107. padding: EdgeInsets.all(5),
  1108. decoration: BoxDecoration(
  1109. boxShadow: [
  1110. BoxShadow(
  1111. color: Colors.grey.withOpacity(0.5),
  1112. spreadRadius: 2,
  1113. blurRadius: 2,
  1114. offset: Offset(0, 0), // changes position of shadow
  1115. ),
  1116. ],
  1117. borderRadius: BorderRadius.all(Radius.circular(5.0)),
  1118. color: Colors.lightGreen.withAlpha(230),
  1119. ),
  1120. child: Column(
  1121. mainAxisAlignment: MainAxisAlignment.center,
  1122. children: <Widget>[
  1123. Icon(Icons.arrow_forward,size: MediaQuery.of(context).size.width/6,color: Colors.white,),
  1124. SizedBox(height: 5,),
  1125. Text('Submit Process',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 16),)
  1126. ],
  1127. ),
  1128. ),
  1129. ),
  1130. (prefs.getString(keyClass.targetProccess)!=null||(prefs.getBool(keyClass.submitProccess)!=null&&prefs.getBool(keyClass.submitProccess)))?InkWell(
  1131. splashColor: Colors.black,
  1132. onTap: ()async{
  1133. await Future.delayed(Duration(milliseconds: 200));
  1134. await clearData(context);
  1135. },
  1136. child: Container(
  1137. padding: EdgeInsets.all(5),
  1138. decoration: BoxDecoration(
  1139. boxShadow: [
  1140. BoxShadow(
  1141. color: Colors.grey.withOpacity(0.5),
  1142. spreadRadius: 2,
  1143. blurRadius: 2,
  1144. offset: Offset(0, 0), // changes position of shadow
  1145. ),
  1146. ],
  1147. borderRadius: BorderRadius.all(Radius.circular(5.0)),
  1148. color: Colors.red.withAlpha(200),
  1149. ),
  1150. child: Column(
  1151. mainAxisAlignment: MainAxisAlignment.center,
  1152. children: <Widget>[
  1153. Icon(Icons.restore_from_trash,size: MediaQuery.of(context).size.width/6,color: Colors.white,),
  1154. SizedBox(height: 5,),
  1155. Text('Clear Data',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 16),)
  1156. ],
  1157. ),
  1158. ),
  1159. ):Container(),
  1160. ],
  1161. )),
  1162. ),
  1163. ],
  1164. ),
  1165. bottomSheet: Container(height: 22,alignment: Alignment.bottomRight,child: Opacity(opacity: 0.5,child: Padding(padding: EdgeInsets.all(5),child: Text((lastDownload!='')?'Master Data Downloaded':'No Master Data Found',style: TextStyle(fontSize: 12),)))),
  1166. ),
  1167. );
  1168. }
  1169. }