217 lines
7.3 KiB
Dart
217 lines
7.3 KiB
Dart
import 'dart:io';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
class ImagePickerPopup {
|
|
ImagePickerPopup._();
|
|
|
|
static final _picker = ImagePicker();
|
|
|
|
static void showImagePickerDialog(
|
|
// BuildContext context,
|
|
Function(File? file) onFetchImage, {
|
|
double? maxWidth,
|
|
double? maxHeight,
|
|
}) {
|
|
_checkCameraPermission().then((permissionGranted) {
|
|
debugPrint("permission status: $permissionGranted");
|
|
if (permissionGranted) {
|
|
_showImageUploadBottomSheet(
|
|
onFetchImage,
|
|
maxWidth: maxWidth,
|
|
maxHeight: maxHeight,
|
|
);
|
|
} else {
|
|
ScaffoldMessenger.of(Get.context!).showSnackBar(
|
|
SnackBar(
|
|
content: const Text(
|
|
'Please allow permission to access Camera and photos.',
|
|
// style: textStyle16w400.copyWith(color: Colors.white),
|
|
),
|
|
duration: const Duration(seconds: 5),
|
|
backgroundColor: Colors.black,
|
|
action: SnackBarAction(
|
|
label: 'Setting', onPressed: () => openAppSettings()),
|
|
),
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
static Future<bool> _checkCameraPermission() async {
|
|
// 1. Checking Initial Permission Status:
|
|
const cameraPermission = Permission.camera;
|
|
|
|
final cameraPermissionStatus = await cameraPermission.status;
|
|
|
|
if (cameraPermissionStatus.isGranted) {
|
|
debugPrint("status: true");
|
|
return true;
|
|
} else {
|
|
Map permissionsResult = await [cameraPermission].request();
|
|
if (permissionsResult.values.isNotEmpty &&
|
|
permissionsResult[cameraPermission] == PermissionStatus.granted) {
|
|
debugPrint("status2: true");
|
|
return true;
|
|
} else {
|
|
debugPrint("status3: false");
|
|
return await cameraPermission.shouldShowRequestRationale;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void _showImageUploadBottomSheet(
|
|
// BuildContext context,
|
|
Function(File) onFetchImage, {
|
|
double? maxWidth,
|
|
double? maxHeight,
|
|
}) {
|
|
Get.bottomSheet(
|
|
Container(
|
|
padding: const EdgeInsets.all(20),
|
|
child: ListView(
|
|
shrinkWrap: true,
|
|
physics: const NeverScrollableScrollPhysics(),
|
|
children: <Widget>[
|
|
const Text(
|
|
"Upload Image",
|
|
// style: textStyle16w700.copyWith(color: Colors.black),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
const SizedBox(height: 5),
|
|
const Text(
|
|
"Click a Photo or upload your image from saved photos.",
|
|
// style: textStyle12w500.copyWith(color: Colors.black),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
const SizedBox(height: 20),
|
|
Row(
|
|
children: <Widget>[
|
|
const SizedBox(width: 30),
|
|
Expanded(
|
|
child: InkWell(
|
|
onTap: () {
|
|
Get.back();
|
|
// Navigator.of(context).pop();
|
|
getImageFromSource(
|
|
fromCamera: true,
|
|
onFetchImage: onFetchImage,
|
|
maxWidth: maxWidth,
|
|
maxHeight: maxHeight,
|
|
);
|
|
},
|
|
borderRadius:
|
|
const BorderRadius.all(Radius.circular(20.0)),
|
|
child: Container(
|
|
padding: const EdgeInsets.all(10.0),
|
|
child: Column(
|
|
children: <Widget>[
|
|
Icon(
|
|
CupertinoIcons.camera,
|
|
size: 50.r,
|
|
color: Get.theme.primaryColor,
|
|
),
|
|
const SizedBox(height: 8),
|
|
const Text(
|
|
"Camera",
|
|
// style: textStyle16w700.copyWith(
|
|
// color: Colors.black)
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
SizedBox(
|
|
width: 40.r,
|
|
height: 64.r,
|
|
child: const Center(
|
|
child: VerticalDivider(color: Colors.grey, width: 2),
|
|
),
|
|
),
|
|
Expanded(
|
|
child: InkWell(
|
|
onTap: () {
|
|
// Navigator.of(context).pop();
|
|
Get.back();
|
|
getImageFromSource(
|
|
fromCamera: false, onFetchImage: onFetchImage);
|
|
},
|
|
borderRadius:
|
|
const BorderRadius.all(Radius.circular(20.0)),
|
|
child: Container(
|
|
padding: const EdgeInsets.all(10.0),
|
|
child: Column(
|
|
children: <Widget>[
|
|
// SvgPicture.asset(
|
|
// Assets.svgPlaceholderImg2,
|
|
// width: 50,
|
|
// height: 50,
|
|
// ),
|
|
Icon(
|
|
CupertinoIcons.photo,
|
|
size: 50.r,
|
|
color: Get.theme.primaryColor,
|
|
),
|
|
const SizedBox(height: 8),
|
|
const Text(
|
|
"Gallery",
|
|
// style: textStyle16w700.copyWith(
|
|
// color: Colors.black)
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(
|
|
width: 30,
|
|
),
|
|
],
|
|
)
|
|
],
|
|
),
|
|
),
|
|
backgroundColor: Colors.white);
|
|
}
|
|
|
|
static void getImageFromSource({
|
|
required bool fromCamera,
|
|
required Function(File) onFetchImage,
|
|
double? maxWidth,
|
|
double? maxHeight,
|
|
}) async {
|
|
final permissionGranted = await _checkCameraPermission();
|
|
debugPrint("permission status: $permissionGranted");
|
|
|
|
if (permissionGranted) {
|
|
final pickedFile = await _picker.pickImage(
|
|
source: fromCamera ? ImageSource.camera : ImageSource.gallery,
|
|
imageQuality: 80,
|
|
maxWidth: maxWidth,
|
|
maxHeight: maxHeight,
|
|
);
|
|
if (pickedFile != null) {
|
|
final picture = File(pickedFile.path);
|
|
onFetchImage(picture);
|
|
}
|
|
} else {
|
|
ScaffoldMessenger.of(Get.context!).showSnackBar(
|
|
SnackBar(
|
|
content: const Text(
|
|
'Please allow permission to access Camera and photos.',
|
|
),
|
|
duration: const Duration(seconds: 5),
|
|
backgroundColor: Colors.black,
|
|
action: SnackBarAction(
|
|
label: 'Setting', onPressed: () => openAppSettings()),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
}
|