import 'dart:io'; 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'; typedef OnFile = void Function(File?); class LocalImagePicker extends StatefulWidget { final Color? color; final String title; final OnFile? onFile; final bool enabled; final String? initialImgUrl; final ImageSource imageSource; const LocalImagePicker( {Key? key, required this.title, this.onFile, this.enabled = true, this.initialImgUrl, this.imageSource = ImageSource.gallery, this.color}) : super(key: key); @override _LocalImagePickerState createState() => _LocalImagePickerState(); } class _LocalImagePickerState extends State { String? url; File? file; @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) { var selectedFile = await ImagePicker().pickImage( source: camera ? ImageSource.camera : ImageSource.gallery, imageQuality: 80, maxWidth: 1000); if (selectedFile != null) { setState(() { this.file = File(selectedFile.path); }); if (widget.onFile != null) { widget.onFile!(File(selectedFile.path)); } } } }, ) : 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) { widget.onFile!(null); } }); }, deleteIcon: Icon( Icons.close, color: widget.color ?? Colors.blue, ), label: Text(widget.title), ), ), ), ); } Future _dialog(BuildContext context, cameraPress(), photoPress()) { return showDialog( context: context, builder: (BuildContext context) { return AlertDialog( content: Container( child: Padding( padding: const EdgeInsets.all(8.0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ Column( mainAxisSize: MainAxisSize.min, children: [ IconButton( icon: Icon( FontAwesomeIcons.camera, size: 30, color: widget.color ?? Colors.blue, ), onPressed: () { Navigator.pop(context); cameraPress(); }), Text("Camera") ], ), Column( mainAxisSize: MainAxisSize.min, children: [ IconButton( icon: Icon( Icons.photo_library, size: 30, color: widget.color ?? Colors.blue, ), onPressed: () { Navigator.pop(context); photoPress(); }), Text("Gallery") ], ), ], ), ), ), ); }, ); } }