import 'dart:io'; import 'package:connectivity/connectivity.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; import 'package:photolink_beta/Model/CustomResponse/APIResponse.dart'; import 'package:photolink_beta/Model/Trip/Trip.dart'; import 'package:photolink_beta/customWidget/loader.dart'; import 'package:photolink_beta/secreens/photolinkmap.dart'; import 'package:photolink_beta/services/Trip/CreateNewTrips.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'GetAllMyTrip.dart'; const TripName_Cache ="TripName_Cache"; const TripDescription_Cache ="TripDescription_Cache"; const TripImage_Cache ="TripImage_Cache"; class CreateTripe extends StatefulWidget { @override _CreateTripeState createState() => _CreateTripeState(); } class _CreateTripeState extends State { final CreateNewTrips TripServices = CreateNewTrips(); TextEditingController _tripName = new TextEditingController(); TextEditingController _tripDescription = new TextEditingController(); bool Connection = false; /********************************************************/ File _image; bool isLoading = false; final picker = ImagePicker(); @override void initState() { super.initState(); setState(() { isLoading = false; }); } /********************************************************/ Future checkConnection() async { var connectivityResult = await (Connectivity().checkConnectivity()); if (connectivityResult == ConnectivityResult.none) { setState(() { Connection = false; }); //checkConnection(); } else { setState(() { Connection = true; }); //await _fetchTrips(); } } Widget DroobLoader(){ return Center( child: Container( child: ColorLoader1(), ), ); } bool ValidatorInpute() { return _image != null && _tripDescription.text != '' && _tripName.text != ''; } Future getImage(BuildContext context) async { final pickedFile = await picker.getImage(source: ImageSource.camera); setState(() { if (pickedFile != null) { _image = File(pickedFile.path); if (_image != null) Navigator.of(context, rootNavigator: true).pop(); } else { print('No image selected.'); } }); } Future getImagefromGallery(BuildContext context) async { final pickedFile = await picker.getImage(source: ImageSource.gallery); setState(() { _image = File(pickedFile.path); if (_image != null) Navigator.of(context, rootNavigator: true).pop(); }); } /********************************************************/ @override Widget build(BuildContext context) { if (isLoading) {return DroobLoader();} return Scaffold( backgroundColor: Colors.white, appBar: AppBar( title: Text("Create New Trip"), ), body: SingleChildScrollView( child: Column( children: [ SizedBox( height: 20, ), Padding( //padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0), padding: EdgeInsets.symmetric(horizontal: 15), child: TextField( controller: _tripName, decoration: InputDecoration( border: OutlineInputBorder(), labelText: 'Trip name', hintText: 'Trip name'), ), ), Padding( padding: const EdgeInsets.only( left: 15.0, right: 15.0, top: 15, bottom: 0), //padding: EdgeInsets.symmetric(horizontal: 15), child: TextField( controller: _tripDescription, decoration: InputDecoration( border: OutlineInputBorder(), labelText: 'Description', hintText: 'Trip Description'), ), ), SizedBox( height: 20, ), Container( height: 50, width: 325, decoration: BoxDecoration( color: Colors.white, ), child: OutlinedButton.icon( onPressed: () { ShowImageOption(context); }, icon: Icon(Icons.add_a_photo, size: 18), label: Text("Add picture for trip"), ), ), SizedBox( height: 20, ), Container( height: 50, width: 250, decoration: BoxDecoration( color: Colors.blue, borderRadius: BorderRadius.circular(20)), child: FlatButton( onPressed: () async { try { if (!ValidatorInpute()) { _image = null; _tripName.text = ''; _tripDescription.text = ''; throw ('please fill all inputs'); } final prefs = await SharedPreferences.getInstance(); await checkConnection(); print(Connection.toString()); if(Connection){ CreateNewTrips controller = new CreateNewTrips(); setState(() { isLoading = true; }); final TripServices = await controller.NewTrip( _image, _tripName.text, _tripDescription.text); if ( (TripServices.statusCode == 200 || TripServices.statusCode == 201) && TripServices.data.id != null) { setState(() { isLoading = false; }); Navigator.push(context, MaterialPageRoute(builder: (_) => photolinkmap(trip: TripServices.data,))); } else { throw (TripServices.errorMessage); } }else{ var Date = new DateTime.now(); Trip trip_cache = new Trip(name :_tripName.text,picture:_image.path,start_date:Date.toString(),id:null,path:null,description:_tripDescription.text,end_date:null); if(prefs.get(TripName_Cache)!=null||prefs.get(TripDescription_Cache)!=null||prefs.get(TripImage_Cache)!=null){ prefs.remove(TripName_Cache); prefs.remove(TripDescription_Cache); prefs.remove(TripImage_Cache); throw ("You have already other trip offline"); }else{ prefs.setString(TripName_Cache, _tripName.text); prefs.setString(TripDescription_Cache, _tripDescription.text); prefs.setString(TripImage_Cache, _image.path); Navigator.push(context, MaterialPageRoute(builder: (_) => photolinkmap(trip: trip_cache,))); } } } catch (e) { //stop loading print(e); showAlertDialog(context); } }, child: Text( 'Create Now', style: TextStyle(color: Colors.yellow, fontSize: 15), ), ), ), SizedBox( height: 20, ), Container( height: 50, width: 250, decoration: BoxDecoration( color: Colors.yellow, borderRadius: BorderRadius.circular(20)), child: FlatButton( onPressed: () { //TODO FORGOT PASSWORD SCREEN GOES HERE Navigator.push( context, MaterialPageRoute(builder: (_) => AllMytrip())); }, child: Text( 'Already have a trip created ?', style: TextStyle(color: Colors.blue, fontSize: 15), ), ), ), ], ), ), ); } showAlertDialog(BuildContext context) { // set up the buttons Widget cancelButton = FlatButton( child: Text("Cancel"), onPressed: () {}, ); Widget continueButton = FlatButton( child: Text("Continue"), onPressed: () {}, ); // set up the AlertDialog AlertDialog alert = AlertDialog( title: Text("AlertDialog"), content: Text("Please make sure from all the field"), actions: [ cancelButton, continueButton, ], ); // show the dialog showDialog( context: context, builder: (BuildContext context) { return alert; }, ); } ShowImageOption(BuildContext context) { // set up the buttons Widget cancelButton = FlatButton( child: Text("Gallery"), onPressed: () async { await getImagefromGallery(context); }, ); Widget continueButton = FlatButton( child: Text("Take Photo now"), onPressed: () async { await getImage(context); }, ); // set up the AlertDialog AlertDialog alert = AlertDialog( title: Text("AlertDialog"), content: Text("Please make sure from all the field"), actions: [ cancelButton, continueButton, ], ); // show the dialog showDialog( context: context, builder: (BuildContext context) { return alert; }, ); } }