Files
fcs/lib/pages/widgets/img_picker.dart

164 lines
5.2 KiB
Dart
Raw Normal View History

2020-10-28 05:11:06 +06:30
import 'dart:io';
2021-09-10 16:48:24 +06:30
import 'package:fcs/helpers/theme.dart';
2020-10-28 05:11:06 +06:30
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:image_picker/image_picker.dart';
import 'show_img.dart';
2021-09-10 16:48:24 +06:30
typedef OnFile = void Function(File?);
2020-10-28 05:11:06 +06:30
class LocalImagePicker extends StatefulWidget {
2021-09-10 16:48:24 +06:30
final Color? color;
2020-10-28 05:11:06 +06:30
final String title;
2021-09-10 16:48:24 +06:30
final OnFile? onFile;
2020-10-28 05:11:06 +06:30
final bool enabled;
2021-09-10 16:48:24 +06:30
final String? initialImgUrl;
2020-10-28 05:11:06 +06:30
final ImageSource imageSource;
const LocalImagePicker(
2021-09-10 16:48:24 +06:30
{Key? key,
required this.title,
2020-10-28 05:11:06 +06:30
this.onFile,
this.enabled = true,
this.initialImgUrl,
this.imageSource = ImageSource.gallery,
this.color})
: super(key: key);
@override
_LocalImagePickerState createState() => _LocalImagePickerState();
}
class _LocalImagePickerState extends State<LocalImagePicker> {
2021-09-10 16:48:24 +06:30
String? url;
File? file;
2020-10-28 05:11:06 +06:30
@override
void initState() {
super.initState();
this.url = widget.initialImgUrl == null || widget.initialImgUrl == ""
? null
: widget.initialImgUrl;
}
@override
Widget build(BuildContext context) {
return Container(
height: 30,
child: this.file == null && this.url == null
? IconButton(
padding: const EdgeInsets.all(3.0),
icon: Icon(Icons.attach_file, color: widget.color ?? Colors.blue),
onPressed: () async {
if (!widget.enabled) return;
bool camera = false, gallery = false;
await _dialog(
context, () => camera = true, () => gallery = true);
if (camera || gallery) {
2021-09-10 16:48:24 +06:30
var selectedFile = await ImagePicker().pickImage(
2020-10-28 05:11:06 +06:30
source: camera ? ImageSource.camera : ImageSource.gallery,
imageQuality: 80,
maxWidth: 1000);
if (selectedFile != null) {
setState(() {
2021-09-10 16:48:24 +06:30
this.file = File(selectedFile.path);
2020-10-28 05:11:06 +06:30
});
if (widget.onFile != null) {
2021-09-10 16:48:24 +06:30
widget.onFile!(File(selectedFile.path));
2020-10-28 05:11:06 +06:30
}
}
}
},
)
: InkWell(
onTap: () => {
Navigator.of(context).push(CupertinoPageRoute(
builder: (context) => ShowImage(
imageFile: file,
url: file == null ? widget.initialImgUrl : null,
fileName: widget.title))),
},
child: Padding(
padding: const EdgeInsets.only(left: 3.0),
child: Chip(
avatar: Icon(
Icons.image,
color: widget.color ?? Colors.blue,
),
onDeleted: !widget.enabled
? null
: () {
setState(() {
this.file = null;
this.url = null;
if (widget.onFile != null) {
2021-09-10 16:48:24 +06:30
widget.onFile!(null);
2020-10-28 05:11:06 +06:30
}
});
},
deleteIcon: Icon(
Icons.close,
color: widget.color ?? Colors.blue,
),
label: Text(widget.title),
),
),
),
);
}
Future<void> _dialog(BuildContext context, cameraPress(), photoPress()) {
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Container(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: Icon(
FontAwesomeIcons.camera,
size: 30,
color: widget.color ?? Colors.blue,
),
onPressed: () {
Navigator.pop(context);
cameraPress();
}),
Text("Camera")
],
),
Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: Icon(
Icons.photo_library,
size: 30,
color: widget.color ?? Colors.blue,
),
onPressed: () {
Navigator.pop(context);
photoPress();
}),
Text("Gallery")
],
),
],
),
),
),
);
},
);
}
}