import 'dart:io'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; import 'package:fcs/widget/show_img.dart'; typedef OnFile = void Function(File); class SignFile extends StatefulWidget { final String title; final OnFile onFile; final bool enabled; final String initialImgUrl; final ImageSource imageSource; const SignFile( {Key key, this.title, this.onFile, this.enabled = true, this.initialImgUrl, this.imageSource = ImageSource.gallery}) : super(key: key); @override _SignFileState createState() => _SignFileState(); } class _SignFileState 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), onPressed: () async { if (!widget.enabled) return; var selectedFile = await ImagePicker.pickImage( source: widget.imageSource, imageQuality: 80, maxWidth: 1000); if (selectedFile != null) { setState(() { this.file = selectedFile; }); if (widget.onFile != null) { widget.onFile(selectedFile); } } }, ) : Padding( padding: const EdgeInsets.only(left: 3.0), child: Chip( avatar: Icon(Icons.image), onDeleted: !widget.enabled ? null : () { setState(() { this.file = null; this.url = null; if (widget.onFile != null) { widget.onFile(null); } }); }, deleteIcon: Icon( Icons.close, ), label: InkWell( onTap: () => { Navigator.push( context, MaterialPageRoute( builder: (context) => ShowImage( imageFile: file, url: file == null ? widget.initialImgUrl : null, fileName: widget.title)), ) }, child: Text(widget.title)), ), ), ); } }