From ce345be4a4f4fc9e3807c98983ed63736e60d97a Mon Sep 17 00:00:00 2001 From: maxxkim Date: Tue, 23 Jan 2024 15:06:03 +0300 Subject: [PATCH] Send circ to API --- .../my_profile_screen/my_profile_screen.dart | 57 +++++++++++++++++-- 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/lib/presentation/profile/my_profile_screen/my_profile_screen.dart b/lib/presentation/profile/my_profile_screen/my_profile_screen.dart index cb7c15fe..9bcea698 100644 --- a/lib/presentation/profile/my_profile_screen/my_profile_screen.dart +++ b/lib/presentation/profile/my_profile_screen/my_profile_screen.dart @@ -12,9 +12,11 @@ import 'package:carousel_slider/carousel_slider.dart'; import 'package:flutter/material.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:flutter/services.dart'; -import 'dart:math'; -import 'package:intl/intl.dart'; +import 'package:http/http.dart' as http; import 'package:fl_chart/fl_chart.dart'; +import 'package:intl/intl.dart'; +import 'dart:math'; +import 'dart:convert'; class MyProfileScreen extends StatelessWidget { static Widget builder(BuildContext context) { @@ -170,10 +172,53 @@ class MyProfileScreen extends StatelessWidget { .textTheme.bodyLarge), ), TextButton( - onPressed: () { - // Отправка данных на API - // Закрытие диалогового окна - Navigator.pop(context); + onPressed: () async { + Map data = { + "chest_circumference": state + .chestCircumferenceController! + .text, + "waist_circumference": state + .waistCircumferenceController! + .text, + "hip_circumference": state + .hipCircumferenceController! + .text, + }; + // Преобразование Map в JSON + String jsonData = + json.encode(data); + + // Отправка HTTP-запроса + print(jsonData); + try { + final response = + await http.post( + Uri.parse(dotenv.env[ + 'SERVER_ROOT']! + + '/profile/addCircumference'), + headers: { + 'Content-Type': + 'application/json', + }, + body: jsonData, + ); + + if (response.statusCode == + 200) { + // Успешный ответ от сервера + print( + "Данные успешно отправлены"); + } else { + // Ошибка от сервера + print( + "Ошибка при отправке данных. Код ответа: ${response.statusCode}"); + } + } catch (e) { + // Обработка ошибок при отправке запроса + print( + "Ошибка при отправке запроса: $e"); + } + //Navigator.pop(context); }, child: Text("Изменить", style: CustomTextStyles