forked from eatvio/eatvio_app
203 lines
7.7 KiB
Dart
203 lines
7.7 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
|
import 'package:eatvio_app/core/const.dart';
|
|
import 'package:eatvio_app/core/theme/theme_helper.dart';
|
|
import 'package:eatvio_app/features/diary/models/diary_day_model.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:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:flutter_svg/flutter_svg.dart';
|
|
import 'package:image_picker/image_picker.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>();
|
|
}
|
|
|
|
showImagePicker(DiaryDayModel diaryDay) {
|
|
showCupertinoModalPopup<void>(
|
|
context: context,
|
|
builder: (context) => CupertinoActionSheet(
|
|
actions: [
|
|
CupertinoActionSheetAction(
|
|
onPressed: () async {
|
|
Navigator.of(context).pop();
|
|
final picker = ImagePicker();
|
|
XFile? image = await picker.pickImage(source: ImageSource.camera);
|
|
if (image != null) {
|
|
diaryCubit.uploadPhotos(
|
|
files: [File(image.path)],
|
|
diaryId: diaryDay.id,
|
|
);
|
|
}
|
|
},
|
|
child: const Row(
|
|
children: [
|
|
Icon(Icons.photo_camera),
|
|
SizedBox(width: 8),
|
|
Text('Фото'),
|
|
],
|
|
),
|
|
),
|
|
CupertinoActionSheetAction(
|
|
onPressed: () async {
|
|
Navigator.of(context).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,
|
|
);
|
|
}
|
|
},
|
|
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;
|
|
if (state.diary.meals[mealIndex].photos.isEmpty) {
|
|
return GestureDetector(
|
|
onTap: () => showImagePicker(state.diary),
|
|
child: SvgPicture.asset(
|
|
width: MediaQuery.of(context).size.width - 32,
|
|
'assets/no_one_image_was_added.svg',
|
|
),
|
|
);
|
|
} else {
|
|
return Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Stack(
|
|
alignment: Alignment.topRight,
|
|
children: [
|
|
ClipRRect(
|
|
borderRadius: BorderRadius.circular(12),
|
|
child: CachedNetworkImage(
|
|
imageUrl: '$baseUrl/storage/${state.diary.meals[mealIndex].photos[currentSelectedPhotoIndex].path}',
|
|
),
|
|
),
|
|
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)
|
|
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: '$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();
|
|
},
|
|
);
|
|
}
|
|
}
|