import 'dart:io'; import 'util/photo_viewer.dart'; import 'package:assetstock/util/dbHandler.dart'; import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; import 'main.dart'; import 'util/Models.dart'; import 'package:http/http.dart' as http; import 'util/prefsKey.dart'; class AssetDetails extends StatefulWidget { // AssetDetails({Key key}) : super(key: key); int no; Listlokasi; String initTag; AssetDetails({this.no,this.lokasi,this.initTag}); @override _AssetDetailsState createState() => _AssetDetailsState(); } class _AssetDetailsState extends State with TickerProviderStateMixin { final barCode = new TextEditingController(); final prefixCode = new TextEditingController(); final qty = new TextEditingController(); final ket = new TextEditingController(); TabController _controller; int _currentIndex; Asset _asset = new Asset(); final focusNode = new FocusNode(); bool isAppend = false; loadAsset()async{ var blob = await DBHelper.database.getBlobbyNo(widget.no); if(blob!=null){ var result = await DBHelper.database.searchbyTagNumber(blob.tag_number.toUpperCase()); if(result['DATA']!=null) { result['DATA'].blob = blob.blob; result['DATA'].no = widget.no; result['DATA'].keterangan = blob.keterangan; } else util.showToast('Alert', 'Asset data not found'); String tag_number = result['DATA'].tag_number; int separator = tag_number.lastIndexOf('/')+1; setState(() { _asset = result['DATA']; prefixCode.text = tag_number.substring(0,separator); barCode.text = tag_number.substring(separator); ket.text = result['DATA'].keterangan; }); } else{ util.showToast('Alert', 'Asset data not found'); } } redirectNonAudit()async { var blob = await DBHelper.database.getBlobbyTag(widget.initTag); print("checkExist $blob"); if(blob!=null){ widget.no = blob.no; await loadAsset(); // var blob = await DBHelper.database.getBlobbyTag(widget.initTag); // var result = await DBHelper.database.searchbyTagNumber(blob.tag_number.toUpperCase()); // if(result['DATA']!=null) { // result['DATA'].blob = blob.blob; // result['DATA'].no = blob.no; // result['DATA'].keterangan = blob.keterangan; // } // _asset = result['DATA']; // ket.text = blob.keterangan; } else{ setState(() { prefixCode.text = widget.initTag.substring(0,6); barCode.text = widget.initTag.substring(6); }); await findAsset(prefixCode.text+barCode.text); } } @override void initState() { // TODO: implement initState super.initState(); qty.text='1'; _controller = new TabController(length: 1, vsync: this); _controller.addListener(() { setState(() { _currentIndex = _controller.index; }); }); if(widget.no!=null){ loadAsset(); } if(widget.initTag!=null){ redirectNonAudit(); } } findAsset(query,{silent=false})async{ var result = await DBHelper.database.searchbyTagNumber(query.toUpperCase()); if(result['DATA']!=null){ if(result['EXIST']){ if(widget.no==null){ setState(() { isAppend=false; barCode.text = ''; ket.text = ''; _asset = new Asset(); }); FocusScope.of(context).requestFocus(focusNode); } else{ String tag_number = _asset.tag_number; int separator = tag_number.lastIndexOf('/')+1; setState(() { prefixCode.text = tag_number.substring(0,separator); barCode.text = tag_number.substring(separator); }); } if(!silent)util.showToast('Alert', 'Asset data already inserted'); } else{ var image; if(_asset.blob!=null)image=_asset.blob; setState(() { isAppend=false; _asset = result['DATA']; if(image!=null)_asset.blob = image; _asset.no = widget.no; }); } } else{ var image; if(_asset.blob!=null)image=_asset.blob; setState(() { // barCode.text = ''; isAppend=true; // ket.text = ''; _asset = new Asset(); if(image!=null)_asset.blob = image; _asset.tag_number = query.toUpperCase(); _asset.no = widget.no; }); // FocusScope.of(context).requestFocus(focusNode); if(!silent)util.showToast('Alert', 'Asset data not found'); } } @override Widget build(BuildContext context) { return Scaffold( resizeToAvoidBottomInset: true, body: Container( padding: EdgeInsets.only(top: 20,bottom: 50), color: Colors.grey.withOpacity(0.3), child: Column( children: [ Expanded( flex: 5, child: Column( children: [ Expanded( flex:18, child: TabBarView( controller: _controller, children: [ Padding( padding: EdgeInsets.only(top:20,left: 20,right: 20), child: Stack( children: [ Container(color: Colors.grey,), Positioned.fill(child: InkWell(onTap: ()async{ var image = await picker.getImage(source: ImageSource.camera,imageQuality: 60,maxWidth: 600); if(image!=null){ _asset.blob = await image.readAsBytes(); setState(() { }); print(File(image.path).lengthSync()/1024); File(image.path).deleteSync(); } },child: Container( decoration: BoxDecoration( image: (_asset!=null&&_asset.blob!=null)?DecorationImage( image: MemoryImage(_asset.blob), fit: BoxFit.fitWidth ):null ), child: (_asset!=null&&_asset.blob!=null)?null:Icon(Icons.camera_alt,size: 200,color: Colors.white,)))) ], ), ), // Padding( // padding: EdgeInsets.only(top:20,left: 20,right: 20), // child: Stack( // children: [ // Container(color: Colors.grey,), // Positioned.fill(child: InkWell(onTap: ()async{ // await picker.getImage(source: ImageSource.camera); // },child: Container(child: Icon(Icons.camera_alt,size: 200,color: Colors.white,)))) // ], // ), // ), ], ), ), Expanded( flex:1, child: Container( // color: Colors.grey, // child: TabBar( // indicatorColor: Colors.blueGrey, // controller: _controller, // labelColor: Colors.blueGrey, // labelStyle: TextStyle( // fontSize: 17, // fontWeight: FontWeight.bold // ), // unselectedLabelStyle: TextStyle( // fontSize: 16 // ), // indicatorSize: TabBarIndicatorSize.label, // tabs: [ // Tab( // text: 'Asset', // ), // Tab( // text: 'Barcode', // ), // ], // ), ) ), ], ), ), Expanded( flex: 5, child: Container( color: Colors.white, padding: EdgeInsets.all(20), child: SingleChildScrollView( scrollDirection: Axis.vertical, child: Column( mainAxisAlignment: MainAxisAlignment.start, children: [ (_asset.tag_number!=null)?Container( alignment: Alignment.centerRight, child: InkWell( onTap: ()async { try{ var dbName = await DBHelper.database.getDbName(); var bUnit = await DBHelper.database.getBUnit(); util.showLoading(context); http.Response result = await http.get( Uri.parse('${prefs.getString(keyClass.hostAddress)??"https://asset.thamringroup.web.id"}/api/images?q=${_asset.tag_number}&s=${dbName}&b=${bUnit}'), ).timeout(Duration(seconds: 3)); Navigator.pop(context); if(result!=null){ Navigator.push(context, MaterialPageRoute(builder: (context) => PhotoViewer(result.bodyBytes))); } else util.showToast('ALERT', "No Archived Images yet"); } catch(e){ Navigator.pop(context); print(e); util.showToast('ALERT', "No Archived Images yet"); } //showPhoto }, child: Padding( padding: const EdgeInsets.all(4.0), child: Text('Archived images',style: TextStyle(color: Colors.indigo, decoration: TextDecoration.underline,),), ), ), ):Container(), (_asset.tag_number!=null)?SizedBox(height: 5,):Container(), Row( children: [ Expanded( flex:3, child: Container( child: TextField( controller: prefixCode, autofocus: (widget.no==null), onSubmitted: (value)async{ String query = value.replaceAll('*', ''); query = query.substring(0,query.lastIndexOf('/')+1); setState(() { prefixCode.text = query; }); if(prefixCode.text!=''&&barCode.text!='')await findAsset(prefixCode.text+barCode.text); else FocusScope.of(context).requestFocus(focusNode); }, decoration: InputDecoration( hintText: "Prefix code", alignLabelWithHint: true, ), ), ), ), SizedBox(width: 10,), Expanded( flex:3, child: Container( child: TextField( controller: barCode, focusNode: focusNode, autofocus: (widget.no==null), onSubmitted: (value)async{ String query = value.replaceAll('*', ''); if(prefixCode.text!=''){ query = query.replaceAll(prefixCode.text.toUpperCase(), ''); } else{ if(query.contains('/'))query = query.substring(query.lastIndexOf('/')); util.showToast('ALERT', 'Please fill the prefix first'); } setState(() { barCode.text = query; }); if(prefixCode.text!=''&&barCode.text!='')await findAsset(prefixCode.text+barCode.text); }, decoration: InputDecoration( hintText: "Suffix / Barcode", alignLabelWithHint: true, suffixIcon: InkWell(onTap: ()async{ if(prefixCode.text!=''){ var scan = await util.scan(); if(scan['STATUS']==1){ String query = scan['DATA']; // print("$query != ${prefixCode.text.toUpperCase()}"); setState(() { query = query.trim().replaceAll(prefixCode.text.toUpperCase(), ''); barCode.text = query; }); if(prefixCode.text!=''&&barCode.text!='')await findAsset(prefixCode.text+barCode.text); } } else util.showToast('ALERT', 'Please fill the prefix first'); },child: Icon(Icons.center_focus_strong)) ), ), ), ), Expanded(flex: 1, child: Container( alignment: Alignment.centerRight, child: Text('Qty :'), ),), Expanded( flex:2, child: Container( child: TextField( enabled: false, keyboardType: TextInputType.number, decoration: InputDecoration( contentPadding: EdgeInsets.all(8) ), controller: qty, ), ), ), ], ), SizedBox(height: 5,), Container( alignment: Alignment.centerLeft, height: 50, child: Row( children: [ Expanded( flex : 3, child: Text('Note ',style: TextStyle(fontSize: 16),), ), Expanded( flex : 1, child: Text(':',style: TextStyle(fontSize: 16),), ), Expanded( flex : 7, child: TextField( controller: ket, decoration: InputDecoration( hintText: "Insert Note here", ), ), ), ], ), ), SizedBox(height: 5,), Container( alignment: Alignment.centerLeft, height: 50, child: Row( children: [ Expanded( flex : 3, child: Text('Asset No ',style: TextStyle(fontSize: 16),), ), Expanded( flex : 1, child: Text(':',style: TextStyle(fontSize: 16),), ), Expanded( flex : 7, child: Text((_asset.asset_no??'-')=='null'?'-':_asset.asset_no??'-',style: TextStyle(fontSize: 16),), ), ], ), ), SizedBox(height: 5,), Container( alignment: Alignment.centerLeft, height: 50, child: Row( children: [ Expanded( flex : 3, child: Text('Asset Desc ',style: TextStyle(fontSize: 16),), ), Expanded( flex : 1, child: Text(':',style: TextStyle(fontSize: 16),), ), Expanded( flex : 7, child: Text(_asset.asset_desc??'-',style: TextStyle(fontSize: 16),), ), ], ), ), SizedBox(height: 5,), Container( alignment: Alignment.centerLeft, height: 50, child: Row( children: [ Expanded( flex : 3, child: Text('PIC ',style: TextStyle(fontSize: 16),), ), Expanded( flex : 1, child: Text(':',style: TextStyle(fontSize: 16),), ), Expanded( flex : 7, child: Text(_asset.pic??'-',style: TextStyle(fontSize: 16),), ), ], ), ), SizedBox(height: 5,), Container( alignment: Alignment.centerLeft, height: 50, child: Row( children: [ Expanded( flex : 3, child: Text('Lantai ',style: TextStyle(fontSize: 16),), ), Expanded( flex : 1, child: Text(':',style: TextStyle(fontSize: 16),), ), Expanded( flex : 7, child: Text(_asset.lantai??'-',style: TextStyle(fontSize: 16),), ), ], ), ), SizedBox(height: 5,), Container( alignment: Alignment.centerLeft, height: 50, child: Row( children: [ Expanded( flex : 3, child: Text('Ruang ',style: TextStyle(fontSize: 16),), ), Expanded( flex : 1, child: Text(':',style: TextStyle(fontSize: 16),), ), Expanded( flex : 7, child: Text(_asset.ruangan??'-',style: TextStyle(fontSize: 16),), ), ], ), ), // SizedBox(height: 5,), // Container( // alignment: Alignment.centerLeft, // height: 50, // child: Row( // children: [ // Expanded( // flex : 3, // child: Text('Gedung ',style: TextStyle(fontSize: 16),), // ), // Expanded( // flex : 1, // child: Text(':',style: TextStyle(fontSize: 16),), // ), // Expanded( // flex : 7, // child: Text(_asset.gedung??'-',style: TextStyle(fontSize: 16),), // ), // ], // ), // ), ], ), ), ), ), ], ), ), bottomSheet: Container( height: 50, // color: Colors.grey.withOpacity(0.3), child: Row( children: [ Expanded( flex: 5, child: TextButton( onPressed: ()async{ if(barCode.text!=''&&prefixCode.text!='')await findAsset(prefixCode.text+barCode.text,silent: true); else _asset.tag_number = null; if(_asset.tag_number!=null&&_asset.blob!=null){ _asset.keterangan = ket.text; bool upsertReady = !isAppend; if(isAppend){ upsertReady = await showDialog(context: context,builder: (context)=>WillPopScope( onWillPop: ()async{ Navigator.pop(context,false); return false; }, child: AlertDialog( title: Text('Append New Asset'), content: Text('${(widget.no==null)?'Insert':'Update'} brand new asset?'), actions: [ TextButton( onPressed: ()async{ Navigator.pop(context,true); }, child: Text('Save'), ), TextButton( onPressed: ()async{ Navigator.pop(context,false); }, child: Text('Cancel'), ) ], ), )); } if(upsertReady){ if(widget.no==null){ var insert = await DBHelper.database.insertAsset(_asset); if(insert!=null){ util.showToast("SUCCESS", 'Asset Inserted'); await DBHelper.database.closeDb(); if(widget.initTag!=null)Navigator.pop(context); setState(() { barCode.text = ''; ket.text = ''; _asset = new Asset(); }); FocusScope.of(context).requestFocus(focusNode); } } else{ var update = await DBHelper.database.updateAsset(_asset); if(update!=null){ util.showToast("SUCCESS", 'Asset Updated'); await DBHelper.database.closeDb(); Navigator.pop(context); } } } } else{ util.showToast('ERROR', 'Please complete the asset data'); } }, style: TextButton.styleFrom( backgroundColor: Colors.green, ), child: Container(alignment: Alignment.center,height: 50, child: Text('Save',style: TextStyle(color: Colors.white),)), ), ), Expanded( flex: 5, child: TextButton( style: TextButton.styleFrom( backgroundColor: Colors.red, ), onPressed: (){ Navigator.pop(context); }, child: Container(alignment: Alignment.center,height:50,child: Text('Cancel',style: TextStyle(color: Colors.white))), ), ), ], ), ), ); } }