import 'dart:typed_data'; import '../Model/unit.dart'; import '../Widgets/photo_viewer.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; import '../Utils/db_helper.dart'; import '../main.dart'; import 'package:intl/intl.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import '../Blocs/Units/Images/get_images_bloc.dart'; import '../Blocs/Units/Save/save_unit_bloc.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(); bool isChanged = false; List images = []; List blobIds = []; List deleteBlobIds = []; List dropMenu = []; List jenisImages = []; int jenisLength=0; late Unit _currentUnit; late GetImagesBloc _unitImagesBloc; late SaveUnitBloc _saveUnitBloc; List blobList = []; loadUnitDetails(){ _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; _unitImagesBloc.add(GetImages(mesin:mesinController.text)); } @override void dispose() { super.dispose(); _unitImagesBloc.close(); _saveUnitBloc.close(); } @override void initState() { super.initState(); _unitImagesBloc = GetImagesBloc(); _saveUnitBloc = SaveUnitBloc(); } @override Widget build(BuildContext context) { 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.bytes!=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: MultiBlocProvider( providers: [ BlocProvider( create: (_)=>_unitImagesBloc, ), BlocProvider( create: (_)=>_saveUnitBloc, ), ], child:MultiBlocListener( listeners: [ BlocListener( listener: (context,state){ if(state is GetImagesError){ util.showFlushbar(context, state.err,color: Colors.red); } }, ), BlocListener( listener: (context,state){ if(state is SaveFinish){ if(state.success){ Navigator.pop(context,true); } util.showFlushbar(context, state.msg,color: state.success?Colors.grey:Colors.red); } }, ), ], child: BlocBuilder( bloc: _unitImagesBloc, builder: (context,state){ if(state is GetImagesLoading){ return Center( child: Container( height: 15, width: 15, child: CircularProgressIndicator(), ), ); } else if (state is GetImagesFinish){ blobList = state.blobs; } else if (state is GetImagesInitial){ loadUnitDetails(); } return Container( padding: EdgeInsets.only(left:10,right: 10,bottom: 10), child: Column( children: [ 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].bytes; 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].bytes = 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 ImageType(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].bytes = 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{ if(currentPosisiton!=null) { if(blobList.where((element) => element.bytes!=null).length==blobList.length){ _saveUnitBloc.add(Save(blobs: blobList, unit: _currentUnit)); } else{ util.showFlushbar(context, 'Tolong lengkapi data foto'); } } else{ util.showFlushbar(context, 'Gagal mengamil koordinat lokasi. Tolong cek gps.'); } }, ), TextButton( child: Text('Cancel'), onPressed: ()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'), 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.bytes!=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, ), ) ], ) ], ), ), ), ) ], ), ), ], ), ); }, ), ), ), ), ); } }