forked from eatvio/eatvio_app
feat(diary) Image upload package and logic
This commit is contained in:
@@ -3,6 +3,7 @@ import 'dart:async';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '/core/app_export.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:eatvio_app/presentation/diary/diary_edit_screen/models/diary_edit_model.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'dart:convert';
|
||||
@@ -25,6 +26,7 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
|
||||
on<AddPortionEvent>(_addPortion); //пользователь нажимает "Добавить продукт"
|
||||
on<RemovePortionEvent>(
|
||||
_removePortion); // пользователь нажимает на крестик около продукта
|
||||
on<UploadImages>(_uploadImages); //пользователь нажимает "Добавить продукт"
|
||||
}
|
||||
|
||||
Future<void> _onInitialize(
|
||||
@@ -169,4 +171,27 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
|
||||
|
||||
FutureOr<void> _removePortion(
|
||||
RemovePortionEvent event, Emitter<DiaryEditState> emit) {}
|
||||
|
||||
Future<FutureOr<void>> _uploadImages(
|
||||
UploadImages event, Emitter<DiaryEditState> emit) async {
|
||||
String imageUrl = await uploadImageToServer(event.image.path);
|
||||
print(imageUrl);
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> uploadImageToServer(String imagePath) async {
|
||||
var request = http.MultipartRequest(
|
||||
'POST',
|
||||
Uri.parse('http://your-api-url.com/upload'), // Замените на ваш маршрут API
|
||||
);
|
||||
|
||||
request.files.add(await http.MultipartFile.fromPath('image', imagePath));
|
||||
|
||||
var response = await request.send();
|
||||
if (response.statusCode == 200) {
|
||||
var responseData = await response.stream.bytesToString();
|
||||
return responseData;
|
||||
} else {
|
||||
throw Exception('Ошибка при загрузке изображения');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,3 +73,10 @@ class RemovePortionEvent extends DiaryEditEvent {
|
||||
@override
|
||||
List<Object> get props => [portionId];
|
||||
}
|
||||
|
||||
class UploadImages extends DiaryEditEvent {
|
||||
UploadImages(this.image);
|
||||
final XFile image;
|
||||
@override
|
||||
List<Object> get props => [image];
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import 'models/diary_edit_model.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_form_builder/flutter_form_builder.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:eatvio_app/core/app_export.dart';
|
||||
import 'package:eatvio_app/widgets/custom_edit_text_form_field.dart';
|
||||
import 'package:eatvio_app/widgets/custom_edit_drop_down.dart';
|
||||
@@ -162,19 +163,47 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
|
||||
/// Section Widget
|
||||
Widget _buildAddPhoto(BuildContext context) {
|
||||
return CustomOutlinedButton(
|
||||
width: 195.h,
|
||||
text: "Добавить фото",
|
||||
rightIcon: Container(
|
||||
margin: EdgeInsets.only(left: 14.h),
|
||||
child: CustomImageView(
|
||||
imagePath: ImageConstant.imgVector,
|
||||
height: 30.v,
|
||||
width: 34.h,
|
||||
return GestureDetector(
|
||||
onTap: () async {
|
||||
final picker = ImagePicker();
|
||||
List<XFile>? images = await picker.pickMultiImage();
|
||||
if (images.isNotEmpty) {
|
||||
for (var image in images) {
|
||||
context.read<DiaryEditBloc>().add(UploadImages(image));
|
||||
}
|
||||
}
|
||||
},
|
||||
child: Container(
|
||||
margin: EdgeInsets.symmetric(horizontal: 10.h),
|
||||
padding: EdgeInsets.symmetric(
|
||||
horizontal: 40.h,
|
||||
vertical: 80.v,
|
||||
),
|
||||
decoration: AppDecoration.fillGray.copyWith(
|
||||
borderRadius: BorderRadiusStyle.roundedBorder5,
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Padding(
|
||||
padding: EdgeInsets.only(
|
||||
left: 7.h,
|
||||
top: 5.v,
|
||||
),
|
||||
child: Text(
|
||||
"Добавить фото",
|
||||
selectionColor: appTheme.blueA400,
|
||||
),
|
||||
),
|
||||
Spacer(),
|
||||
Icon(
|
||||
Icons.photo_library,
|
||||
color: appTheme.blueGray300,
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
|
||||
buttonTextStyle: theme.textTheme.bodyMedium!,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -277,6 +306,7 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
_buildProduct(context),
|
||||
SizedBox(height: 30.v),
|
||||
_buildAddPhoto(context),
|
||||
SizedBox(height: 30.v),
|
||||
Text(
|
||||
"Ваша сытость после приёма пищи",
|
||||
style: CustomTextStyles.bodyMediumBlack900,
|
||||
@@ -365,7 +395,7 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
top: 5.v,
|
||||
),
|
||||
child: Text(
|
||||
"Omelette",
|
||||
"Сырный суп",
|
||||
style: CustomTextStyles.titleMedium16,
|
||||
),
|
||||
),
|
||||
@@ -377,21 +407,16 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
),
|
||||
child: Text(
|
||||
"15гр",
|
||||
style: CustomTextStyles.titleMedium16,
|
||||
),
|
||||
),
|
||||
Spacer(),
|
||||
CustomImageView(
|
||||
imagePath: ImageConstant.imgSearch,
|
||||
height: 24.adaptSize,
|
||||
width: 24.adaptSize,
|
||||
),
|
||||
CustomImageView(
|
||||
imagePath: ImageConstant.imgClose,
|
||||
height: 28.adaptSize,
|
||||
width: 28.adaptSize,
|
||||
margin: EdgeInsets.only(left: 10.h),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 10),
|
||||
child: Icon(
|
||||
Icons.close,
|
||||
color: appTheme.blueGray300,
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
@@ -8,7 +8,7 @@ class CustomButtonStyles {
|
||||
backgroundColor: appTheme.blueA400,
|
||||
side: BorderSide(
|
||||
color: theme.colorScheme.primary,
|
||||
width: 1,
|
||||
width: 0,
|
||||
),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(15.h),
|
||||
|
||||
@@ -99,18 +99,13 @@ class CustomDropDown extends StatelessWidget {
|
||||
);
|
||||
InputDecoration get decoration => InputDecoration(
|
||||
hintText: hintText ?? "",
|
||||
hintStyle: hintStyle,
|
||||
hintStyle: hintStyle ?? theme.textTheme.bodyLarge,
|
||||
prefixIcon: prefix,
|
||||
prefixIconConstraints: prefixConstraints,
|
||||
suffixIcon: suffix,
|
||||
suffixIconConstraints: suffixConstraints,
|
||||
isDense: true,
|
||||
contentPadding: contentPadding ??
|
||||
EdgeInsets.only(
|
||||
left: 17,
|
||||
top: 17,
|
||||
bottom: 17,
|
||||
),
|
||||
contentPadding: contentPadding ?? EdgeInsets.all(6.h),
|
||||
fillColor: fillColor,
|
||||
filled: filled,
|
||||
border: borderDecoration ??
|
||||
|
||||
@@ -24,6 +24,7 @@ dependencies:
|
||||
flutter_dotenv: ^5.1.0
|
||||
carousel_slider: ^4.2.1
|
||||
flutter_form_builder: ^9.2.1
|
||||
image_picker: ^1.0.7
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
||||
Reference in New Issue
Block a user