import 'dart:convert'; import 'dart:io'; import 'package:doctor/common/settings.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:image_picker/image_picker.dart'; import 'package:multi_image_picker/multi_image_picker.dart'; class AddResources extends StatefulWidget { const AddResources({Key? key}) : super(key: key); @override State createState() => _AddResourcesState(); } class _AddResourcesState extends State { TextEditingController nameController = TextEditingController(); TextEditingController youtubeLinkUrlController = TextEditingController(); TextEditingController dosController1 = TextEditingController(); TextEditingController dosController2 = TextEditingController(); TextEditingController dosController3 = TextEditingController(); TextEditingController donotsController1 = TextEditingController(); TextEditingController donotsController2 = TextEditingController(); TextEditingController donotsController3 = TextEditingController(); TextEditingController urlController1 = TextEditingController(); TextEditingController urlController2 = TextEditingController(); TextEditingController urlController3 = TextEditingController(); bool isSecondAddButtonShow=false; bool is2nddosControllerVisible=false; bool is3rddosControllerVisible=false; bool is4thdosControllerVisible=false; bool is5thdosControllerVisible=false; bool is2nddonotsControllerVisible=false; bool is3rddonotsControllerVisible=false; bool is4thdonotsControllerVisible=false; bool is5thdonotsControllerVisible=false; bool is2ndurlControllerVisible=false; bool is3rdurlControllerVisible=false; bool is4thurlControllerVisible=false; bool is5thurlControllerVisible=false; List dos=[]; List donots=[]; List urls=[]; List images=[]; List videos=[]; List uiImages = []; List imageFileListResources = []; final ImagePicker _picker = ImagePicker(); String Url = ''; final ImagePicker imagePicker = ImagePicker(); Future takeImageFromCameraForReports() async { try { final image = await _picker.pickImage(source: ImageSource.camera); if (image == null) return; final imageTemp = File(image.path); AppSettings.preLoaderDialog(context); var res = await AppSettings.uploadResourceCamera(image); print(jsonDecode(res)); Navigator.of(context, rootNavigator: true).pop(); setState(() { uiImages = jsonDecode(res)['pictures']; images= jsonDecode(res)['pictures']; //reports=jsonDecode(res)['reportsPictureUpload']; //reportsPictureId=jsonDecode(res)['reportsPictureUpload']['reportsPictureId']; }); } on PlatformException catch (e) { print('Failed to pick image: $e'); } } Future pickImageFromGalleryForReports() async { imageFileListResources = []; final List? selectedImages = await imagePicker.pickMultiImage(); AppSettings.preLoaderDialog(context); if (selectedImages!.isNotEmpty) { imageFileListResources.addAll(selectedImages); } var res = await AppSettings.uploadResourcesGallery(imageFileListResources); print(jsonDecode(res)); Navigator.of(context, rootNavigator: true).pop(); setState(() { uiImages = jsonDecode(res)['pictures']; images= jsonDecode(res)['pictures']; //reports=jsonDecode(res)['reportsPictureUpload']; //reportsPictureId=jsonDecode(res)['reportsPictureUpload']['reportsPictureId']; }); } @override Widget build(BuildContext context) { return Scaffold( appBar:AppSettings.appBar('Add Resources'), body: Stack(children: [ Container( decoration: const BoxDecoration( /* image: DecorationImage( image: AssetImage("images/backgroundimage.png"), fit: BoxFit.cover, ),*/ ), ), GestureDetector( onTap: () { FocusScope.of(context).requestFocus(new FocusNode()); }, child: SafeArea( child: SingleChildScrollView( child: Padding( padding: EdgeInsets.all(10), child: Column( children: [ SizedBox( height:MediaQuery.of(context).size.height * .02, ), SizedBox(height:MediaQuery.of(context).size.height * .02,), Container( child: TextFormField( cursorColor: greyColor, controller: nameController, decoration: const InputDecoration( prefixIcon: Icon( Icons.person, color: greyColor, ), border: OutlineInputBorder( borderSide: BorderSide(color: greyColor)), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), labelText: 'Resource name', labelStyle: TextStyle( color: greyColor, //<-- SEE HERE ), ), ), ),//name SizedBox(height:MediaQuery.of(context).size.height * .02,), Text('Dos',style: TextStyle(color: primaryColor,fontSize: 14,fontWeight: FontWeight.bold),), SizedBox(height:MediaQuery.of(context).size.height * .02,), Container( width: double.infinity, decoration: BoxDecoration( color: secondaryColor, border: Border.all( //width: 10, color: Colors.white, ), borderRadius: BorderRadius.circular( 20, )), child: Padding( padding: EdgeInsets.all(10), child: Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ TextFormField( cursorColor: greyColor, controller: dosController1, decoration: const InputDecoration( prefixIcon: Icon( Icons.location_city, color: greyColor, ), border: OutlineInputBorder( borderSide: BorderSide(color: greyColor)), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), labelText: 'Description', labelStyle: TextStyle( color: greyColor, //<-- SEE HERE ), ), ), Visibility( visible: dosController1.text!='', child: IconButton( onPressed: (){ setState(() { is2nddosControllerVisible=true; }); }, icon: Icon(Icons.add_box, color: primaryColor,))) ], ), ), ), SizedBox(height:MediaQuery.of(context).size.height * .02,), Visibility( visible:dosController2.text!=''&&dosController2.text!='null'||is2nddosControllerVisible, child: Container( width: double.infinity, decoration: BoxDecoration( color: secondaryColor, border: Border.all( //width: 10, color: Colors.white, ), borderRadius: BorderRadius.circular( 20, )), child: Padding( padding: EdgeInsets.all(10), child: Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ TextFormField( cursorColor: greyColor, controller: dosController2, decoration: const InputDecoration( prefixIcon: Icon( Icons.location_city, color: greyColor, ), border: OutlineInputBorder( borderSide: BorderSide(color: greyColor)), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), labelText: 'Description', labelStyle: TextStyle( color: greyColor, //<-- SEE HERE ), ), ), Visibility( visible: dosController2.text!='', child: IconButton( onPressed: (){ setState(() { is3rddosControllerVisible=true; }); }, icon: Icon(Icons.add_box, color: primaryColor,))) ], ), ), ),), SizedBox(height:MediaQuery.of(context).size.height * .02,), Visibility( visible: dosController3.text!=''&&dosController3.text!='null'||is3rddosControllerVisible, child: Container( width: double.infinity, decoration: BoxDecoration( color: secondaryColor, border: Border.all( //width: 10, color: Colors.white, ), borderRadius: BorderRadius.circular( 20, )), child: Padding( padding: EdgeInsets.all(10), child: Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ TextFormField( cursorColor: greyColor, controller: dosController3, decoration: const InputDecoration( prefixIcon: Icon( Icons.location_city, color: greyColor, ), border: OutlineInputBorder( borderSide: BorderSide(color: greyColor)), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), labelText: 'Description', labelStyle: TextStyle( color: greyColor, //<-- SEE HERE ), ), ), /*Visibility( visible: dosController3.text!='', child: IconButton( onPressed: (){ setState(() { is4thdosControllerVisible=true; }); }, icon: Icon(Icons.add_box, color: primaryColor,)))*/ ], ), ), ),), SizedBox(height:MediaQuery.of(context).size.height * .02,), Text('Donts',style: TextStyle(color: primaryColor,fontSize: 14,fontWeight: FontWeight.bold),), SizedBox(height:MediaQuery.of(context).size.height * .02,), Container( width: double.infinity, decoration: BoxDecoration( color: secondaryColor, border: Border.all( //width: 10, color: Colors.white, ), borderRadius: BorderRadius.circular( 20, )), child: Padding( padding: EdgeInsets.all(10), child: Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ TextFormField( cursorColor: greyColor, controller: donotsController1, decoration: const InputDecoration( prefixIcon: Icon( Icons.location_city, color: greyColor, ), border: OutlineInputBorder( borderSide: BorderSide(color: greyColor)), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), labelText: 'Description', labelStyle: TextStyle( color: greyColor, //<-- SEE HERE ), ), ), Visibility( visible: donotsController1.text!='', child: IconButton( onPressed: (){ setState(() { is2nddonotsControllerVisible=true; }); }, icon: Icon(Icons.add_box, color: primaryColor,))) ], ), ), ), SizedBox(height:MediaQuery.of(context).size.height * .02,), Visibility( visible:donotsController2.text!=''&&donotsController2.text!='null'||is2nddonotsControllerVisible, child: Container( width: double.infinity, decoration: BoxDecoration( color: secondaryColor, border: Border.all( //width: 10, color: Colors.white, ), borderRadius: BorderRadius.circular( 20, )), child: Padding( padding: EdgeInsets.all(10), child: Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ TextFormField( cursorColor: greyColor, controller: donotsController2, decoration: const InputDecoration( prefixIcon: Icon( Icons.location_city, color: greyColor, ), border: OutlineInputBorder( borderSide: BorderSide(color: greyColor)), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), labelText: 'Description', labelStyle: TextStyle( color: greyColor, //<-- SEE HERE ), ), ), Visibility( visible: donotsController2.text!='', child: IconButton( onPressed: (){ setState(() { is3rddonotsControllerVisible=true; }); }, icon: Icon(Icons.add_box, color: primaryColor,))) ], ), ), ),), SizedBox(height:MediaQuery.of(context).size.height * .02,), Visibility( visible: donotsController3.text!=''&&donotsController3.text!='null'||is3rddonotsControllerVisible, child: Container( width: double.infinity, decoration: BoxDecoration( color: secondaryColor, border: Border.all( //width: 10, color: Colors.white, ), borderRadius: BorderRadius.circular( 20, )), child: Padding( padding: EdgeInsets.all(10), child: Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ TextFormField( cursorColor: greyColor, controller: donotsController3, decoration: const InputDecoration( prefixIcon: Icon( Icons.location_city, color: greyColor, ), border: OutlineInputBorder( borderSide: BorderSide(color: greyColor)), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), labelText: 'Description', labelStyle: TextStyle( color: greyColor, //<-- SEE HERE ), ), ), /*Visibility( visible: dosController3.text!='', child: IconButton( onPressed: (){ setState(() { is4thdosControllerVisible=true; }); }, icon: Icon(Icons.add_box, color: primaryColor,)))*/ ], ), ), ),), SizedBox(height:MediaQuery.of(context).size.height * .02,), SizedBox(height:MediaQuery.of(context).size.height * .02,), Text('Urls',style: TextStyle(color: primaryColor,fontSize: 14,fontWeight: FontWeight.bold),), SizedBox(height:MediaQuery.of(context).size.height * .02,), Container( width: double.infinity, decoration: BoxDecoration( color: secondaryColor, border: Border.all( //width: 10, color: Colors.white, ), borderRadius: BorderRadius.circular( 20, )), child: Padding( padding: EdgeInsets.all(10), child: Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ TextFormField( cursorColor: greyColor, controller: urlController1, decoration: const InputDecoration( prefixIcon: Icon( Icons.link, color: greyColor, ), border: OutlineInputBorder( borderSide: BorderSide(color: greyColor)), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), labelText: 'Enter video link url if any', labelStyle: TextStyle( color: greyColor, //<-- SEE HERE ), ), ), Visibility( visible: urlController1.text!='', child: IconButton( onPressed: (){ setState(() { is2ndurlControllerVisible=true; }); }, icon: Icon(Icons.add_box, color: primaryColor,))) ], ), ), ), SizedBox(height:MediaQuery.of(context).size.height * .02,), Visibility( visible:urlController2.text!=''&&urlController2.text!='null'||is2ndurlControllerVisible, child: Container( width: double.infinity, decoration: BoxDecoration( color: secondaryColor, border: Border.all( //width: 10, color: Colors.white, ), borderRadius: BorderRadius.circular( 20, )), child: Padding( padding: EdgeInsets.all(10), child: Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ TextFormField( cursorColor: greyColor, controller: urlController2, decoration: const InputDecoration( prefixIcon: Icon( Icons.link, color: greyColor, ), border: OutlineInputBorder( borderSide: BorderSide(color: greyColor)), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), labelText: 'Enter video link url if any', labelStyle: TextStyle( color: greyColor, //<-- SEE HERE ), ), ), Visibility( visible: urlController2.text!='', child: IconButton( onPressed: (){ setState(() { is3rdurlControllerVisible=true; }); }, icon: Icon(Icons.add_box, color: primaryColor,))) ], ), ), ),), SizedBox(height:MediaQuery.of(context).size.height * .02,), Visibility( visible: urlController3.text!=''&&urlController3.text!='null'||is3rdurlControllerVisible, child: Container( width: double.infinity, decoration: BoxDecoration( color: secondaryColor, border: Border.all( //width: 10, color: Colors.white, ), borderRadius: BorderRadius.circular( 20, )), child: Padding( padding: EdgeInsets.all(10), child: Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ TextFormField( cursorColor: greyColor, controller: urlController3, decoration: const InputDecoration( prefixIcon: Icon( Icons.link, color: greyColor, ), border: OutlineInputBorder( borderSide: BorderSide(color: greyColor)), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), labelText: 'Enter video link url if any', labelStyle: TextStyle( color: greyColor, //<-- SEE HERE ), ), ), /*Visibility( visible: dosController3.text!='', child: IconButton( onPressed: (){ setState(() { is4thdosControllerVisible=true; }); }, icon: Icon(Icons.add_box, color: primaryColor,)))*/ ], ), ), ),), Visibility( visible: uiImages.length == 0, child: ElevatedButton( style: ElevatedButton.styleFrom( primary: primaryColor, // background onPrimary: Colors.white, // foreground ), onPressed: () async { showModalBottomSheet( context: context, builder: (BuildContext context) { return SizedBox( height: 200, child: Center( child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ GestureDetector( child: Icon( Icons.camera_alt_outlined, size: 100, color: primaryColor, ), onTap: () async { await takeImageFromCameraForReports(); Navigator.pop(context); }, ), SizedBox( width: MediaQuery.of(context).size.width * .20, ), GestureDetector( child: Icon( Icons.photo, size: 100, color: primaryColor, ), onTap: () async { await pickImageFromGalleryForReports(); Navigator.pop(context); }, ), ], ), ), ); }); }, child: const Text('Select Image'), ), ), Visibility( visible: uiImages.length > 0, child: Container( width: double.infinity, height: MediaQuery.of(context).size.height * .20, child: ListView.builder( scrollDirection: Axis.horizontal, itemCount: uiImages.length, itemBuilder: (context, index) { return Column( children: [ Card( child: GestureDetector( onTap: () { /* Navigator.push( context, new MaterialPageRoute( builder: (__) => new ImageZoomPage(imageName:'Reports',imageDetails:uiReportsImages[index]['url']))); */ }, child: Container( width: MediaQuery.of(context).size.width * .30, height: MediaQuery.of(context).size.height * .15, decoration: BoxDecoration( shape: BoxShape.rectangle, image: DecorationImage( image: NetworkImage( uiImages[0]) as ImageProvider, // picked file fit: BoxFit.fill)), child: Stack(children: [ /*Positioned( right: 0, child: Container( child: IconButton( iconSize: 30, icon: const Icon( Icons.cancel, color: Colors.red, ), onPressed: () async { AppSettings.preLoaderDialog(context); String fileName = uiReportsImages[index]['url'].split('/').last; var response = await AppSettings.deleteReports(fileName,reportsPictureId); print(jsonDecode(response)); Navigator.of(context, rootNavigator: true).pop(); setState(() { uiReportsImages = jsonDecode(response)['reportsPictureUpload']['reports']; }); }, ), ), )*/ ]), ), ) ), /*Expanded(child:IconButton( icon: const Icon(Icons.remove,color: Colors.red,), onPressed: () async{ }, ),)*/ ], ); }), )), SizedBox(height:MediaQuery.of(context).size.height * .02,), Container( width:double.infinity, height: MediaQuery.of(context).size.height * .06, child: ElevatedButton( style: ElevatedButton.styleFrom( primary: primaryColor, // background onPrimary: Colors.white, // foreground ), onPressed: () async{ if(dosController1!=''){ dos.add({ 'dos': dosController1.text, }); } if(dosController2!=''){ dos.add({ 'dos': dosController2.text, }); } if(dosController3!=''){ dos.add({ 'dos': dosController3.text, }); } if(donotsController1!=''){ donots.add({ 'doNots': donotsController1.text, }); } if(donotsController2!=''){ donots.add({ 'doNots': donotsController2.text, }); } if(donotsController3!=''){ donots.add({ 'doNots': donotsController3.text, }); } if(urlController1!=''){ urls.add({ 'url': urlController1.text, }); } if(urlController2!=''){ urls.add({ 'url': urlController2.text, }); } if(urlController3!=''){ urls.add({ 'url': urlController3.text, }); } AppSettings.preLoaderDialog(context); var payload = new Map(); payload["resourceName"] = nameController.text.toString(); payload["dos"] =dos ; payload["doNots"] =donots ; payload["url"] =urls ; payload["image"] =images; payload["createdBy"] =AppSettings.doctorName ; payload["updatedBy"] =AppSettings.doctorName ; bool addResourcesState = await AppSettings.addResource(payload); try{ if(addResourcesState){ Navigator.of(context,rootNavigator: true).pop(); AppSettings.longSuccessToast('Resource added successfully'); Navigator.pop(context); } else{ Navigator.of(context,rootNavigator: true).pop(); AppSettings.longFailedToast('Fail to add Resource details'); } } catch(e){ print(e); Navigator.of(context,rootNavigator: true).pop(); AppSettings.longFailedToast('Fail to add Resource details'); } }, child: Text('Add Resource'), ) ), ], ), ) )), ), ])); } }