2020-09-17 06:02:48 +06:30
|
|
|
import 'dart:io';
|
|
|
|
|
|
2020-09-18 21:33:41 +06:30
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
2020-10-07 02:33:06 +06:30
|
|
|
import 'package:fcs/helpers/theme.dart';
|
2020-11-09 05:53:25 +06:30
|
|
|
import 'package:fcs/pages/widgets/callbacks.dart';
|
2020-10-07 02:33:06 +06:30
|
|
|
import 'package:fcs/pages/widgets/right_left_page_rout.dart';
|
|
|
|
|
import 'package:fcs/pages/widgets/show_img.dart';
|
|
|
|
|
import 'package:fcs/pages/widgets/show_multiple_img.dart';
|
2020-09-17 06:02:48 +06:30
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
2021-09-10 14:25:37 +06:30
|
|
|
import 'package:flutter_vector_icons/flutter_vector_icons.dart';
|
2020-09-17 06:02:48 +06:30
|
|
|
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
|
|
|
|
import 'package:image_picker/image_picker.dart';
|
|
|
|
|
|
|
|
|
|
import 'display_image_source.dart';
|
|
|
|
|
import 'multi_img_controller.dart';
|
|
|
|
|
|
|
|
|
|
typedef OnFile = void Function(File);
|
|
|
|
|
|
|
|
|
|
class MultiImageFile extends StatefulWidget {
|
2021-09-10 14:25:37 +06:30
|
|
|
final String? title;
|
2020-09-17 06:02:48 +06:30
|
|
|
final bool enabled;
|
|
|
|
|
final ImageSource imageSource;
|
2021-09-10 14:25:37 +06:30
|
|
|
final MultiImgController? controller;
|
2020-09-17 06:02:48 +06:30
|
|
|
|
|
|
|
|
const MultiImageFile(
|
2021-09-10 14:25:37 +06:30
|
|
|
{Key? key,
|
2020-09-17 06:02:48 +06:30
|
|
|
this.title,
|
|
|
|
|
this.enabled = true,
|
|
|
|
|
this.controller,
|
|
|
|
|
this.imageSource = ImageSource.gallery})
|
|
|
|
|
: super(key: key);
|
|
|
|
|
@override
|
|
|
|
|
_MultiImageFileState createState() => _MultiImageFileState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _MultiImageFileState extends State<MultiImageFile> {
|
|
|
|
|
List<DisplayImageSource> fileContainers = [];
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
2021-09-10 14:25:37 +06:30
|
|
|
fileContainers = widget.controller!.fileContainers;
|
|
|
|
|
widget.controller!.onChange(() {
|
2020-09-17 06:02:48 +06:30
|
|
|
setState(() {
|
2021-09-10 14:25:37 +06:30
|
|
|
this.fileContainers = widget.controller!.fileContainers;
|
2020-09-17 06:02:48 +06:30
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2020-11-09 05:53:25 +06:30
|
|
|
return Column(
|
|
|
|
|
children: [
|
|
|
|
|
widget.enabled
|
|
|
|
|
? Row(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
|
children: [
|
|
|
|
|
InkWell(
|
|
|
|
|
onTap: () => {_openImagePicker(false)},
|
|
|
|
|
child: Stack(
|
|
|
|
|
children: [
|
|
|
|
|
Container(
|
|
|
|
|
width: 50,
|
|
|
|
|
height: 50,
|
|
|
|
|
child: Icon(
|
|
|
|
|
MaterialCommunityIcons.image,
|
|
|
|
|
color: primaryColor,
|
|
|
|
|
size: 35,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
Positioned(
|
|
|
|
|
right: 0,
|
|
|
|
|
top: 0,
|
|
|
|
|
child: actionIcon(
|
|
|
|
|
color: Colors.green, iconData: Icons.add))
|
|
|
|
|
],
|
2020-09-17 06:02:48 +06:30
|
|
|
),
|
|
|
|
|
),
|
2020-11-09 05:53:25 +06:30
|
|
|
InkWell(
|
|
|
|
|
onTap: () => {_openImagePicker(true)},
|
|
|
|
|
child: Stack(
|
|
|
|
|
children: [
|
|
|
|
|
Container(
|
|
|
|
|
width: 50,
|
|
|
|
|
height: 50,
|
|
|
|
|
child: Icon(
|
|
|
|
|
MaterialCommunityIcons.camera,
|
|
|
|
|
color: primaryColor,
|
|
|
|
|
size: 35,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
Positioned(
|
|
|
|
|
right: 0,
|
|
|
|
|
top: 0,
|
|
|
|
|
child: actionIcon(
|
|
|
|
|
color: Colors.green, iconData: Icons.add))
|
|
|
|
|
],
|
2020-09-17 06:02:48 +06:30
|
|
|
),
|
|
|
|
|
),
|
2020-11-09 05:53:25 +06:30
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
: Container(),
|
|
|
|
|
Container(
|
|
|
|
|
height: 100,
|
|
|
|
|
child: ListView.separated(
|
|
|
|
|
separatorBuilder: (context, index) => Divider(
|
|
|
|
|
color: Colors.black,
|
|
|
|
|
),
|
|
|
|
|
itemCount: fileContainers.length,
|
|
|
|
|
scrollDirection: Axis.horizontal,
|
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
|
return InkWell(
|
|
|
|
|
onTap: () => Navigator.push(
|
|
|
|
|
context,
|
|
|
|
|
RightLeftPageRoute(ShowMultiImage(
|
|
|
|
|
displayImageSources: fileContainers,
|
|
|
|
|
initialPage: index,
|
|
|
|
|
))),
|
|
|
|
|
child: Stack(alignment: Alignment.topLeft, children: <Widget>[
|
|
|
|
|
Padding(
|
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
|
child: Container(
|
|
|
|
|
width: 100,
|
|
|
|
|
height: 100,
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
border: Border.all(
|
|
|
|
|
color: primaryColor,
|
|
|
|
|
width: 1.0,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
child: fileContainers[index].file == null
|
|
|
|
|
? CachedNetworkImage(
|
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
|
width: 50,
|
|
|
|
|
height: 50,
|
2021-09-10 14:25:37 +06:30
|
|
|
imageUrl: fileContainers[index].url!,
|
2020-11-09 05:53:25 +06:30
|
|
|
placeholder: (context, url) => Column(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
SizedBox(
|
|
|
|
|
width: 30,
|
|
|
|
|
height: 30,
|
|
|
|
|
child: CircularProgressIndicator()),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
errorWidget: (context, url, error) =>
|
|
|
|
|
Icon(Icons.error),
|
|
|
|
|
)
|
2021-09-14 11:03:03 +06:30
|
|
|
: Image.file(
|
|
|
|
|
fileContainers[index].file!,
|
|
|
|
|
width: 50,
|
|
|
|
|
height: 50,
|
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
|
),
|
2020-11-09 05:53:25 +06:30
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
widget.enabled
|
|
|
|
|
? Positioned(
|
|
|
|
|
top: 10,
|
|
|
|
|
right: 0,
|
|
|
|
|
child: actionIcon(
|
|
|
|
|
color: Colors.red,
|
|
|
|
|
iconData: Icons.remove,
|
|
|
|
|
onTap: () =>
|
2020-09-17 06:02:48 +06:30
|
|
|
{_fileRemove(fileContainers[index])}),
|
2020-11-09 05:53:25 +06:30
|
|
|
)
|
|
|
|
|
: Container(),
|
|
|
|
|
]),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
2020-09-17 06:02:48 +06:30
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-09 05:53:25 +06:30
|
|
|
_openImagePicker(bool camera) async {
|
2024-01-09 13:11:22 +06:30
|
|
|
var selectedFile = await ImagePicker().pickImage(
|
2020-11-09 05:53:25 +06:30
|
|
|
source: camera ? ImageSource.camera : ImageSource.gallery,
|
|
|
|
|
imageQuality: 80,
|
|
|
|
|
maxWidth: 1000);
|
|
|
|
|
if (selectedFile != null) {
|
|
|
|
|
_fileAdded(DisplayImageSource(), File(selectedFile.path));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-17 06:02:48 +06:30
|
|
|
_fileAdded(DisplayImageSource fileContainer, File selectedFile) {
|
|
|
|
|
fileContainer.file = selectedFile;
|
|
|
|
|
setState(() {
|
|
|
|
|
fileContainers.add(fileContainer);
|
2021-09-10 14:25:37 +06:30
|
|
|
widget.controller!.addFile = fileContainer;
|
2020-09-17 06:02:48 +06:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_fileRemove(DisplayImageSource fileContainer) {
|
|
|
|
|
setState(() {
|
2021-09-10 14:25:37 +06:30
|
|
|
widget.controller!.removeFile = fileContainer;
|
2020-09-17 06:02:48 +06:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget getFile(DisplayImageSource 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) {
|
2024-01-09 13:11:22 +06:30
|
|
|
var selectedFile = await ImagePicker().pickImage(
|
2020-09-17 06:02:48 +06:30
|
|
|
source: camera ? ImageSource.camera : ImageSource.gallery,
|
|
|
|
|
imageQuality: 80,
|
|
|
|
|
maxWidth: 1000);
|
|
|
|
|
if (selectedFile != null) {
|
|
|
|
|
_fileAdded(fileContainer,
|
|
|
|
|
File.fromRawPath(await selectedFile.readAsBytes()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
: Padding(
|
|
|
|
|
padding: const EdgeInsets.only(left: 3.0),
|
|
|
|
|
child: InkWell(
|
|
|
|
|
onTap: () => {
|
|
|
|
|
Navigator.push(
|
|
|
|
|
context,
|
|
|
|
|
MaterialPageRoute(
|
|
|
|
|
builder: (context) => ShowImage(
|
2021-09-10 14:25:37 +06:30
|
|
|
imageFile: fileContainer.file!,
|
2020-09-17 06:02:48 +06:30
|
|
|
url: fileContainer.file == null
|
2021-09-10 14:25:37 +06:30
|
|
|
? fileContainer.url!
|
|
|
|
|
: '',
|
|
|
|
|
fileName: widget.title!)),
|
2020-09-17 06:02:48 +06:30
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
child: Chip(
|
|
|
|
|
avatar: Icon(Icons.image),
|
|
|
|
|
onDeleted: !widget.enabled
|
|
|
|
|
? null
|
|
|
|
|
: () {
|
|
|
|
|
_fileRemove(fileContainer);
|
|
|
|
|
},
|
|
|
|
|
deleteIcon: Icon(
|
|
|
|
|
Icons.close,
|
|
|
|
|
),
|
2021-09-10 14:25:37 +06:30
|
|
|
label: Text("${widget.title}" + " - ${index + 1}"),
|
2020-09-17 06:02:48 +06:30
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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: primaryColor,
|
|
|
|
|
),
|
|
|
|
|
onPressed: () {
|
|
|
|
|
Navigator.pop(context);
|
|
|
|
|
cameraPress();
|
|
|
|
|
}),
|
|
|
|
|
Text("Camera")
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
Column(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: <Widget>[
|
|
|
|
|
IconButton(
|
|
|
|
|
icon: Icon(
|
|
|
|
|
Icons.photo_library,
|
|
|
|
|
size: 30,
|
|
|
|
|
color: primaryColor,
|
|
|
|
|
),
|
|
|
|
|
onPressed: () {
|
|
|
|
|
Navigator.pop(context);
|
|
|
|
|
photoPress();
|
|
|
|
|
}),
|
|
|
|
|
Text("Gallery")
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-11-09 05:53:25 +06:30
|
|
|
|
2021-09-10 14:25:37 +06:30
|
|
|
Widget actionIcon({OnTap? onTap, Color? color, IconData? iconData}) {
|
2020-11-09 05:53:25 +06:30
|
|
|
return InkWell(
|
|
|
|
|
onTap: onTap,
|
|
|
|
|
child: ClipOval(
|
|
|
|
|
child: Container(
|
|
|
|
|
color: color,
|
|
|
|
|
height: 20,
|
|
|
|
|
width: 20,
|
|
|
|
|
child: Icon(
|
|
|
|
|
iconData,
|
|
|
|
|
color: Colors.white,
|
|
|
|
|
size: 15,
|
|
|
|
|
)),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-09-17 06:02:48 +06:30
|
|
|
}
|