forked from eatvio/eatvio_app
241 lines
9.8 KiB
Dart
241 lines
9.8 KiB
Dart
import 'dart:io';
|
||
|
||
import 'package:cached_network_image/cached_network_image.dart';
|
||
import 'package:eatvio_app/core/app_config.dart';
|
||
import 'package:eatvio_app/core/app_router.dart';
|
||
import 'package:eatvio_app/core/locator_service.dart';
|
||
import 'package:eatvio_app/core/permission_handler/permission_handler.dart';
|
||
import 'package:eatvio_app/core/theme/theme_helper.dart';
|
||
import 'package:eatvio_app/features/diary/entities/diary_day_entity.dart';
|
||
import 'package:eatvio_app/features/diary/presentation/bloc/diary_cubit.dart';
|
||
import 'package:eatvio_app/features/diary/presentation/bloc/diary_state.dart';
|
||
import 'package:eatvio_app/generated/l10n.dart';
|
||
import 'package:flutter/cupertino.dart';
|
||
import 'package:flutter/material.dart';
|
||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||
import 'package:flutter_svg/flutter_svg.dart';
|
||
import 'package:go_router/go_router.dart';
|
||
import 'package:image_picker/image_picker.dart';
|
||
import 'package:permission_handler/permission_handler.dart';
|
||
|
||
class PhotoGalleryWidget extends StatefulWidget {
|
||
const PhotoGalleryWidget({super.key});
|
||
|
||
@override
|
||
State<PhotoGalleryWidget> createState() => _PhotoGalleryWidgetState();
|
||
}
|
||
|
||
class _PhotoGalleryWidgetState extends State<PhotoGalleryWidget> {
|
||
late final DiaryCubit diaryCubit;
|
||
int currentSelectedPhotoIndex = 0;
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
diaryCubit = context.read<DiaryCubit>();
|
||
}
|
||
|
||
void showImagePicker(DiaryDayEntity diaryDay) {
|
||
showCupertinoModalPopup<void>(
|
||
context: context,
|
||
builder: (context) {
|
||
final s = S.of(context);
|
||
return CupertinoActionSheet(
|
||
actions: [
|
||
CupertinoActionSheetAction(
|
||
onPressed: () async {
|
||
PermissionHandler.checkPermission(
|
||
permission: Permission.camera,
|
||
title: s.allowAccessToCamera,
|
||
description: s.allowAccessToCameraDescription,
|
||
onGranted: () async {
|
||
AppRouter.navigatorKey.currentContext?.pop();
|
||
|
||
final picker = ImagePicker();
|
||
XFile? image = await picker.pickImage(source: ImageSource.camera);
|
||
if (image != null) {
|
||
diaryCubit.uploadPhotos(
|
||
files: [File(image.path)],
|
||
diaryId: diaryDay.id,
|
||
);
|
||
}
|
||
},
|
||
context: context,
|
||
);
|
||
},
|
||
child: const Row(
|
||
children: [
|
||
Icon(Icons.photo_camera),
|
||
SizedBox(width: 8),
|
||
Text('Фото'),
|
||
],
|
||
),
|
||
),
|
||
CupertinoActionSheetAction(
|
||
onPressed: () async {
|
||
PermissionHandler.checkPermission(
|
||
permission: Permission.photos,
|
||
title: s.allowAcessToGalery,
|
||
description: s.allowAcessToGaleryDescription,
|
||
onGranted: () async {
|
||
AppRouter.navigatorKey.currentContext?.pop();
|
||
final picker = ImagePicker();
|
||
List<XFile>? images = await picker.pickMultiImage();
|
||
if (images.isNotEmpty) {
|
||
diaryCubit.uploadPhotos(
|
||
files: images.map((f) => File(f.path)).toList(),
|
||
diaryId: diaryDay.id,
|
||
);
|
||
}
|
||
},
|
||
context: context,
|
||
);
|
||
},
|
||
child: const Row(
|
||
children: [
|
||
Icon(Icons.photo),
|
||
SizedBox(width: 8),
|
||
Text('Галерея'),
|
||
],
|
||
),
|
||
),
|
||
],
|
||
cancelButton: CupertinoActionSheetAction(
|
||
onPressed: () => Navigator.of(context).pop(),
|
||
isDestructiveAction: true,
|
||
child: const Text('Отмена'),
|
||
),
|
||
);
|
||
},
|
||
);
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return BlocBuilder<DiaryCubit, DiaryState>(
|
||
builder: (context, state) {
|
||
if (state is DiaryStateLoaded) {
|
||
int mealIndex = state.selectedMeal - 1;
|
||
final photos = state.diary.meals[mealIndex].photos;
|
||
// BUG-5 fix: сбросить индекс, если он вышел за пределы
|
||
// (например, после переключения на meal с меньшим числом фото)
|
||
if (currentSelectedPhotoIndex >= photos.length) {
|
||
currentSelectedPhotoIndex = 0;
|
||
}
|
||
if (photos.isEmpty && !state.isConsultantView && !state.isDiaryFinished && !state.isDiaryCommented) {
|
||
return GestureDetector(
|
||
onTap: () async {
|
||
showImagePicker(state.diary);
|
||
},
|
||
child: SvgPicture.asset(
|
||
width: MediaQuery.of(context).size.width - 32,
|
||
'assets/no_one_image_was_added.svg',
|
||
),
|
||
);
|
||
} else {
|
||
if (state.diary.meals[mealIndex].photos.isEmpty) {
|
||
return const SizedBox.shrink();
|
||
}
|
||
return Column(
|
||
mainAxisSize: MainAxisSize.min,
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: [
|
||
Stack(
|
||
alignment: Alignment.topRight,
|
||
children: [
|
||
ClipRRect(
|
||
borderRadius: BorderRadius.circular(12),
|
||
child: CachedNetworkImage(
|
||
imageUrl: '${sl<AppConfig>().baseUrl}/storage/${state.diary.meals[mealIndex].photos[currentSelectedPhotoIndex].path}',
|
||
),
|
||
),
|
||
if (!state.isConsultantView && !state.isDiaryFinished && !state.isDiaryCommented)
|
||
Padding(
|
||
padding: const EdgeInsets.all(12),
|
||
child: IconButton.filled(
|
||
style: IconButton.styleFrom(
|
||
backgroundColor: Colors.white,
|
||
elevation: 0,
|
||
),
|
||
onPressed: () async {
|
||
await diaryCubit.deletePhoto(
|
||
photo: state.diary.meals[mealIndex].photos[currentSelectedPhotoIndex],
|
||
diaryId: state.diary.id,
|
||
);
|
||
setState(() {
|
||
if (currentSelectedPhotoIndex == state.diary.meals[mealIndex].photos.length - 1 && currentSelectedPhotoIndex != 0) {
|
||
currentSelectedPhotoIndex = state.diary.meals[mealIndex].photos.length - 2;
|
||
}
|
||
});
|
||
},
|
||
icon: const Icon(
|
||
Icons.close_rounded,
|
||
color: Colors.grey,
|
||
),
|
||
),
|
||
),
|
||
],
|
||
),
|
||
const SizedBox(height: 8),
|
||
SingleChildScrollView(
|
||
physics: const BouncingScrollPhysics(),
|
||
scrollDirection: Axis.horizontal,
|
||
child: Row(
|
||
mainAxisSize: MainAxisSize.min,
|
||
mainAxisAlignment: MainAxisAlignment.start,
|
||
children: [
|
||
for (int i = 0; i < state.diary.meals[mealIndex].photos.length + 1; i++) ...[
|
||
if (i == state.diary.meals[mealIndex].photos.length && !state.isConsultantView && !state.isDiaryFinished && !state.isDiaryCommented)
|
||
Padding(
|
||
padding: const EdgeInsets.only(left: 8),
|
||
child: InkWell(
|
||
onTap: () => showImagePicker(state.diary),
|
||
borderRadius: BorderRadius.circular(8),
|
||
child: Ink(
|
||
height: 52,
|
||
width: 52,
|
||
decoration: BoxDecoration(
|
||
borderRadius: BorderRadius.circular(8),
|
||
color: const Color(0xFFF1F2F6),
|
||
),
|
||
child: Icon(
|
||
Icons.add_rounded,
|
||
color: appTheme.blueA400,
|
||
),
|
||
),
|
||
),
|
||
),
|
||
if (i < state.diary.meals[mealIndex].photos.length)
|
||
GestureDetector(
|
||
onTap: () {
|
||
setState(() {
|
||
currentSelectedPhotoIndex = i;
|
||
});
|
||
},
|
||
child: ClipRRect(
|
||
borderRadius: BorderRadius.circular(8),
|
||
child: SizedBox(
|
||
height: 52,
|
||
width: 52,
|
||
child: CachedNetworkImage(
|
||
imageUrl: '${sl<AppConfig>().baseUrl}/storage/${state.diary.meals[mealIndex].photos[i].path}',
|
||
fit: BoxFit.fitWidth,
|
||
),
|
||
),
|
||
),
|
||
),
|
||
if (i < state.diary.meals[mealIndex].photos.length - 1) const SizedBox(width: 8),
|
||
],
|
||
],
|
||
),
|
||
),
|
||
],
|
||
);
|
||
}
|
||
}
|
||
return const SizedBox.shrink();
|
||
},
|
||
);
|
||
}
|
||
}
|