import 'dart:typed_data'; import 'Util/UnitModel.dart'; import 'Util/photo_viewer.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; import 'Util/DBHelper.dart'; import 'Util/Util.dart'; import 'main.dart'; import 'package:intl/intl.dart'; class UnitDetails extends StatefulWidget { Unit unit; UnitDetails({this.unit}); @override _UnitDetailsState createState() => _UnitDetailsState(); } class _UnitDetailsState extends State { final tipeController = TextEditingController(); final rangkaController = TextEditingController(); final colorController = TextEditingController(); final kodeController = TextEditingController(); final mesinController = TextEditingController(); final tahunController = TextEditingController(); final stateController = TextEditingController(); final channelController = TextEditingController(); // insertUnit _unitDetails; bool isChanged = false; Util util = new Util(); List images = []; List blobIds = []; List deleteBlobIds = []; List dropMenu = []; List jenisImages = []; int jenisLength=0; Unit _currentUnit; List blobList = []; // List allUnits = []; loadUnitDetails(context)async{ _currentUnit = widget.unit; tipeController.text= _currentUnit.tipe; rangkaController.text = _currentUnit.rangka; colorController.text = _currentUnit.warna; kodeController.text = _currentUnit.kode; mesinController.text = _currentUnit.mesin; tahunController.text = _currentUnit.tahun; stateController.text = _currentUnit.state; channelController.text = (_currentUnit.channel==null||_currentUnit.channel==""||_currentUnit.channel=="null")?'-':_currentUnit.channel; await loadBlob(); } // loadTipe() async{ // allUnits.clear(); // allUnits = await DBHelper.database.getAllUnits(); // } loadBlob()async{ var result = await DBHelper.database.getBlobUnits(mesinController.text); if(result!=null){ setState(() { blobList = result; }); // for(int i =0; i element.value==jenisImages[i]); // } // } } } @override void initState() { // TODO: implement initState super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) async { util.showLoading(context); await Future.sync(()async{await loadUnitDetails(context);}); Navigator.pop(context); }); } @override Widget build(BuildContext context) { // if(allUnits.length!=0){ // print(allUnits[0].tipe); //// loadTipe(); // } return Scaffold( resizeToAvoidBottomInset: true, appBar: AppBar( backgroundColor: Colors.transparent, elevation: 0.0, iconTheme: IconThemeData( color: Colors.indigo, ), title: Text((widget.unit.flag=='FALSE')?'Inserting New Unit':'Editing Unit',style: TextStyle(color: Colors.indigo),), ), body: WillPopScope( onWillPop: ()async{ if(widget.unit.flag=='TRUE'){ if(isChanged){ await showDialog(context: context,builder: (context)=> Material( color: Colors.white.withOpacity(0.9), child: Center( child: Container( decoration: BoxDecoration( color: Colors.white, boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.5), spreadRadius: 2, blurRadius: 2, offset: Offset(0, 0), // changes position of shadow ), ], borderRadius: BorderRadius.circular(5) ), height:220, // height: MediaQuery.of(context).size.height/4.8, width: MediaQuery.of(context).size.width*0.75, child: Column( children: [ Flexible( flex:3, child: Container( padding: EdgeInsets.only(top:10,left: 10,right: 10), alignment: Alignment.center, decoration: BoxDecoration( // color: Colors.indigo, borderRadius: BorderRadius.only(topLeft: Radius.circular(5),topRight: Radius.circular(5)) ), child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.center, children: [ Icon(Icons.warning,size: 70,color: Colors.indigo,), ], ), ), ), Flexible( flex: 1, child: Container( padding: EdgeInsets.only(left: 20,right: 20), alignment: Alignment.centerLeft, child: Text('Data belum disimpan. Lanjutkan ?',style: TextStyle(fontWeight: FontWeight.w500,fontSize: 14,color: Colors.black.withOpacity(0.6)),) ), ), Flexible( flex: 1, child: Container( padding: EdgeInsets.only(bottom:10), child: Row( mainAxisAlignment: MainAxisAlignment.end, children: [ TextButton( child: Text('Proceed'), // color: Colors.red, onPressed: (){ Navigator.pop(context); Navigator.pop(context); }, ), TextButton( child: Text('Cancel'), // color: Colors.grey, onPressed: (){ Navigator.pop(context); }, ), ], ), ), ) ], ), ), ), ), ); } else Navigator.pop(context); } else{ bool not_empty = (stateController.text!='-'&&mesinController.text!='-'&&tahunController.text!='-'&&kodeController.text!='-'&&colorController.text!='-'&&rangkaController.text!='-'&&tipeController.text!='-'); if(blobList.where((element) => element.blob_file!=null).length!=0){ await showDialog(context: context,builder: (context)=> Material( color: Colors.white.withOpacity(0.9), child: Center( child: Container( decoration: BoxDecoration( color: Colors.white, boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.5), spreadRadius: 2, blurRadius: 2, offset: Offset(0, 0), // changes position of shadow ), ], borderRadius: BorderRadius.circular(5) ), height:220, // height: MediaQuery.of(context).size.height/4.8, width: MediaQuery.of(context).size.width*0.75, child: Column( children: [ Flexible( flex:3, child: Container( padding: EdgeInsets.only(top:10,left: 10,right: 10), alignment: Alignment.center, decoration: BoxDecoration( // color: Colors.indigo, borderRadius: BorderRadius.only(topLeft: Radius.circular(5),topRight: Radius.circular(5)) ), child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.center, children: [ Icon(Icons.warning,size: 70,color: Colors.indigo,), ], ), ), ), Flexible( flex: 1, child: Container( padding: EdgeInsets.only(left: 20,right: 20), alignment: Alignment.centerLeft, child: Text('Data belum disimpan. Lanjutkan ?',style: TextStyle(fontWeight: FontWeight.w500,fontSize: 14,color: Colors.black.withOpacity(0.6)),) ), ), Flexible( flex: 1, child: Container( padding: EdgeInsets.only(bottom:10), child: Row( mainAxisAlignment: MainAxisAlignment.end, children: [ TextButton( child: Text('Back',style: TextStyle(color: Colors.indigo),), onPressed: (){ Navigator.pop(context); Navigator.pop(context); }, ), TextButton( child: Text('Cancel',style: TextStyle(color: Colors.indigo)), onPressed: (){ Navigator.pop(context); }, ), ], ), ), ) ], ), ), ), ), ); } else Navigator.pop(context); } return false; }, child: Container( padding: EdgeInsets.only(left:10,right: 10,bottom: 10), child: Column( children: [ // SizedBox(height: MediaQuery.of(context).size.height/25,), Expanded( child: Row( children: [ Flexible( flex: 4, child: Container( height: MediaQuery.of(context).size.height, child: ListView.builder( // itemCount: ((images.length+1)>jenisLength)?jenisLength:images.length+1, itemCount: blobList.length, itemBuilder: (context, index){ // Uint8List image = ((images.length==0)||(images.length<(index+1)))?null:images[index]; Uint8List image = blobList[index].blob_file; return Padding( padding: const EdgeInsets.only(top: 10,right: 10), child: Material( child: InkWell( onTap: ()async{ if(image==null){ // var selected = await showDialog( // context: context, // builder: (context){ // var selected = dropMenu[0].value; // return WillPopScope( // onWillPop: ()async{ // Navigator.pop(context,false); // return false; // }, // child: AlertDialog( // content: StatefulBuilder( // builder: (context,setState){ // return DropdownButtonFormField( // decoration: InputDecoration.collapsed(), // value: selected, // onChanged: (value){ // setState(() { // selected = value; // }); // }, // items: dropMenu.map((item) { // return new DropdownMenuItem( // value: item.value, // child: Row( // children: [ // Text(item.value), //// (item.optional=='TRUE')?Container():Text(' *',style: TextStyle(color: Colors.red),) // ], // ), // ); // }).toList(), // ); // }, // ), // actions: [ // TextButton( // child: Text('Done'), // onPressed: ()async{ // jenisImages.add(selected); // // Navigator.pop(context,true); // }, // ), // TextButton( // child: Text('Cancel'), // onPressed: (){ // Navigator.pop(context,false); // }, // ), // ], // ), // ); // } // ); // if (selected){ XFile temp = await ImagePicker().pickImage(source: ImageSource.camera,maxWidth: 800,imageQuality: 80); if(temp!=null){ isChanged = true; blobList[index].blob_file = await temp.readAsBytes(); // if(widget.idInsert!=null) { // blobIds.add(null); // isChanged = true; // } setState(() { }); } // else { // jenisImages.removeLast(); // } // await loadDropMenu(); // } } else { // List dropNewMenu = new List.from(dropMenu); // dropNewMenu.add(new jenisDrop(value: jenisImages[index])); var result = await Navigator.push(context, MaterialPageRoute(builder: (context) => PhotoViewer(image,id: widget.unit.id.toString()+'.$index',jenis: blobList[index].jenis,))); if(result!=null){ blobList[index] = result['byte']; isChanged = true; // images[index] = result['byte']; // jenisImages[index] = result['jenis']; // await loadDropMenu(); // if(widget.idInsert!=null) { // isChanged = true; // } setState(() { }); } } }, splashColor: Colors.grey, child: Column( children: [ Container(padding: EdgeInsets.all(5),child: Text(blobList[index].jenis)), Stack( children: [ Container( decoration: BoxDecoration( color: Colors.grey.withOpacity(0.7), image: (image!=null)?DecorationImage( image: MemoryImage(image), fit: BoxFit.cover ):null, ), height: MediaQuery.of(context).size.width*0.35, width: MediaQuery.of(context).size.width*0.35, child: (image==null)?Icon(Icons.add):null, ), (image!=null)?Container( height: MediaQuery.of(context).size.width*0.35, width: MediaQuery.of(context).size.width*0.35, alignment: Alignment.topRight, child: InkWell( onTap: ()async{ // if(widget.idInsert!=null){ // isChanged = true; // deleteBlobIds.add(blobIds[index]); // blobIds.removeAt(index); // } setState(() { blobList[index].blob_file = null; // images.removeAt(index); // jenisImages.removeAt(index); }); // await loadDropMenu(); }, child: Container( padding: EdgeInsets.all(5), child: Icon(Icons.cancel,color: Colors.red,)), ), ):Container(), ], ), ], ), ), ), ); }) ), ), Flexible( flex: 6, child: Container( alignment: Alignment.topLeft, padding: EdgeInsets.only(top: 10), child: SingleChildScrollView( scrollDirection: Axis.vertical, child: Column( mainAxisAlignment: MainAxisAlignment.start, children: [ // Divider(), // Row( // mainAxisAlignment: MainAxisAlignment.spaceBetween, // children: [ // Expanded( // child: // textfield??Container(), // ), // ], // ), Padding( padding: const EdgeInsets.all(5.0), child: Text('Units Details',style: TextStyle(fontSize: 18,fontWeight: FontWeight.bold),), ), Divider(), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text('Tipe : '), Expanded( child: TextFormField( enabled: false, style: TextStyle(color: Colors.grey), controller: tipeController, decoration: InputDecoration.collapsed(hintText: null), ), ), ], ), Divider(), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text('Mesin : '), Expanded( child: TextFormField( enabled: false, style: TextStyle(color: Colors.grey), controller: mesinController, decoration: InputDecoration.collapsed(hintText: null), ), ), ], ), Divider(), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text('Rangka : '), Expanded( child: TextFormField( enabled: false, style: TextStyle(color: Colors.grey), controller: rangkaController, decoration: InputDecoration.collapsed(hintText: null), ), ), ], ), Divider(), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text('Kode : '), Expanded( child: TextFormField( enabled: false, style: TextStyle(color: Colors.grey), controller: kodeController, decoration: InputDecoration.collapsed(hintText: null), ), ), ], ), Divider(), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text('Tahun : '), Expanded( child: TextFormField( enabled: false, style: TextStyle(color: Colors.grey), controller: tahunController, decoration: InputDecoration.collapsed(hintText: null), ), ), ], ), Divider(), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text('Warna : '), Expanded( child: TextFormField( enabled: false, style: TextStyle(color: Colors.grey), controller: colorController, decoration: InputDecoration.collapsed(hintText: null), ), ), ], ), Divider(), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text('State : '), Expanded( child: TextFormField( enabled: false, style: TextStyle(color: Colors.grey), controller: stateController, decoration: InputDecoration.collapsed(hintText: null), ), ), ], ), Divider(), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text('Channel : '), Expanded( child: TextFormField( enabled: false, style: TextStyle(color: Colors.grey), controller: channelController, decoration: InputDecoration.collapsed(hintText: null), ), ), ], ), Divider(), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ TextButton( style: TextButton.styleFrom( backgroundColor: Colors.green, ), child: Text((widget.unit.flag=='FALSE')?'Add':'Save'), onPressed: ()async{ bool succeed = false; String errMsg = ''; util.showLoading(context); await Future.sync(()async{ if(currentPosisiton!=null){ if(blobList.where((element) => element.blob_file!=null).length==blobList.length){ for(int i = 0 ; i Material( color: Colors.white.withOpacity(0.9), child: Center( child: Container( decoration: BoxDecoration( color: Colors.white, boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.5), spreadRadius: 2, blurRadius: 2, offset: Offset(0, 0), // changes position of shadow ), ], borderRadius: BorderRadius.circular(5) ), height:220, // height: MediaQuery.of(context).size.height/4.8, width: MediaQuery.of(context).size.width*0.75, child: Column( children: [ Flexible( flex:3, child: Container( padding: EdgeInsets.only(top:10,left: 10,right: 10), alignment: Alignment.center, decoration: BoxDecoration( // color: Colors.indigo, borderRadius: BorderRadius.only(topLeft: Radius.circular(5),topRight: Radius.circular(5)) ), child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.center, children: [ Icon(Icons.warning,size: 70,color: Colors.indigo,), ], ), ), ), Flexible( flex: 1, child: Container( padding: EdgeInsets.only(left: 20,right: 20), alignment: Alignment.centerLeft, child: Text('Data belum disimpan. Lanjutkan ?',style: TextStyle(fontWeight: FontWeight.w500,fontSize: 14,color: Colors.black.withOpacity(0.6)),) ), ), Flexible( flex: 1, child: Container( padding: EdgeInsets.only(bottom:10), child: Row( mainAxisAlignment: MainAxisAlignment.end, children: [ TextButton( child: Text('Proceed'), onPressed: (){ Navigator.pop(context); Navigator.popUntil(context,ModalRoute.withName('/stocking')); }, ), TextButton( child: Text('Cancel'), // color: Colors.grey, onPressed: (){ Navigator.pop(context); }, ), ], ), ), ) ], ), ), ), ), ); } else Navigator.pop(context); } else { // bool not_empty = (stateController.text!='-'&&mesinController.text!='-'&&tahunController.text!='-'&&kodeController.text!='-'&&colorController.text!='-'&&rangkaController.text!='-'&&tipeController.text!='-'); if(blobList.where((element) => element.blob_file!=null).length!=0){ await showDialog(context: context,builder: (context)=> Material( color: Colors.white.withOpacity(0.9), child: Center( child: Container( decoration: BoxDecoration( color: Colors.white, boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.5), spreadRadius: 2, blurRadius: 2, offset: Offset(0, 0), // changes position of shadow ), ], borderRadius: BorderRadius.circular(5) ), height:220, // height: MediaQuery.of(context).size.height/4.8, width: MediaQuery.of(context).size.width*0.75, child: Column( children: [ Flexible( flex:3, child: Container( padding: EdgeInsets.only(top:10,left: 10,right: 10), alignment: Alignment.center, decoration: BoxDecoration( // color: Colors.indigo, borderRadius: BorderRadius.only(topLeft: Radius.circular(5),topRight: Radius.circular(5)) ), child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.center, children: [ Icon(Icons.warning,size: 70,color: Colors.indigo,), ], ), ), ), Flexible( flex: 1, child: Container( padding: EdgeInsets.only(left: 20,right: 20), alignment: Alignment.centerLeft, child: Text('Data belum disimpan. Lanjutkan ?',style: TextStyle(fontWeight: FontWeight.w500,fontSize: 14,color: Colors.black.withOpacity(0.6)),) ), ), Flexible( flex: 1, child: Container( padding: EdgeInsets.only(bottom:10), child: Row( mainAxisAlignment: MainAxisAlignment.end, children: [ TextButton( child: Text('Back'), onPressed: (){ Navigator.pop(context); Navigator.popUntil(context,ModalRoute.withName('/stocking')); }, ), TextButton( child: Text('Cancel'), onPressed: (){ Navigator.pop(context); }, ), ], ), ), ) ], ), ), ), ), ); } else Navigator.pop(context); } }, style: TextButton.styleFrom( backgroundColor: Colors.redAccent, ), ) ], ) ], ), ), ), ) ], ), ), ], ), ), ), ); } }