diff --git a/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_bloc.dart b/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_bloc.dart index 78b5592e..73f12489 100644 --- a/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_bloc.dart +++ b/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_bloc.dart @@ -79,6 +79,8 @@ class DiaryEditBloc extends Bloc { moderationExpanded: currentState.moderationExpanded, token: currentState.token, meals: currentState.meals, + portions: currentState.portions, + photos: currentState.photos, wakeupController: currentState.wakeupController, retireController: currentState.retireController, commentController: currentState.commentController, @@ -90,7 +92,6 @@ class DiaryEditBloc extends Bloc { carbsController: currentState.carbsController, selectedSatiety: currentState.selectedSatiety, selectedMood: currentState.selectedMood, - portions: currentState.portions, diaryDate: currentState.diaryDate, diaryId: currentState.diaryId, productQuantityController: @@ -108,6 +109,7 @@ class DiaryEditBloc extends Bloc { token: token, meals: diary['meals'], portions: diary['meals'][0]['portions'], + photos: diary['meals'][0]['photos'], wakeupController: toTime(diary['wakeup'] ?? "09:00"), retireController: toTime(diary['retire'] ?? "23:00"), mealTimeController: @@ -183,7 +185,6 @@ class DiaryEditBloc extends Bloc { final diary = jsonDecode(diaryResponse.body)['diary']; if (diary is Map) { - print(diary); final String diaryDate = diary['date'] ?? DateTime.now().toString(); // Продолжайте обработку данных дневника // Если текущее состояние - начальное, создаем новое загруженное состояние @@ -197,6 +198,7 @@ class DiaryEditBloc extends Bloc { token: token, meals: diary['meals'], portions: diary['meals'][0]['portions'], + photos: diary['meals'][0]['photos'], wakeupController: toTime(diary['wakeup'] ?? "09:00"), retireController: toTime(diary['retire'] ?? "23:00"), mealTimeController: @@ -216,6 +218,7 @@ class DiaryEditBloc extends Bloc { autocompleteList: [], diaryEditModelObj: state.diaryEditModelObj ?.copyWith(mealList: fillMealList())))); + print("Updated"); } catch (e) { print("Ошибка при создании нового состояния $e"); @@ -333,9 +336,8 @@ class DiaryEditBloc extends Bloc { currentState.copyWith(wakeupController: event.selectedTime); print("Wakeup time changed to ${event.selectedTime}"); emit(updatedState); // Передаем обновленное состояние в emit - } else { + } else print("Wakeup time change error"); - } } } @@ -365,17 +367,17 @@ class DiaryEditBloc extends Bloc { final wakeupString = formatter.format(DateTime(2022, 1, 1, wakeup.hour, wakeup.minute)); final Map credentials = { - 'wakeup': wakeupString, + 'wakeUp': wakeupString, }; - var response = await http.post( - Uri.parse(url), - headers: { - 'Content-Type': 'application/json', - 'Authorization': 'Bearer $token', - }, - body: json.encode(credentials), - encoding: utf8, - ); + + var response = await http.post(Uri.parse(url), + headers: { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer $token', + }, + body: json.encode(credentials), + encoding: utf8); + return response; } @@ -446,6 +448,7 @@ class DiaryEditBloc extends Bloc { DiaryEditLoadedState updatedState = currentState.copyWith( currentMeal: event.value, portions: meal['portions'], + photos: meal['photos'], mealTimeController: toTime(meal['time'] ?? "1$id:00"), selectedMood: meal['mood'], selectedSatiety: meal['satiety']); @@ -655,13 +658,8 @@ class DiaryEditBloc extends Bloc { if (state is DiaryEditLoadedState) { var currentState = state as DiaryEditLoadedState; - String imageUrl = await uploadImageToServer( - event.image.path, - currentState.diaryId, - currentState.currentMeal!.id!, - currentState.token); - - print(imageUrl); + await uploadImageToServer(event.image.path, currentState.diaryId, + currentState.currentMeal!.id!, currentState.token); } } @@ -673,7 +671,7 @@ class DiaryEditBloc extends Bloc { var request = http.MultipartRequest('POST', Uri.parse(url)); request.headers['Authorization'] = 'Bearer $token'; - request.files.add(await http.MultipartFile.fromPath('image', imagePath)); + request.files.add(await http.MultipartFile.fromPath('photo', imagePath)); var client = http.Client(); var streamedResponse = await client.send(request); @@ -681,6 +679,7 @@ class DiaryEditBloc extends Bloc { if (streamedResponse.statusCode == 200) { var response = await http.Response.fromStream(streamedResponse); var imageUrl = response.body; + print('Image successfully uploaded'); return imageUrl; } else { throw Exception( diff --git a/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_state.dart b/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_state.dart index 80930398..abaed360 100644 --- a/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_state.dart +++ b/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_state.dart @@ -24,6 +24,7 @@ class DiaryEditLoadedState extends DiaryEditState { this.token, this.meals, this.portions, + this.photos, this.autocompleteList, this.wakeupController, this.retireController, @@ -49,6 +50,7 @@ class DiaryEditLoadedState extends DiaryEditState { String? token; List? meals; List? portions; + List? photos; List? autocompleteList; SelectionPopupModel? currentMeal; TimeOfDay? wakeupController; @@ -73,6 +75,8 @@ class DiaryEditLoadedState extends DiaryEditState { diaryDate, token, meals, + portions, + photos, autocompleteList, wakeupController, retireController, @@ -99,6 +103,7 @@ class DiaryEditLoadedState extends DiaryEditState { String? token, List? meals, List? portions, + List? photos, List? autocompleteList, TimeOfDay? wakeupController, TimeOfDay? retireController, diff --git a/lib/presentation/diary/diary_edit_screen/diary_edit_screen.dart b/lib/presentation/diary/diary_edit_screen/diary_edit_screen.dart index 4b5ff998..87284d6c 100644 --- a/lib/presentation/diary/diary_edit_screen/diary_edit_screen.dart +++ b/lib/presentation/diary/diary_edit_screen/diary_edit_screen.dart @@ -394,6 +394,9 @@ class DiaryEditScreen extends StatelessWidget { SizedBox(height: 30.v), _buildAddPhoto(context), SizedBox(height: 30.v), + ...state.photos! + .map((photo) => _buildPhoto(context, photo)) + .toList(), Text( "Ваша сытость после приёма пищи", style: CustomTextStyles.bodyMediumBlack900, @@ -628,6 +631,58 @@ class DiaryEditScreen extends StatelessWidget { ); } + _buildP(BuildContext context, Map portion) { + return Container( + margin: EdgeInsets.symmetric(horizontal: 10.h), + padding: EdgeInsets.symmetric( + horizontal: 8.h, + vertical: 6.v, + ), + decoration: AppDecoration.fillGray.copyWith( + borderRadius: BorderRadiusStyle.roundedBorder5, + ), + child: Row( + mainAxisAlignment: MainAxisAlignment.end, + mainAxisSize: MainAxisSize.min, + children: [ + Padding( + padding: EdgeInsets.only( + left: 7.h, + top: 5.v, + ), + child: Text( + portion['product_name'].length > 8 + ? portion['product_name'].substring(0, 8) + : portion['product_name'], + style: CustomTextStyles.titleMedium16, + )), + Padding( + padding: EdgeInsets.only( + left: 20.h, + top: 3.v, + bottom: 3.v, + ), + child: Text( + portion['quantity'].toString() + + ' ' + + portion['portion'].toString(), + ), + ), + Spacer(), + GestureDetector( + onTap: removePortion(context, portion), + child: Padding( + padding: const EdgeInsets.only(right: 10), + child: Icon( + Icons.close, + color: appTheme.blueGray300, + ), + )), + ], + ), + ); + } + removePortion(BuildContext context, Map portion) { return () { context.read().add(RemovePortionEvent(portion['id'])); @@ -1114,9 +1169,7 @@ class DiaryEditScreen extends StatelessWidget { ); if (pickedTime != null && pickedTime != state.wakeupController) { state.wakeupController = pickedTime; - context.read().add(WakeupEvent( - pickedTime, - )); + context.read().add(WakeupEvent(pickedTime)); } }