flutter app untuk unitstock
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

1135 行
55 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:'logout',
  517. child: Row(
  518. children: <Widget>[
  519. Icon(Icons.exit_to_app,color: Colors.black,),
  520. SizedBox(width: 10,),
  521. Text('Logout')
  522. ],
  523. ),
  524. ),
  525. ];
  526. },
  527. onSelected: (value)async{
  528. if(value == 'hostChange'){
  529. hostAddress.text = prefs.getString(keyClass.hostAddress);
  530. await showDialog(context: context,builder: (context)=>
  531. Material(
  532. color: Colors.white.withOpacity(0.9),
  533. child: Center(
  534. child: Container(
  535. decoration: BoxDecoration(
  536. color: Colors.white,
  537. boxShadow: [
  538. BoxShadow(
  539. color: Colors.grey.withOpacity(0.5),
  540. spreadRadius: 2,
  541. blurRadius: 2,
  542. offset: Offset(0, 0), // changes position of shadow
  543. ),
  544. ],
  545. borderRadius: BorderRadius.circular(5)
  546. ),
  547. height:220,
  548. // height: MediaQuery.of(context).size.height/3.2,
  549. width: MediaQuery.of(context).size.width*0.75,
  550. child: Column(
  551. children: <Widget>[
  552. Flexible(
  553. flex:3,
  554. child: Container(
  555. padding: EdgeInsets.only(top:10,left: 10,right: 10),
  556. alignment: Alignment.center,
  557. decoration: BoxDecoration(
  558. // color: Colors.indigo,
  559. borderRadius: BorderRadius.only(topLeft: Radius.circular(5),topRight: Radius.circular(5))
  560. ),
  561. child: Column(
  562. mainAxisSize: MainAxisSize.min,
  563. crossAxisAlignment: CrossAxisAlignment.center,
  564. children: <Widget>[
  565. Icon(Icons.network_wifi,size: 70,color: Colors.indigo,),
  566. SizedBox(height: 5,),
  567. Text('Set Ip Address',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 16,color: Colors.indigo),)
  568. ],
  569. ),
  570. ),
  571. ),
  572. Flexible(
  573. flex: 1,
  574. child: Container(
  575. padding: EdgeInsets.only(left: 20,right: 20),
  576. alignment: Alignment.centerLeft,
  577. child: Theme(
  578. data: ThemeData(
  579. canvasColor: Colors.white,
  580. primaryColor: Colors.indigo,
  581. accentColor: Colors.indigo,
  582. hintColor: Colors.indigo
  583. ),
  584. child: TextField(
  585. controller: hostAddress,
  586. decoration: InputDecoration(
  587. contentPadding: EdgeInsets.all(8),
  588. ),
  589. onSubmitted: (value){
  590. prefs.setString(keyClass.hostAddress,(value=='')?'https://tbg.thamringroup.web.id/ords/tbs/unit/v1':value);
  591. Navigator.pop(context);
  592. },
  593. ),
  594. ),
  595. ),
  596. ),
  597. Flexible(
  598. flex: 1,
  599. child: Container(
  600. child: Row(
  601. mainAxisAlignment: MainAxisAlignment.end,
  602. children: <Widget>[
  603. TextButton(
  604. child: Text('OK',style: TextStyle(color: Colors.indigo),),
  605. onPressed: (){
  606. prefs.setString(keyClass.hostAddress,(hostAddress.text=='')?'https://tbg.thamringroup.web.id/ords/tbs/unit/v1':hostAddress.text);
  607. Navigator.pop(context);
  608. },
  609. )
  610. ],
  611. ),
  612. ),
  613. )
  614. ],
  615. ),
  616. ),
  617. ),
  618. )
  619. // AlertDialog(
  620. // title: Text('Set IP Address'),
  621. // content: TextField(
  622. // controller: hostAddress,
  623. // onSubmitted: (value){
  624. // prefs.setString(keyClass.hostAddress,(value=='')?'https://unitstocksbackend.thamringroup.web.id':value);
  625. // Navigator.pop(context);
  626. // },
  627. // ),
  628. // actions: <Widget>[
  629. // TextButton(
  630. // child: Text('OK'),
  631. // onPressed: (){
  632. // prefs.setString(keyClass.hostAddress,(hostAddress.text=='')?'https://unitstocksbackend.thamringroup.web.id':hostAddress.text);
  633. // Navigator.pop(context);
  634. // },
  635. // )
  636. // ],
  637. // )
  638. );
  639. }
  640. if(value=="cabangChange") {
  641. var res = await changeCabang(context);
  642. if(res??false){
  643. setState(() {
  644. });
  645. }
  646. }
  647. if(value == 'logout'){
  648. prefs.remove(keyClass.cabang_id);
  649. prefs.remove(keyClass.company);
  650. prefs.remove(keyClass.loginId);
  651. prefs.setBool(keyClass.logged_in, false);
  652. Navigator.pushNamed(context, '/login');
  653. }
  654. },
  655. ),
  656. ],
  657. ),
  658. ),
  659. (prefs.getString(keyClass.stock_id)==null)?Container():Padding(
  660. padding: const EdgeInsets.only(top: 8,right: 8),
  661. child: Container(
  662. decoration: BoxDecoration(
  663. color: Colors.indigo.withOpacity(0.9),
  664. borderRadius: BorderRadius.circular(8)
  665. // border: Border.all(),
  666. ),
  667. padding: EdgeInsets.all(8),
  668. child: Row(
  669. mainAxisSize: MainAxisSize.min,
  670. mainAxisAlignment: MainAxisAlignment.end,
  671. crossAxisAlignment: CrossAxisAlignment.center,
  672. children: <Widget>[
  673. Text('Status : ',style: TextStyle(color: Colors.white),),
  674. Text(state??'-',style: TextStyle(fontWeight: FontWeight.bold,color: Colors.white),),
  675. ],
  676. ),
  677. ),
  678. ),
  679. Expanded(
  680. child: Container(
  681. child:
  682. GridView.count(
  683. padding: EdgeInsets.all(10),
  684. crossAxisSpacing: 10,
  685. mainAxisSpacing: 10,
  686. crossAxisCount: 2,
  687. children: <Widget>[
  688. AbsorbPointer(
  689. absorbing: isLoading,
  690. child: InkWell(
  691. splashColor: Colors.black,
  692. onTap: ()async{
  693. bool getData = await showDialog(
  694. context: context,
  695. builder: (context)=>AlertDialog(
  696. title: Text("Get Data Units?"),
  697. content: Text('Fetch data unit for stocking'),
  698. actions: <Widget>[
  699. TextButton(
  700. child: Text('Proceed'),
  701. onPressed: (){
  702. Navigator.pop(context,true);
  703. },
  704. ),
  705. TextButton(
  706. child: Text('Cancel'),
  707. onPressed: (){
  708. Navigator.pop(context,false);
  709. },
  710. )
  711. ],
  712. )
  713. );
  714. if(getData??false){
  715. setState(() {
  716. isLoading = true;
  717. });
  718. var a = Stopwatch();
  719. a.start();
  720. await Future.delayed(Duration(milliseconds: 200));
  721. bool isclear;
  722. Directory documentsDirectory = await getExternalStorageDirectory();
  723. String path = join(documentsDirectory.path, "UnitStocking.db");
  724. File db = File(path);
  725. if(db.existsSync()){
  726. isclear = await clearData(context);
  727. }
  728. else{
  729. isclear = true;
  730. }
  731. if(isclear??false){
  732. print('fetching');
  733. util.showLoading(context);
  734. file_Trans_Handler trans = new file_Trans_Handler();
  735. // trans.downloadFile('UnitStocking.db',"http://172.16.1.8:14002/getSqliteDbtest/TBS/02");
  736. trans.downloadFile('UnitStocking.db',"${prefs.getString(keyClass.hostAddress)}/stock_taking/get_units/${prefs.getString(keyClass.company)}/${prefs.getString(keyClass.cabang_id)}");
  737. progressDLStream = trans.progress.listen((value)async {
  738. if(progressDL==null) {
  739. // print(['test',value]);
  740. if(value!=null) Navigator.pop(context);
  741. }
  742. setState(() {
  743. progressDL = (value!=-1.0)?value:null;
  744. });
  745. if(value!=null&&value >= 1.0) {
  746. progressDLStream.cancel();
  747. progressDL = null;
  748. setState(() {
  749. lastDownload = DateTime.now().toIso8601String();
  750. });
  751. await prefs.setString(keyClass.lastDownload, lastDownload);
  752. await DBHelper.database.insertUpdateValue(new valueTab(name: 'TGL_START',value: DateFormat('dd-MM-yyyy HH:mm:ss').format(DateTime.parse(lastDownload))));
  753. await DBHelper.database.insertUpdateValue(new valueTab(name: 'TGL_STOCK_TAKING',value: DateFormat('dd-MM-yyyy').format(DateTime.parse(lastDownload))));
  754. await DBHelper.database.closeDb();
  755. util.showFlushbar(context,'Data downloaded');
  756. loadState();
  757. a.stop();
  758. if(a.elapsed.inSeconds<3){
  759. await Future.delayed(Duration(seconds: 3-a.elapsed.inSeconds));
  760. }
  761. setState(() {
  762. isLoading = false;
  763. });
  764. }
  765. if(value==-1.0){
  766. setState(() {
  767. isLoading = false;
  768. });
  769. util.showFlushbar(context,trans.error??'Data download error',color: Colors.red);
  770. }
  771. });
  772. // Navigator.popUntil(context, ModalRoute.withName('/home'));
  773. }
  774. }
  775. },
  776. child: Container(
  777. padding: EdgeInsets.all(5),
  778. decoration: BoxDecoration(
  779. boxShadow: [
  780. BoxShadow(
  781. color: Colors.grey.withOpacity(0.5),
  782. spreadRadius: 2,
  783. blurRadius: 2,
  784. offset: Offset(0, 0), // changes position of shadow
  785. ),
  786. ],
  787. borderRadius: BorderRadius.all(Radius.circular(5.0)),
  788. color: Colors.green.withAlpha(220).withOpacity(0.7),
  789. ),
  790. child: Stack(
  791. children: <Widget>[
  792. Container(
  793. alignment: (progressDL!=null)?Alignment.bottomCenter:Alignment.bottomLeft,
  794. padding: EdgeInsets.only(left: 10,right: 10,top: 10,bottom: 15),
  795. child: (progressDL!=null)?Column(
  796. mainAxisSize: MainAxisSize.min,
  797. children: <Widget>[
  798. LinearProgressIndicator(
  799. value: progressDL,
  800. ),
  801. Text('${(progressDL*100).floor()}%',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold),)
  802. ],
  803. ):(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)),
  804. ),
  805. Positioned.fill(
  806. child: Column(
  807. mainAxisAlignment: MainAxisAlignment.center,
  808. children: <Widget>[
  809. Icon(Icons.file_upload,size: MediaQuery.of(context).size.width/6,color: Colors.white,),
  810. SizedBox(height: 5,),
  811. Text('Get Data Master',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 16),),
  812. ],
  813. ),
  814. ),
  815. ],
  816. ),
  817. ),
  818. ),
  819. ),
  820. InkWell(
  821. splashColor: Colors.black,
  822. onTap: ()async{
  823. await Future.delayed(Duration(milliseconds: 200));
  824. if(lastDownload!=''){
  825. Navigator.pushNamed(context, '/stocking');
  826. }
  827. else{
  828. util.showFlushbar(context, 'Data Master tidak ditemukan. Get data master dulu.');
  829. }
  830. },
  831. child: Container(
  832. padding: EdgeInsets.all(5),
  833. decoration: BoxDecoration(
  834. boxShadow: [
  835. BoxShadow(
  836. color: Colors.grey.withOpacity(0.5),
  837. spreadRadius: 2,
  838. blurRadius: 2,
  839. offset: Offset(0, 0), // changes position of shadow
  840. ),
  841. ],
  842. borderRadius: BorderRadius.all(Radius.circular(5.0)),
  843. color: Colors.blueGrey.withAlpha(230).withOpacity(0.8),
  844. ),
  845. child: Column(
  846. mainAxisAlignment: MainAxisAlignment.center,
  847. children: <Widget>[
  848. Icon(Icons.receipt,size: MediaQuery.of(context).size.width/6,color: Colors.white,),
  849. SizedBox(height: 5,),
  850. Text('Stocking',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 16),)
  851. ],
  852. ),
  853. ),
  854. ),
  855. InkWell(
  856. splashColor: Colors.black,
  857. onTap: ()async{
  858. if(lastDownload!=''){
  859. await DBHelper.database.insertUpdateValue(new valueTab(name: 'TGL_SELESAI',value: DateFormat('dd-MM-yyyy HH:mm:ss').format(new DateTime.now())));
  860. await DBHelper.database.closeDb();
  861. file_Trans_Handler trans = new file_Trans_Handler();
  862. bool popped = false;
  863. util.showLoading(context,onwillpop:()async{
  864. await trans.cancel();
  865. popped = true;
  866. return true;
  867. });
  868. var upload = await trans.uploadFile('UnitStocking.db',"${prefs.getString(keyClass.hostAddress)}/stock_taking/upload/",prefs.getString(keyClass.company),prefs.getString(keyClass.cabang_id));
  869. if(!popped){
  870. Navigator.pop(context);
  871. util.showFlushbar(context, upload['DATA'],color:(upload['STATUS']!=1)?Colors.red:Colors.grey);
  872. if(upload['STATUS']==1){
  873. setState(() {
  874. lastUpload = new DateTime.now().toIso8601String();
  875. });
  876. prefs.setString(keyClass.lastUpload, lastUpload);
  877. prefs.setString(keyClass.targetProccess, upload[keyClass.targetProccess]);
  878. }
  879. }
  880. }
  881. else{
  882. util.showFlushbar(context, "Belum ada data yang disimpan.",color: Colors.red);
  883. }
  884. },
  885. child: Container(
  886. padding: EdgeInsets.all(5),
  887. decoration: BoxDecoration(
  888. boxShadow: [
  889. BoxShadow(
  890. color: Colors.grey.withOpacity(0.5),
  891. spreadRadius: 2,
  892. blurRadius: 2,
  893. offset: Offset(0, 0), // changes position of shadow
  894. ),
  895. ],
  896. borderRadius: BorderRadius.all(Radius.circular(5.0)),
  897. color: Colors.indigo.withAlpha(220).withOpacity(0.7),
  898. ),
  899. child: Stack(
  900. children: <Widget>[
  901. Container(
  902. alignment: (progressUL!=null)?Alignment.bottomCenter:Alignment.bottomLeft,
  903. padding: EdgeInsets.only(left: 10,right: 10,top: 10,bottom: 0),
  904. child: (progressUL!=null)?Column(
  905. mainAxisSize: MainAxisSize.min,
  906. children: <Widget>[
  907. LinearProgressIndicator(
  908. value: progressUL,
  909. ),
  910. Text('${(progressUL*100).floor()}%',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold),)
  911. ],
  912. ):(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)),
  913. ),
  914. Positioned.fill(
  915. child: Column(
  916. mainAxisAlignment: MainAxisAlignment.center,
  917. children: <Widget>[
  918. Icon(Icons.file_upload,size: MediaQuery.of(context).size.width/6,color: Colors.white,),
  919. SizedBox(height: 5,),
  920. Text('Send Data Stokan',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 16),),
  921. ],
  922. ),
  923. ),
  924. ],
  925. ),
  926. ),
  927. ),
  928. (prefs.getString(keyClass.targetProccess)==null&&prefs.getBool(keyClass.submitProccess)==null)?InkWell(
  929. splashColor: Colors.black,
  930. onTap: ()async{
  931. await Future.delayed(Duration(milliseconds: 200));
  932. await clearData(context);
  933. },
  934. child: Container(
  935. padding: EdgeInsets.all(5),
  936. decoration: BoxDecoration(
  937. boxShadow: [
  938. BoxShadow(
  939. color: Colors.grey.withOpacity(0.5),
  940. spreadRadius: 2,
  941. blurRadius: 2,
  942. offset: Offset(0, 0), // changes position of shadow
  943. ),
  944. ],
  945. borderRadius: BorderRadius.all(Radius.circular(5.0)),
  946. color: Colors.red.withAlpha(200).withOpacity(0.7),
  947. ),
  948. child: Column(
  949. mainAxisAlignment: MainAxisAlignment.center,
  950. children: <Widget>[
  951. Icon(Icons.restore_from_trash,size: MediaQuery.of(context).size.width/6,color: Colors.white,),
  952. SizedBox(height: 5,),
  953. Text('Clear Data',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 16),)
  954. ],
  955. ),
  956. ),
  957. ):
  958. (prefs.getString(keyClass.targetProccess)!=null)?InkWell(
  959. onTap: ()async{
  960. bool result = await showDialog(context: context,builder: (context)=>WillPopScope(
  961. onWillPop: ()async{
  962. Navigator.pop(context,false);
  963. return false;
  964. },
  965. child: AlertDialog(
  966. title: Text('Process Data ?'),
  967. content: Text('Proceed to unpack the uploaded data.'),
  968. actions: <Widget>[
  969. TextButton(
  970. child: Text('Proceed'),
  971. onPressed: ()async{
  972. Navigator.pop(context,true);
  973. },
  974. ),
  975. TextButton(
  976. child: Text('Cancel'),
  977. onPressed: (){Navigator.pop(context,false);},
  978. )
  979. ],
  980. ),
  981. ));
  982. if(result){
  983. util.showLoading(context);
  984. 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/');
  985. Navigator.pop(context);
  986. util.showFlushbar(context, unpack['DATA'],color:(unpack['STATUS']!=1)?Colors.red:Colors.grey);
  987. if(unpack['STATUS']==1){
  988. prefs.remove(keyClass.targetProccess);
  989. prefs.setBool(keyClass.submitProccess,true);
  990. }
  991. setState(() {
  992. });
  993. }
  994. },
  995. child: Container(
  996. padding: EdgeInsets.all(5),
  997. decoration: BoxDecoration(
  998. boxShadow: [
  999. BoxShadow(
  1000. color: Colors.grey.withOpacity(0.5),
  1001. spreadRadius: 2,
  1002. blurRadius: 2,
  1003. offset: Offset(0, 0), // changes position of shadow
  1004. ),
  1005. ],
  1006. borderRadius: BorderRadius.all(Radius.circular(5.0)),
  1007. color: Colors.orangeAccent.withAlpha(230).withOpacity(0.7),
  1008. ),
  1009. child: Column(
  1010. mainAxisAlignment: MainAxisAlignment.center,
  1011. children: <Widget>[
  1012. Icon(Icons.arrow_forward,size: MediaQuery.of(context).size.width/6,color: Colors.white,),
  1013. SizedBox(height: 5,),
  1014. Text('Process Units',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 16),)
  1015. ],
  1016. ),
  1017. ),
  1018. ):
  1019. InkWell(
  1020. onTap: ()async{
  1021. bool result = await showDialog(context: context,builder: (context)=>WillPopScope(
  1022. onWillPop: ()async{
  1023. Navigator.pop(context,false);
  1024. return false;
  1025. },
  1026. child: AlertDialog(
  1027. title: Text('Submit Data ?'),
  1028. content: Text('Submit the uploaded data.'),
  1029. actions: <Widget>[
  1030. TextButton(
  1031. child: Text('Submit'),
  1032. onPressed: ()async{
  1033. Navigator.pop(context,true);
  1034. },
  1035. ),
  1036. TextButton(
  1037. child: Text('Cancel'),
  1038. onPressed: (){Navigator.pop(context,false);},
  1039. )
  1040. ],
  1041. ),
  1042. ));
  1043. if(result){
  1044. util.showLoading(context);
  1045. if(prefs.getString(keyClass.stock_id) == null)
  1046. {
  1047. valueTab value = await DBHelper.database.getValue(keyClass.stock_id);
  1048. await prefs.setString(keyClass.stock_id, value.value);
  1049. }
  1050. 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/');
  1051. Navigator.pop(context);
  1052. util.showFlushbar(context, submits['DATA'],color:(submits['STATUS']!=1)?Colors.red:Colors.grey);
  1053. if(submits['STATUS']==1){
  1054. loadState();
  1055. prefs.remove(keyClass.submitProccess);
  1056. }
  1057. setState(() {
  1058. });
  1059. }
  1060. },
  1061. child: Container(
  1062. padding: EdgeInsets.all(5),
  1063. decoration: BoxDecoration(
  1064. boxShadow: [
  1065. BoxShadow(
  1066. color: Colors.grey.withOpacity(0.5),
  1067. spreadRadius: 2,
  1068. blurRadius: 2,
  1069. offset: Offset(0, 0), // changes position of shadow
  1070. ),
  1071. ],
  1072. borderRadius: BorderRadius.all(Radius.circular(5.0)),
  1073. color: Colors.lightGreen.withAlpha(230),
  1074. ),
  1075. child: Column(
  1076. mainAxisAlignment: MainAxisAlignment.center,
  1077. children: <Widget>[
  1078. Icon(Icons.arrow_forward,size: MediaQuery.of(context).size.width/6,color: Colors.white,),
  1079. SizedBox(height: 5,),
  1080. Text('Submit Process',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 16),)
  1081. ],
  1082. ),
  1083. ),
  1084. ),
  1085. (prefs.getString(keyClass.targetProccess)!=null||(prefs.getBool(keyClass.submitProccess)!=null&&prefs.getBool(keyClass.submitProccess)))?InkWell(
  1086. splashColor: Colors.black,
  1087. onTap: ()async{
  1088. await Future.delayed(Duration(milliseconds: 200));
  1089. await clearData(context);
  1090. },
  1091. child: Container(
  1092. padding: EdgeInsets.all(5),
  1093. decoration: BoxDecoration(
  1094. boxShadow: [
  1095. BoxShadow(
  1096. color: Colors.grey.withOpacity(0.5),
  1097. spreadRadius: 2,
  1098. blurRadius: 2,
  1099. offset: Offset(0, 0), // changes position of shadow
  1100. ),
  1101. ],
  1102. borderRadius: BorderRadius.all(Radius.circular(5.0)),
  1103. color: Colors.red.withAlpha(200),
  1104. ),
  1105. child: Column(
  1106. mainAxisAlignment: MainAxisAlignment.center,
  1107. children: <Widget>[
  1108. Icon(Icons.restore_from_trash,size: MediaQuery.of(context).size.width/6,color: Colors.white,),
  1109. SizedBox(height: 5,),
  1110. Text('Clear Data',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 16),)
  1111. ],
  1112. ),
  1113. ),
  1114. ):Container(),
  1115. ],
  1116. )),
  1117. ),
  1118. ],
  1119. ),
  1120. 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),)))),
  1121. ),
  1122. );
  1123. }
  1124. }