From d1314c4fb0f1c30891a0ef9e75cf0d1ecad2ad17 Mon Sep 17 00:00:00 2001 From: suresh Date: Tue, 12 Sep 2023 13:28:22 +0530 Subject: [PATCH] Added Profile picture --- lib/dashboard.dart | 145 +++++++++++++++++++++++------------------ lib/settings.dart | 26 ++++++++ lib/updateprofile.dart | 42 +++++++----- 3 files changed, 135 insertions(+), 78 deletions(-) diff --git a/lib/dashboard.dart b/lib/dashboard.dart index 2b38c66..e338f45 100644 --- a/lib/dashboard.dart +++ b/lib/dashboard.dart @@ -9,6 +9,7 @@ import 'package:watermanagement/login.dart'; import 'package:watermanagement/settings.dart'; import 'package:image_picker/image_picker.dart'; import 'package:watermanagement/tankersview.dart'; +import 'dart:convert'; import 'dart:io'; import 'package:watermanagement/updateprofile.dart'; import 'booking_requests.dart'; @@ -91,16 +92,18 @@ class _DashboardState extends State { Future pickImageFromGallery() async { try { - final image = await _picker.pickImage(source: ImageSource.gallery); - if (image == null) return; - final imageTemp = File(image.path); + final image = await _picker.pickImage(source: ImageSource.gallery); + if (image == null) return; + final imageTemp = File(image.path); + + AppSettings.preLoaderDialog(context); + var res = await AppSettings.uploadImageHTTPNew(image); + print(jsonDecode(res)); + Navigator.of(context, rootNavigator: true).pop(); setState(() { - AppSettings.updatedImage = imageTemp; + AppSettings.profilePictureUrl = jsonDecode(res)['picture']; }); - uploadProfileApi(image.path); - AppSettings.saveProfile(image.path); - - + AppSettings.saveData('profile', jsonDecode(res)['picture'], 'STRING'); } on PlatformException catch (e) { print('Failed to pick image: $e'); } @@ -111,13 +114,14 @@ class _DashboardState extends State { final image = await _picker.pickImage(source: ImageSource.camera); if (image == null) return; final imageTemp = File(image.path); + AppSettings.preLoaderDialog(context); + var res = await AppSettings.uploadImageHTTPNew(image); + print(jsonDecode(res)); + Navigator.of(context, rootNavigator: true).pop(); setState(() { - AppSettings.updatedImage = imageTemp; + AppSettings.profilePictureUrl = jsonDecode(res)['picture']; }); - - uploadProfileApi(image.path); - AppSettings.saveProfile(image.path); - + AppSettings.saveData('profile', jsonDecode(res)['picture'], 'STRING'); } on PlatformException catch (e) { print('Failed to pick image: $e'); } @@ -428,59 +432,76 @@ class _DashboardState extends State { children: [ - Expanded(child: GestureDetector( - child: Container( - width: MediaQuery.of(context).size.width * .20, - height: MediaQuery.of(context).size.height * .15, - decoration: BoxDecoration( - shape: BoxShape.circle, + Expanded( + child: GestureDetector( + child: Container( + width: (MediaQuery.of(context).size.width > 600) + ? MediaQuery.of(context).size.width * .30 + : MediaQuery.of(context).size.width * .60, // Adjusted width for tablet + height: (MediaQuery.of(context).size.width > 600) + ? MediaQuery.of(context).size.height * .25 + : MediaQuery.of(context).size.height * .50, // Adjusted height for tablet + decoration: BoxDecoration( + shape: BoxShape.rectangle, + border: Border.all(width: 2, color: Colors.blueGrey), image: DecorationImage( - image: (AppSettings.updatedImage != null) ? FileImage(AppSettings.updatedImage!) as ImageProvider : AssetImage("images/profile_pic.png"), // picked file - fit: BoxFit.cover)), - ), - onTap: () { - 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: greyColor, + image: (AppSettings.profilePictureUrl != '' && + AppSettings.profilePictureUrl != 'null') + ? NetworkImage(AppSettings.profilePictureUrl) as ImageProvider + : AssetImage(" images/profile_pic.png"), // picked file + fit: BoxFit.fitWidth, + ), + ), + ), + onTap: () { + 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: greyColor, + ), + onTap: () async { + await takeImageFromCamera(); + Navigator.pop(context); + }, + ), + SizedBox( + width: MediaQuery.of(context) + .size + .width * + .20, ), - onTap: () async { - await takeImageFromCamera(); - Navigator.pop(context); - }, - ), - SizedBox( - width: - MediaQuery.of(context).size.width * .20, - ), - GestureDetector( - child: Icon( - Icons.photo, - size: 100, - color: greyColor, + GestureDetector( + child: Icon( + Icons.photo, + size: 100, + color: greyColor, + ), + onTap: () async { + await pickImageFromGallery(); + Navigator.pop(context); + }, ), - onTap: () async { - await pickImageFromGallery(); - Navigator.pop(context); - }, - ), - ], + ], + ), ), - ), - ); - }); - }, - ),), + ); + }); + }, + ), + + + ), Text( AppSettings.suppliername, diff --git a/lib/settings.dart b/lib/settings.dart index 1cbfefd..7f1b445 100644 --- a/lib/settings.dart +++ b/lib/settings.dart @@ -124,6 +124,13 @@ class AppSettings { static String acceptRequestUrl = host +"friend-request/accept"; static String rejectRequestUrl = host +"friend-request/reject"; static String profilePicUrl = host + 'supplier/profile-picture'; + static String uploadPicUrl = host + 'uploads'; + + + + static String profilePictureUrl = ''; + + static File? updatedImage; static String image=''; @@ -226,6 +233,25 @@ class AppSettings { return false; } + + + static Future uploadImageHTTPNew(file) async { + + var request = http.MultipartRequest('POST', Uri.parse(uploadPicUrl + '/' + supplierId)); + request.files.add(await http.MultipartFile.fromPath('picture', file.path)); + var res = await request.send(); + var response = await http.Response.fromStream(res); + return response.body; + + } + + + + + + + + /*Apis Starts here*/ static Future login(payload) async { diff --git a/lib/updateprofile.dart b/lib/updateprofile.dart index 1bb3c10..d9ff686 100644 --- a/lib/updateprofile.dart +++ b/lib/updateprofile.dart @@ -5,6 +5,7 @@ import 'package:flutter/services.dart'; import 'package:watermanagement/dashboard.dart'; import 'package:watermanagement/settings.dart'; import 'package:image_picker/image_picker.dart'; +import 'dart:convert'; import 'dart:io'; import 'login.dart'; @@ -52,17 +53,20 @@ class _UpdateprofileState extends State { final ImagePicker _picker = ImagePicker(); Future pickImageFromGallery() async { + try { final image = await _picker.pickImage(source: ImageSource.gallery); if (image == null) return; final imageTemp = File(image.path); + + AppSettings.preLoaderDialog(context); + var res = await AppSettings.uploadImageHTTPNew(image); + print(jsonDecode(res)); + Navigator.of(context, rootNavigator: true).pop(); setState(() { - AppSettings.updatedImage = imageTemp; + AppSettings.profilePictureUrl = jsonDecode(res)['picture']; }); - //uploadProfileApi(AppSettings.updatedImage); - AppSettings.saveProfile(image.path); - - + AppSettings.saveData('profile', jsonDecode(res)['picture'], 'STRING'); } on PlatformException catch (e) { print('Failed to pick image: $e'); } @@ -73,11 +77,15 @@ class _UpdateprofileState extends State { final image = await _picker.pickImage(source: ImageSource.camera); if (image == null) return; final imageTemp = File(image.path); + AppSettings.preLoaderDialog(context); + var res = await AppSettings.uploadImageHTTPNew(image); + print(jsonDecode(res)); + Navigator.of(context, rootNavigator: true).pop(); setState(() { - AppSettings.updatedImage = imageTemp; + AppSettings.profilePictureUrl = jsonDecode(res)['picture']; }); - - // uploadProfileApi(AppSettings.updatedImage); + AppSettings.saveData('profile', jsonDecode(res)['picture'], 'STRING'); + // uploadProfileApi(AppSettings.updatedImage); AppSettings.saveProfile(image.path); } on PlatformException catch (e) { @@ -115,15 +123,17 @@ class _UpdateprofileState extends State { image: AssetImage('images/logo.png'), height: MediaQuery.of(context).size.height * .10, )),*/ - Container(child: GestureDetector( + Container(child: + GestureDetector( child: Container( - width: MediaQuery.of(context).size.width * .30, - height: MediaQuery.of(context).size.height * .20, + width: MediaQuery.of(context).size.width * .60, + height: MediaQuery.of(context).size.height * .15, decoration: BoxDecoration( - shape: BoxShape.circle, + shape: BoxShape.rectangle, + border: Border.all(width: 2, color: Colors.blueGrey), image: DecorationImage( - image: (AppSettings.updatedImage != null) ? FileImage(AppSettings.updatedImage!) as ImageProvider : AssetImage("images/profile_pic.png"), // picked file - fit: BoxFit.cover)), + image: (AppSettings.profilePictureUrl != ''&& AppSettings.profilePictureUrl != 'null') ? NetworkImage(AppSettings.profilePictureUrl) as ImageProvider : AssetImage("images/mobilebg.png"), // picked file + fit: BoxFit.fitWidth)), ), onTap: () { showModalBottomSheet( @@ -139,7 +149,7 @@ class _UpdateprofileState extends State { child: Icon( Icons.camera_alt_outlined, size: 100, - color: greyColor, + color: primaryColor, ), onTap: () async { await takeImageFromCamera(); @@ -154,7 +164,7 @@ class _UpdateprofileState extends State { child: Icon( Icons.photo, size: 100, - color: greyColor, + color: primaryColor, ), onTap: () async { await pickImageFromGallery();