|
|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
|
|
import 'package:file_picker/file_picker.dart';
|
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
|
|
|
@ -13,31 +14,31 @@ import 'package:test_sa/views/widgets/buttons/app_flat_button.dart';
|
|
|
|
|
|
|
|
|
|
import 'multi_image_picker_item.dart';
|
|
|
|
|
|
|
|
|
|
class MultiImagesPicker extends StatefulWidget {
|
|
|
|
|
class MultiFilesPicker extends StatefulWidget {
|
|
|
|
|
final String label;
|
|
|
|
|
final bool error;
|
|
|
|
|
final List<File> images;
|
|
|
|
|
final List<File> files;
|
|
|
|
|
final bool enabled;
|
|
|
|
|
|
|
|
|
|
const MultiImagesPicker({Key key, this.images, this.label, this.error = false, this.enabled = true}) : super(key: key);
|
|
|
|
|
const MultiFilesPicker({Key key, this.files, this.label, this.error = false, this.enabled = true}) : super(key: key);
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
_MultiImagesPickerState createState() => _MultiImagesPickerState();
|
|
|
|
|
_MultiFilesPickerState createState() => _MultiFilesPickerState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _MultiImagesPickerState extends State<MultiImagesPicker> with TickerProviderStateMixin {
|
|
|
|
|
class _MultiFilesPickerState extends State<MultiFilesPicker> with TickerProviderStateMixin {
|
|
|
|
|
Size _size;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
_size = MediaQuery.of(context).size;
|
|
|
|
|
Subtitle _subtitle = AppLocalization.of(context).subtitle;
|
|
|
|
|
Subtitle subtitle = AppLocalization.of(context).subtitle;
|
|
|
|
|
return Container(
|
|
|
|
|
padding: EdgeInsets.all(12),
|
|
|
|
|
padding: const EdgeInsets.all(12),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: Color(0xfff5f5f5),
|
|
|
|
|
color: const Color(0xfff5f5f5),
|
|
|
|
|
border: Border.all(
|
|
|
|
|
color: Color(0xffefefef),
|
|
|
|
|
color: const Color(0xffefefef),
|
|
|
|
|
),
|
|
|
|
|
borderRadius: BorderRadius.circular(AppStyle.borderRadius * AppStyle.getScaleFactor(context)),
|
|
|
|
|
),
|
|
|
|
|
@ -49,7 +50,7 @@ class _MultiImagesPickerState extends State<MultiImagesPicker> with TickerProvid
|
|
|
|
|
children: [
|
|
|
|
|
Expanded(
|
|
|
|
|
child: Text(
|
|
|
|
|
widget.label ?? _subtitle.images,
|
|
|
|
|
widget.label ?? subtitle.images,
|
|
|
|
|
style: Theme.of(context).textTheme.headline6.copyWith(
|
|
|
|
|
fontSize: 14,
|
|
|
|
|
),
|
|
|
|
|
@ -57,10 +58,11 @@ class _MultiImagesPickerState extends State<MultiImagesPicker> with TickerProvid
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
AFlatButton(
|
|
|
|
|
text: _subtitle.add,
|
|
|
|
|
text: subtitle.add,
|
|
|
|
|
onPressed: widget.enabled
|
|
|
|
|
? () {
|
|
|
|
|
onImagePick(_subtitle);
|
|
|
|
|
// onImagePick(_subtitle);
|
|
|
|
|
onFilePicker(subtitle);
|
|
|
|
|
}
|
|
|
|
|
: null,
|
|
|
|
|
),
|
|
|
|
|
@ -68,14 +70,14 @@ class _MultiImagesPickerState extends State<MultiImagesPicker> with TickerProvid
|
|
|
|
|
),
|
|
|
|
|
12.height,
|
|
|
|
|
AnimatedSize(
|
|
|
|
|
duration: Duration(milliseconds: 400),
|
|
|
|
|
duration: const Duration(milliseconds: 400),
|
|
|
|
|
child: !widget.error
|
|
|
|
|
? SizedBox.shrink()
|
|
|
|
|
? const SizedBox.shrink()
|
|
|
|
|
: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
Text(
|
|
|
|
|
_subtitle.imagesRequired,
|
|
|
|
|
subtitle.imagesRequired,
|
|
|
|
|
style: Theme.of(context).textTheme.headline6.copyWith(
|
|
|
|
|
fontSize: 14,
|
|
|
|
|
color: AColors.red,
|
|
|
|
|
@ -89,9 +91,9 @@ class _MultiImagesPickerState extends State<MultiImagesPicker> with TickerProvid
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
AnimatedSwitcher(
|
|
|
|
|
duration: Duration(milliseconds: 400),
|
|
|
|
|
duration: const Duration(milliseconds: 400),
|
|
|
|
|
child: Container(
|
|
|
|
|
key: ValueKey(widget.images.length),
|
|
|
|
|
key: ValueKey(widget.files.length),
|
|
|
|
|
width: _size.width,
|
|
|
|
|
height: 200 * AppStyle.getScaleFactor(context),
|
|
|
|
|
padding: EdgeInsets.all(
|
|
|
|
|
@ -102,16 +104,17 @@ class _MultiImagesPickerState extends State<MultiImagesPicker> with TickerProvid
|
|
|
|
|
border: Border.all(color: Theme.of(context).primaryColor, width: 2),
|
|
|
|
|
borderRadius: BorderRadius.circular(8 * AppStyle.getScaleFactor(context)),
|
|
|
|
|
),
|
|
|
|
|
child: widget.images.isEmpty
|
|
|
|
|
child: widget.files.isEmpty
|
|
|
|
|
? MaterialButton(
|
|
|
|
|
onPressed: widget.enabled
|
|
|
|
|
? () {
|
|
|
|
|
onImagePick(_subtitle);
|
|
|
|
|
// onImagePick(_subtitle);
|
|
|
|
|
onFilePicker(subtitle);
|
|
|
|
|
}
|
|
|
|
|
: null,
|
|
|
|
|
child: Center(
|
|
|
|
|
child: Icon(
|
|
|
|
|
Icons.add_a_photo_outlined,
|
|
|
|
|
Icons.file_upload,
|
|
|
|
|
size: 48 * AppStyle.getScaleFactor(context),
|
|
|
|
|
color: Theme.of(context).primaryColor,
|
|
|
|
|
)),
|
|
|
|
|
@ -122,12 +125,12 @@ class _MultiImagesPickerState extends State<MultiImagesPicker> with TickerProvid
|
|
|
|
|
scrollDirection: Axis.horizontal,
|
|
|
|
|
mainAxisSpacing: 10,
|
|
|
|
|
crossAxisSpacing: 10,
|
|
|
|
|
children: List.generate(widget.images.length, (index) {
|
|
|
|
|
File _image = widget.images[index];
|
|
|
|
|
return MultiImagesPickerItem(
|
|
|
|
|
image: _image,
|
|
|
|
|
children: List.generate(widget.files.length, (index) {
|
|
|
|
|
File _image = widget.files[index];
|
|
|
|
|
return MultiFilesPickerItem(
|
|
|
|
|
file: _image,
|
|
|
|
|
onRemoveTap: (image) {
|
|
|
|
|
widget.images.remove(image);
|
|
|
|
|
widget.files.remove(image);
|
|
|
|
|
setState(() {});
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
@ -140,9 +143,23 @@ class _MultiImagesPickerState extends State<MultiImagesPicker> with TickerProvid
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onImagePick(Subtitle _subtitle) async {
|
|
|
|
|
if (widget.images.length >= 5) {
|
|
|
|
|
Fluttertoast.showToast(msg: _subtitle.maxImagesNumberIs5);
|
|
|
|
|
onFilePicker(Subtitle subtitle) async {
|
|
|
|
|
FilePickerResult result = await FilePicker.platform.pickFiles(
|
|
|
|
|
type: FileType.custom,
|
|
|
|
|
allowMultiple: true,
|
|
|
|
|
allowedExtensions: ['jpg', 'png', 'pdf', 'doc', 'docx', 'xlsx', 'pptx'],
|
|
|
|
|
);
|
|
|
|
|
if (result != null) {
|
|
|
|
|
for (var path in result.paths) {
|
|
|
|
|
widget.files.insert(0, File(path));
|
|
|
|
|
}
|
|
|
|
|
setState(() {});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onImagePick(Subtitle subtitle) async {
|
|
|
|
|
if (widget.files.length >= 5) {
|
|
|
|
|
Fluttertoast.showToast(msg: subtitle.maxImagesNumberIs5);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ImageSource source = await showDialog(
|
|
|
|
|
@ -150,13 +167,13 @@ class _MultiImagesPickerState extends State<MultiImagesPicker> with TickerProvid
|
|
|
|
|
builder: (dialogContext) => CupertinoAlertDialog(
|
|
|
|
|
actions: <Widget>[
|
|
|
|
|
TextButton(
|
|
|
|
|
child: Text(_subtitle.pickFromCamera),
|
|
|
|
|
child: Text(subtitle.pickFromCamera),
|
|
|
|
|
onPressed: () {
|
|
|
|
|
Navigator.of(dialogContext).pop(ImageSource.camera);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
TextButton(
|
|
|
|
|
child: Text(_subtitle.pickFromGallery),
|
|
|
|
|
child: Text(subtitle.pickFromGallery),
|
|
|
|
|
onPressed: () {
|
|
|
|
|
Navigator.of(dialogContext).pop(ImageSource.gallery);
|
|
|
|
|
},
|
|
|
|
|
@ -168,9 +185,9 @@ class _MultiImagesPickerState extends State<MultiImagesPicker> with TickerProvid
|
|
|
|
|
final pickedFile = await ImagePicker().pickImage(source: source, imageQuality: 70, maxWidth: 800, maxHeight: 800);
|
|
|
|
|
|
|
|
|
|
if (pickedFile != null) {
|
|
|
|
|
File _fileImage = File(pickedFile.path);
|
|
|
|
|
if (_fileImage != null) {
|
|
|
|
|
widget.images.insert(0, _fileImage);
|
|
|
|
|
File fileImage = File(pickedFile.path);
|
|
|
|
|
if (fileImage != null) {
|
|
|
|
|
widget.files.insert(0, fileImage);
|
|
|
|
|
setState(() {});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|