feat(diary): Image remove layout and API method

This commit is contained in:
2024-04-01 17:21:26 +03:00
parent ce19460af3
commit a2998c1fc7
3 changed files with 92 additions and 13 deletions

View File

@@ -26,8 +26,9 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
on<ToggleMoodExpandedEvent>(_toggleMood);
on<ToggleWakeupExpandedEvent>(_toggleWakeup);
on<AddPortionEvent>(_addPortion); //пользователь нажимает "Добавить продукт"
on<RemovePortionEvent>(
_removePortion); // пользователь нажимает на крестик около продукта
on<RemovePortionEvent>(_removePortion);
on<RemoveImageEvent>(
_removeImage); // пользователь нажимает на крестик около продукта
on<UploadImages>(_uploadImages); //пользователь нажимает "Добавить фото"
on<AddComment>(_addComment); //пользователь нажимает "Добавить комментарий"
on<ProductInput>(
@@ -469,7 +470,6 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
DiaryEditLoadedState updatedState = currentState.copyWith(
portionController: event.value,
);
print(updatedState.portionController?.title);
emit(updatedState);
}
}
@@ -508,8 +508,6 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
currentState.diaryId,
);
print(response.body);
if (response.statusCode == 200) {
await reloadScreen(event, emit);
} else {
@@ -624,6 +622,49 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
return response;
}
FutureOr<void> _removeImage(
RemoveImageEvent event, Emitter<DiaryEditState> emit) async {
if (state is DiaryEditLoadedState) {
var currentState = state as DiaryEditLoadedState;
var response = await sendRemovePhoto(
currentState.token,
event.path,
event.photoID,
currentState.currentMeal?.id,
currentState.diaryId,
);
print(response.statusCode);
if (response.statusCode == 204) {
await reloadScreen(event, emit);
} else {
print(
'Failed to remove photo. Status Code: ${response.statusCode} ${response.body}');
}
}
}
Future<http.Response> sendRemovePhoto(String? token, String? path,
String? photoID, int? mealID, int diaryID) async {
String url =
'${dotenv.env['SERVER_ROOT']!}/diary/$diaryID/meal/$mealID/photo/delete';
final Map<String, dynamic> credentials = {
'id': photoID,
'path': path,
};
var response = await http.delete(
Uri.parse(url),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $token',
},
body: json.encode(credentials),
encoding: utf8,
);
return response;
}
Future<void> _finishDay(FinishDay event, Emitter<DiaryEditState> emit) async {
if (state is DiaryEditLoadedState) {
var currentState = state as DiaryEditLoadedState;

View File

@@ -98,6 +98,14 @@ class UploadImages extends DiaryEditEvent {
List<Object> get props => [image];
}
class RemoveImageEvent extends DiaryEditEvent {
RemoveImageEvent(this.photoID, this.path);
final String photoID;
final String path;
@override
List<Object> get props => [photoID, path];
}
class ProductInput extends DiaryEditEvent {
ProductInput(this.input);
final String input;

View File

@@ -653,14 +653,36 @@ class DiaryEditScreen extends StatelessWidget {
}
_buildPhoto(BuildContext context, Map<String, dynamic> photo) {
return Container(
margin: EdgeInsets.symmetric(horizontal: 10.h, vertical: 10.h),
decoration: AppDecoration.fillGray.copyWith(
borderRadius: BorderRadiusStyle.roundedBorder5,
),
child: Image.network(
dotenv.env['STORAGE_URL']! + photo['path'],
),
return Stack(
children: [
Container(
margin: EdgeInsets.symmetric(horizontal: 10.h, vertical: 10.h),
decoration: AppDecoration.fillGray.copyWith(
borderRadius: BorderRadiusStyle.roundedBorder5,
),
child: Image.network(
dotenv.env['STORAGE_URL']! + photo['path'],
),
),
Positioned(
top: 5,
right: 5,
child: GestureDetector(
onTap: removePhoto(context, photo),
child: Container(
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.transparent,
),
child: Icon(
Icons.close,
color: appTheme.blueGray300,
),
),
),
),
],
);
}
@@ -670,6 +692,14 @@ class DiaryEditScreen extends StatelessWidget {
};
}
removePhoto(BuildContext context, Map<String, dynamic> photo) {
return () {
context
.read<DiaryEditBloc>()
.add(RemoveImageEvent(photo['id'].toString(), photo['path']));
};
}
_buildNutritionChart(BuildContext context) {
final List<double> recommendedValues = [
2000,