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.
mohemm-flutter-app/lib/widgets/image_picker.dart

192 lines
6.5 KiB
Dart

import 'dart:convert';
import 'dart:io';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/extensions/string_extensions.dart';
import 'package:mohem_flutter_app/widgets/bottom_sheet.dart';
import 'package:mohem_flutter_app/widgets/bottom_sheets/attachment_options.dart';
class ImageOptions {
static void showImageOptionsNew(BuildContext context, Function(String, File) image) {
showMyBottomSheet(
context,
child: AttachmentOptions(
onCameraTap: () async {
if (Platform.isAndroid) {
cameraImageAndroid(image);
} else {
File _image = File((await ImagePicker.platform.pickImage(source: ImageSource.camera, imageQuality: 20))?.path ?? "");
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 ImagePicker.platform.pickImage(source: ImageSource.gallery, imageQuality: 20))?.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();
},
),
);
}
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: 20))?.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: 20))?.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 ImagePicker.platform.pickImage(source: ImageSource.gallery, imageQuality: 20))?.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 ImagePicker.platform.pickImage(source: ImageSource.camera, imageQuality: 20))?.path ?? "");
String fileName = _image.path;
var bytes = File(fileName).readAsBytesSync();
String base64Encode = base64.encode(bytes);
if (base64Encode != null) {
image(base64Encode, _image);
}
}
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).backgroundColor, 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;
_BottomSheetItem({Key? key, required this.onTap, required this.title, required this.icon, this.color = MyColors.gradiantStartColor}) : super(key: key);
@override
Widget build(BuildContext context) {
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: color,
size: 18.0,
),
if (icon != null) const SizedBox(width: 24.0),
title.toText17(),
],
),
),
);
}
}