201 lines
6.0 KiB
Dart
201 lines
6.0 KiB
Dart
|
|
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 'package:fcs/widget/multi_img_controller.dart';
|
||
|
|
import 'package:fcs/widget/show_img.dart';
|
||
|
|
|
||
|
|
typedef OnFile = void Function(File);
|
||
|
|
|
||
|
|
class MultiImageFile extends StatefulWidget {
|
||
|
|
final String title;
|
||
|
|
final bool enabled;
|
||
|
|
final ImageSource imageSource;
|
||
|
|
final MultiImgController controller;
|
||
|
|
|
||
|
|
const MultiImageFile(
|
||
|
|
{Key key,
|
||
|
|
this.title,
|
||
|
|
this.enabled = true,
|
||
|
|
this.controller,
|
||
|
|
this.imageSource = ImageSource.gallery})
|
||
|
|
: super(key: key);
|
||
|
|
@override
|
||
|
|
_MultiImageFileState createState() => _MultiImageFileState();
|
||
|
|
}
|
||
|
|
|
||
|
|
class _MultiImageFileState extends State<MultiImageFile> {
|
||
|
|
List<FileContainer> fileContainers = [];
|
||
|
|
@override
|
||
|
|
void initState() {
|
||
|
|
super.initState();
|
||
|
|
fileContainers = widget.controller.fileContainers;
|
||
|
|
widget.controller.onChange(() {
|
||
|
|
setState(() {
|
||
|
|
this.fileContainers = widget.controller.fileContainers;
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
Widget build(BuildContext context) {
|
||
|
|
return new Column(
|
||
|
|
children: new List.generate(
|
||
|
|
widget.enabled ? fileContainers.length : fileContainers.length - 1,
|
||
|
|
(i) => getFile(fileContainers[i], i)).toList(),
|
||
|
|
);
|
||
|
|
|
||
|
|
// return Container(
|
||
|
|
// height: 30,
|
||
|
|
// width: 100,
|
||
|
|
// child: ListView.builder(
|
||
|
|
// itemCount: fileContainers.length,
|
||
|
|
// itemBuilder: (context, i) {
|
||
|
|
// return getFile(fileContainers[i]);
|
||
|
|
// },
|
||
|
|
// ),
|
||
|
|
// );
|
||
|
|
}
|
||
|
|
|
||
|
|
_fileAdded(FileContainer fileContainer, File selectedFile) {
|
||
|
|
fileContainer.file = selectedFile;
|
||
|
|
setState(() {
|
||
|
|
widget.controller.addFile = fileContainer;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
_fileRemove(FileContainer fileContainer) {
|
||
|
|
setState(() {
|
||
|
|
widget.controller.removeFile = fileContainer;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
Widget getFile(FileContainer fileContainer, int index) {
|
||
|
|
return Container(
|
||
|
|
height: 40,
|
||
|
|
padding: EdgeInsets.only(top: 5, bottom: 5),
|
||
|
|
child: fileContainer.file == null && fileContainer.url == null
|
||
|
|
? IconButton(
|
||
|
|
padding: const EdgeInsets.all(3.0),
|
||
|
|
icon: Icon(Icons.attach_file),
|
||
|
|
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) {
|
||
|
|
_fileAdded(fileContainer, selectedFile);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
},
|
||
|
|
)
|
||
|
|
: Padding(
|
||
|
|
padding: const EdgeInsets.only(left: 3.0),
|
||
|
|
child: InkWell(
|
||
|
|
onTap: () => {
|
||
|
|
Navigator.push(
|
||
|
|
context,
|
||
|
|
MaterialPageRoute(
|
||
|
|
builder: (context) => ShowImage(
|
||
|
|
imageFile: fileContainer.file,
|
||
|
|
url: fileContainer.file == null
|
||
|
|
? fileContainer.url
|
||
|
|
: null,
|
||
|
|
fileName: widget.title)),
|
||
|
|
)
|
||
|
|
},
|
||
|
|
child: Chip(
|
||
|
|
avatar: Icon(Icons.image),
|
||
|
|
onDeleted: !widget.enabled
|
||
|
|
? null
|
||
|
|
: () {
|
||
|
|
_fileRemove(fileContainer);
|
||
|
|
},
|
||
|
|
deleteIcon: Icon(
|
||
|
|
Icons.close,
|
||
|
|
),
|
||
|
|
label: Text(widget.title + " - ${index + 1}"),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
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,
|
||
|
|
),
|
||
|
|
onPressed: () {
|
||
|
|
Navigator.pop(context);
|
||
|
|
cameraPress();
|
||
|
|
}),
|
||
|
|
Text("Camera")
|
||
|
|
],
|
||
|
|
),
|
||
|
|
Column(
|
||
|
|
mainAxisSize: MainAxisSize.min,
|
||
|
|
children: <Widget>[
|
||
|
|
IconButton(
|
||
|
|
icon: Icon(Icons.photo_library, size: 30),
|
||
|
|
onPressed: () {
|
||
|
|
Navigator.pop(context);
|
||
|
|
photoPress();
|
||
|
|
}),
|
||
|
|
Text("Gallery")
|
||
|
|
],
|
||
|
|
),
|
||
|
|
],
|
||
|
|
),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
);
|
||
|
|
},
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
class FileContainer {
|
||
|
|
String url;
|
||
|
|
File file;
|
||
|
|
FileContainer({this.url, this.file});
|
||
|
|
|
||
|
|
@override
|
||
|
|
bool operator ==(other) {
|
||
|
|
if (identical(this, other)) {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
return (other.file == this.file &&
|
||
|
|
(other.file != null || this.file != null)) ||
|
||
|
|
(other.url == this.url && (other.url != null || this.url != null));
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
int get hashCode {
|
||
|
|
int result = 17;
|
||
|
|
result = 37 * result + file.hashCode;
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
}
|