You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
HMG_Patient_App_New/lib/widgets/image_picker.dart

276 lines
9.3 KiB
Dart

7 months ago
import 'dart:convert';
import 'dart:io';
1 day ago
import 'package:easy_localization/easy_localization.dart';
7 months ago
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
1 day ago
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
7 months ago
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/attachment_options.dart';
import 'package:hmg_patient_app_new/widgets/bottom_sheet.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
1 day ago
import 'package:hmg_patient_app_new/widgets/common_bottom_sheet.dart';
7 months ago
import 'package:image_picker/image_picker.dart';
final ImagePicker picker = ImagePicker();
class ImageOptions {
static void showImageOptionsNew(BuildContext context, bool showFilesOption, Function(String, File) image) {
1 day ago
showCommonBottomSheetWithoutHeight(
7 months ago
context,
1 day ago
title: LocaleKeys.feedback.tr(),
7 months ago
child: AttachmentOptions(
showFilesOption: showFilesOption,
onCameraTap: () async {
if (Platform.isAndroid) {
cameraImageAndroid(image);
} else {
File _image = File((await ImagePicker.platform.pickImage(source: ImageSource.camera, imageQuality: 20))?.path ?? "");
// XFile? media = await picker.pickMedia();
String? fileName = _image.path;
var bytes = File(fileName!).readAsBytesSync();
String base64Encode = base64.encode(bytes);
if (base64Encode != null) {
image(base64Encode, _image);
}
}
},
onGalleryTap: () async {
if (Platform.isAndroid) {
galleryImageAndroid(image);
} else {
File _image = File((await picker.pickMedia())?.path ?? "");
String fileName = _image.path;
var bytes = File(fileName).readAsBytesSync();
String base64Encode = base64.encode(bytes);
if (base64Encode != null) {
image(base64Encode, _image);
}
}
},
onFilesTap: () async {
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: [
'jpg',
'jpeg ',
'pdf',
'txt',
'docx',
'doc',
'pptx',
'xlsx',
'png',
'rar',
'zip',
],
);
List<File> files = result!.paths.map((path) => File(path!)).toList();
image(result.files.first.path.toString(), files.first);
},
),
1 day ago
callBackFunc: () {},
isFullScreen: false,
7 months ago
);
1 day ago
// showMyBottomSheet(
// context,
// callBackFunc: () {},
// child: AttachmentOptions(
// showFilesOption: showFilesOption,
// onCameraTap: () async {
// if (Platform.isAndroid) {
// cameraImageAndroid(image);
// } else {
// File _image = File((await ImagePicker.platform.pickImage(source: ImageSource.camera, imageQuality: 20))?.path ?? "");
// // XFile? media = await picker.pickMedia();
// String? fileName = _image.path;
// var bytes = File(fileName!).readAsBytesSync();
// String base64Encode = base64.encode(bytes);
// if (base64Encode != null) {
// image(base64Encode, _image);
// }
// }
// },
// onGalleryTap: () async {
// if (Platform.isAndroid) {
// galleryImageAndroid(image);
// } else {
// File _image = File((await picker.pickMedia())?.path ?? "");
// String fileName = _image.path;
// var bytes = File(fileName).readAsBytesSync();
// String base64Encode = base64.encode(bytes);
// if (base64Encode != null) {
// image(base64Encode, _image);
// }
// }
// },
// onFilesTap: () async {
// FilePickerResult? result = await FilePicker.platform.pickFiles(
// type: FileType.custom,
// allowedExtensions: [
// 'jpg',
// 'jpeg ',
// 'pdf',
// 'txt',
// 'docx',
// 'doc',
// 'pptx',
// 'xlsx',
// 'png',
// 'rar',
// 'zip',
// ],
// );
// List<File> files = result!.paths.map((path) => File(path!)).toList();
// image(result.files.first.path.toString(), files.first);
// },
// ),
// );
7 months ago
}
// static void showImageOptions(BuildContext context, Function(String, File) image) {
// showModalBottomSheet(
// backgroundColor: Colors.transparent,
// context: context,
// builder: (BuildContext bc) {
// return _BottomSheet(
// children: <Widget>[
// _BottomSheetItem(
// title: "Select File Source",
// onTap: () {},
// icon: Icons.file_present,
// color: MyColors.black,
// ),
// _BottomSheetItem(
// title: "Gallery",
// icon: Icons.image,
// onTap: () async {
// if (Platform.isAndroid) {
// galleryImageAndroid(image);
// } else {
// File _image = File((await ImagePicker.platform.pickImage(source: ImageSource.gallery, imageQuality: 10))?.path ?? "");
// String fileName = _image.path;
// var bytes = File(fileName).readAsBytesSync();
// String base64Encode = base64.encode(bytes);
// if (base64Encode != null) {
// image(base64Encode, _image);
// }
// }
// },
// ),
// _BottomSheetItem(
// title: "Camera",
// icon: Icons.camera_alt,
// onTap: () async {
// if (Platform.isAndroid) {
// cameraImageAndroid(image);
// } else {
// File _image = File((await ImagePicker.platform.pickImage(source: ImageSource.camera, imageQuality: 10))?.path ?? "");
// String fileName = _image.path;
// var bytes = File(fileName).readAsBytesSync();
// String base64Encode = base64.encode(bytes);
// if (base64Encode != null) {
// image(base64Encode, _image);
// }
// }
// },
// ),
// _BottomSheetItem(
// title: "Cancel",
// onTap: () {},
// icon: Icons.cancel,
// color: MyColors.redColor,
// )
// ],
// );
// });
// }
}
void galleryImageAndroid(Function(String, File) image) async {
File _image = File((await picker.pickMedia())?.path ?? "");
String fileName = _image.path;
var bytes = File(fileName).readAsBytesSync();
String base64Encode = base64.encode(bytes);
if (base64Encode != null) {
image(base64Encode, _image);
}
}
void cameraImageAndroid(Function(String, File) image) async {
File _image = File((await picker.pickMedia())?.path ?? "");
String fileName = _image.path;
var bytes = File(fileName).readAsBytesSync();
String base64Encode = base64.encode(bytes);
if (base64Encode != null) {
image(base64Encode, _image);
}
4 months ago
7 months ago
}
class _BottomSheet extends StatelessWidget {
final List<Widget> children;
const _BottomSheet({Key? key, required this.children}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.symmetric(vertical: 12.0),
decoration: BoxDecoration(color: Theme.of(context).scaffoldBackgroundColor, borderRadius: const BorderRadius.only(topLeft: Radius.circular(16.0), topRight: Radius.circular(16.0))),
child: SafeArea(
top: false,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
decoration: BoxDecoration(color: Theme.of(context).dividerColor, borderRadius: BorderRadius.circular(3.0)),
width: 40.0,
height: 6.0,
),
...children
],
),
),
);
}
}
class _BottomSheetItem extends StatelessWidget {
final Function onTap;
final IconData icon;
final String title;
final Color? color;
7 months ago
_BottomSheetItem({Key? key, required this.onTap, required this.title, required this.icon, this.color}) : super(key: key);
7 months ago
@override
Widget build(BuildContext context) {
final resolvedColor = color ?? AppColors.mainPurple;
7 months ago
return InkWell(
onTap: () {
if (onTap != null) {
Navigator.pop(context);
onTap();
}
},
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 18.0, vertical: 18.0),
child: Row(
children: <Widget>[
if (icon != null)
Icon(
icon,
color: resolvedColor,
7 months ago
size: 18.0,
),
if (icon != null) const SizedBox(width: 24.0),
title.toText17(),
],
),
),
);
}
}