forked from eatvio/eatvio_app
144 lines
4.8 KiB
Dart
144 lines
4.8 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:eatvio_app/core/app_router.dart';
|
|
import 'package:eatvio_app/core/permission_handler/permission_handler.dart';
|
|
import 'package:eatvio_app/features/create_recipe/presentation/bloc/create_recipe_cubit/create_recipe_cubit.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 ImageWidget extends StatefulWidget {
|
|
const ImageWidget({super.key, required this.image});
|
|
final File? image;
|
|
@override
|
|
State<ImageWidget> createState() => _ImageWidgetState();
|
|
}
|
|
|
|
class _ImageWidgetState extends State<ImageWidget> {
|
|
late final CreateRecipeCubit createRecipeCubit;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
createRecipeCubit = context.read<CreateRecipeCubit>();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return GestureDetector(
|
|
onTap: () {
|
|
_showImagePicker();
|
|
},
|
|
child: widget.image == null
|
|
? SvgPicture.asset(
|
|
width: MediaQuery.of(context).size.width - 32,
|
|
'assets/icons/upload_photo_of_portion.svg',
|
|
)
|
|
: Stack(
|
|
children: [
|
|
ClipRRect(
|
|
borderRadius: BorderRadius.circular(20),
|
|
child: Image.file(
|
|
File(widget.image!.path),
|
|
),
|
|
),
|
|
Positioned(
|
|
right: 16,
|
|
top: 16,
|
|
child: GestureDetector(
|
|
onTap: () {
|
|
createRecipeCubit.replaceImage(null);
|
|
},
|
|
child: Container(
|
|
padding: const EdgeInsets.all(4),
|
|
decoration: const BoxDecoration(
|
|
color: Colors.white,
|
|
shape: BoxShape.circle,
|
|
),
|
|
child: const Icon(Icons.close_rounded),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
void _showImagePicker() {
|
|
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 {
|
|
final picker = ImagePicker();
|
|
final image = await picker.pickImage(source: ImageSource.camera);
|
|
setState(() {});
|
|
if (image != null) {
|
|
createRecipeCubit.replaceImage(File(image.path));
|
|
createRecipeCubit.validateImage();
|
|
}
|
|
AppRouter.navigatorKey.currentContext?.pop();
|
|
},
|
|
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 {
|
|
final picker = ImagePicker();
|
|
final image = await picker.pickImage(source: ImageSource.gallery);
|
|
setState(() {});
|
|
if (image != null) {
|
|
createRecipeCubit.replaceImage(File(image.path));
|
|
createRecipeCubit.validateImage();
|
|
}
|
|
AppRouter.navigatorKey.currentContext?.pop();
|
|
},
|
|
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('Отмена'),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|