Send settings data to server

This commit is contained in:
2024-02-01 11:35:01 +03:00
parent c846a0c8ca
commit cc4cf40b19

View File

@@ -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<NotificationSwitch> {
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<HyperSwitch> {
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<HyperSwitch> {
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<HypoSwitch> {
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<ExpandableList> {
state!.selectedCountry = newValue;
});
}
Future<void> sendProfileData(MyProfileLoadedState? state) async {
try {
// Создаем JSON объект с данными профиля
Map<String, dynamic> 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');
}
}
}