forked from eatvio/eatvio_app
feat(diary): Modal window for finishDay, event and bloc method forFinishDay. addPortion and removePortion
This commit is contained in:
@@ -32,6 +32,7 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
|
||||
on<AddComment>(_addComment); //пользователь нажимает "Добавить комментарий"
|
||||
on<ProductInput>(
|
||||
_inputProduct); //пользователь нажимает "Добавить комментарий"
|
||||
on<FinishDay>(_finishDay); //пользователь нажимает "Добавить комментарий"
|
||||
}
|
||||
|
||||
Future<void> _onInitialize(
|
||||
@@ -394,19 +395,12 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
|
||||
currentState.currentMeal!.id,
|
||||
currentState.diaryId);
|
||||
if (response.statusCode == 200) {
|
||||
dynamic responseData = jsonDecode(response.body);
|
||||
var newPortion = responseData['portion']
|
||||
as int; // Assuming the portion quantity is an integer
|
||||
|
||||
// Ensure meals and id are correctly accessed and handle null values
|
||||
var currentPortions =
|
||||
currentState.meals?[id ?? 0]['portions'] as int ?? 0;
|
||||
|
||||
dynamic newPortion = jsonDecode(response.body)['portion'];
|
||||
print(newPortion);
|
||||
var updatedState = currentState.copyWith(
|
||||
portions: currentPortions + newPortion,
|
||||
portions: currentState.meals?[id ?? 0]['portions'], // + newPortion,
|
||||
);
|
||||
|
||||
emit(updatedState);
|
||||
emit(updatedState); // Передаем обновленное состояние в emit
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -472,7 +466,83 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
|
||||
}
|
||||
|
||||
FutureOr<void> _removePortion(
|
||||
RemovePortionEvent event, Emitter<DiaryEditState> emit) {}
|
||||
RemovePortionEvent event, Emitter<DiaryEditState> emit) async {
|
||||
if (state is DiaryEditLoadedState) {
|
||||
var currentState = state as DiaryEditLoadedState;
|
||||
|
||||
int? id = currentState.currentMeal?.id;
|
||||
|
||||
// Отправляем данные по HTTP маршруту и ждем ответ
|
||||
var response = await sendRemovePortion(
|
||||
currentState.token, event.portionId, id, currentState.diaryId);
|
||||
if (response.statusCode == 200) {
|
||||
print("removed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<http.Response> sendRemovePortion(
|
||||
String? token,
|
||||
int? portionID,
|
||||
int? mealID,
|
||||
int diaryID,
|
||||
) async {
|
||||
String url = dotenv.env['SERVER_ROOT']! +
|
||||
'/diary/$diaryID/meal/$mealID/portions/$portionID/delete';
|
||||
|
||||
var response = await http.delete(Uri.parse(url),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer $token',
|
||||
},
|
||||
encoding: utf8);
|
||||
print(response.body);
|
||||
return response;
|
||||
}
|
||||
|
||||
Future<FutureOr<void>> _finishDay(
|
||||
FinishDay event, Emitter<DiaryEditState> emit) async {
|
||||
print("imageUrl");
|
||||
if (state is DiaryEditLoadedState) {
|
||||
var currentState = state as DiaryEditLoadedState;
|
||||
|
||||
// Отправляем данные по HTTP маршруту и ждем ответ
|
||||
var response = await sendFinishDay(
|
||||
currentState.token,
|
||||
currentState.diaryId,
|
||||
currentState.wakeupController,
|
||||
currentState.retireController,
|
||||
currentState.commentController?.text,
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
print("finished");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<http.Response> sendFinishDay(
|
||||
String? token,
|
||||
int diaryID,
|
||||
TimeOfDay? wakeup,
|
||||
TimeOfDay? retire,
|
||||
String? comment,
|
||||
) async {
|
||||
final Map<String, dynamic> credentials = {
|
||||
'wakeup': wakeup,
|
||||
'retire': retire,
|
||||
'comment': comment,
|
||||
};
|
||||
String url = dotenv.env['SERVER_ROOT']! + '/diary/$diaryID/finishDay';
|
||||
var response = await http.post(Uri.parse(url),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer $token',
|
||||
},
|
||||
body: json.encode(credentials),
|
||||
encoding: utf8);
|
||||
print(response.body);
|
||||
return response;
|
||||
}
|
||||
|
||||
Future<FutureOr<void>> _uploadImages(
|
||||
UploadImages event, Emitter<DiaryEditState> emit) async {
|
||||
|
||||
@@ -113,3 +113,10 @@ class ChangeMeal extends DiaryEditEvent {
|
||||
@override
|
||||
List<Object?> get props => [value, meals];
|
||||
}
|
||||
|
||||
class FinishDay extends DiaryEditEvent {
|
||||
FinishDay();
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
@@ -900,10 +900,49 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.transparent,
|
||||
), // Установите прозрачный фон
|
||||
),
|
||||
onPressed: () => onFinishDayPressed(context),
|
||||
// Установите прозрачный фон
|
||||
);
|
||||
}
|
||||
|
||||
Function onFinishDayPressed(BuildContext context) {
|
||||
return () {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext dialogContext) {
|
||||
return AlertDialog(
|
||||
title: Text("Подтвердите действие"),
|
||||
content: Text("Вы уверены, что хотите завершить день?"),
|
||||
actions: <Widget>[
|
||||
TextButton(
|
||||
child: Text("Отмена"),
|
||||
onPressed: () => closeDialog(dialogContext),
|
||||
),
|
||||
TextButton(
|
||||
child: Text("Подтвердить"),
|
||||
onPressed: () => sendFinishDayData(dialogContext, context),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
Function sendFinishDayData(BuildContext dialogContext, BuildContext context) {
|
||||
return () {
|
||||
closeDialog(dialogContext);
|
||||
context.read<DiaryEditBloc>().add(FinishDay());
|
||||
};
|
||||
}
|
||||
|
||||
Function closeDialog(BuildContext dialogContext) {
|
||||
return () {
|
||||
Navigator.of(dialogContext).pop();
|
||||
};
|
||||
}
|
||||
|
||||
_buildMood(BuildContext context, DiaryEditLoadedState state) {
|
||||
return Container(
|
||||
margin: EdgeInsets.symmetric(horizontal: 16.h),
|
||||
|
||||
Reference in New Issue
Block a user