From cc4cf40b196a988d53abbbdebb94132d85d0903b Mon Sep 17 00:00:00 2001 From: maxxkim Date: Thu, 1 Feb 2024 11:35:01 +0300 Subject: [PATCH] Send settings data to server --- .../my_profile_screen/my_settings_screen.dart | 65 +++++++++++++++++-- 1 file changed, 59 insertions(+), 6 deletions(-) diff --git a/lib/presentation/profile/my_profile_screen/my_settings_screen.dart b/lib/presentation/profile/my_profile_screen/my_settings_screen.dart index 753d5758..d03faece 100644 --- a/lib/presentation/profile/my_profile_screen/my_settings_screen.dart +++ b/lib/presentation/profile/my_profile_screen/my_settings_screen.dart @@ -1,5 +1,3 @@ -import 'dart:ffi'; - import 'bloc/my_profile_bloc.dart'; import 'models/my_profile_model.dart'; import 'package:eatvio_app/core/app_export.dart'; @@ -7,10 +5,12 @@ import 'package:eatvio_app/widgets/app_bar/appbar_subtitle.dart'; import 'package:eatvio_app/widgets/app_bar/appbar_leading_image.dart'; import 'package:eatvio_app/widgets/app_bar/custom_app_bar.dart'; import 'package:eatvio_app/widgets/custom_bottom_bar.dart'; -import 'package:eatvio_app/widgets/custom_text_form_field.dart'; import 'package:intl/intl.dart'; import 'package:eatvio_app/widgets/next_step_button.dart'; import 'package:flutter/material.dart'; +import 'dart:convert'; +import 'package:flutter_dotenv/flutter_dotenv.dart'; +import 'package:http/http.dart' as http; class MySettingsScreen extends StatelessWidget { static Widget builder(BuildContext context) { @@ -203,7 +203,7 @@ class _NotificationSwitchState extends State { class HyperSwitch extends StatefulWidget { final String title; - MyProfileLoadedState? state; + final MyProfileLoadedState? state; HyperSwitch(this.title, this.state); @override @@ -213,6 +213,14 @@ class HyperSwitch extends StatefulWidget { class _HyperState extends State { bool switchValue = false; + @override + void initState() { + super.initState(); + if (widget.state?.hyperFormController == '1') { + switchValue = true; + } + } + @override Widget build(BuildContext context) { return Row( @@ -242,8 +250,8 @@ class _HyperState extends State { class HypoSwitch extends StatefulWidget { final String title; - - HypoSwitch(this.title); + final MyProfileLoadedState? state; + HypoSwitch(this.title, this.state); @override _HypoState createState() => _HypoState(); @@ -252,6 +260,14 @@ class HypoSwitch extends StatefulWidget { class _HypoState extends State { bool switchValue = false; + @override + void initState() { + super.initState(); + if (widget.state?.hypoFormController == '1') { + switchValue = true; + } + } + @override Widget build(BuildContext context) { return Row( @@ -530,4 +546,41 @@ class _ExpandableListState extends State { state!.selectedCountry = newValue; }); } + + Future sendProfileData(MyProfileLoadedState? state) async { + try { + // Создаем JSON объект с данными профиля + Map profileData = { + 'weight': state?.weightController?.text, + 'chestCircumference': state?.chestCircumferenceController?.text, + 'waistCircumference': state?.waistCircumferenceController?.text, + 'hipCircumference': state?.hipCircumferenceController?.text, + 'firstName': state?.firstNameFormController?.text, + 'lastName': state?.lastNameFormController?.text, + 'country': state?.selectedCountry, + 'city': state?.selectedCity, + 'hypoFormController': state?.hypoFormController, + 'hyperFormController': state?.hyperFormController, + // добавьте остальные поля здесь + }; + + // Отправляем JSON на сервер + String apiUrl = dotenv.env['SERVER_ROOT']! + 'profile/edit'; + http.Response response = await http.post( + Uri.parse(apiUrl), + body: json.encode(profileData), + headers: {'Content-Type': 'application/json'}, + ); + + // Проверяем ответ от сервера + if (response.statusCode == 200) { + // Редирект + // ваш код для редиректа + } else { + throw Exception('Ошибка при отправке данных профиля'); + } + } catch (error) { + throw Exception('Ошибка при отправке данных профиля: $error'); + } + } }