You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.6 KiB
Dart
119 lines
3.6 KiB
Dart
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';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:test_sa/extensions/context_extension.dart';
|
|
import 'package:test_sa/extensions/int_extensions.dart';
|
|
|
|
import '../../../new_views/common_widgets/app_dashed_button.dart';
|
|
import 'multi_image_picker_item.dart';
|
|
|
|
class MultiFilesPicker extends StatefulWidget {
|
|
final String label;
|
|
final bool error;
|
|
final List<File> files;
|
|
final bool enabled, onlyImages;
|
|
|
|
const MultiFilesPicker({Key key, this.files, this.label, this.error = false, this.enabled = true, this.onlyImages = false}) : super(key: key);
|
|
|
|
@override
|
|
State<MultiFilesPicker> createState() => _MultiFilesPickerState();
|
|
}
|
|
|
|
class _MultiFilesPickerState extends State<MultiFilesPicker> {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
AppDashedButton(title: widget.label, onPressed: (widget.enabled == false) ? () {} : onFilePicker),
|
|
16.height,
|
|
if (widget.files?.isNotEmpty ?? false)
|
|
Wrap(
|
|
spacing: 8.toScreenWidth,
|
|
children: List.generate(
|
|
widget.files.length,
|
|
(index) {
|
|
File image = widget.files[index];
|
|
return MultiFilesPickerItem(
|
|
file: image,
|
|
enabled: widget.enabled,
|
|
onRemoveTap: (image) {
|
|
if (!widget.enabled) {
|
|
return;
|
|
}
|
|
widget.files.remove(image);
|
|
setState(() {});
|
|
},
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
fromFilePicker() async {
|
|
FilePickerResult result = await FilePicker.platform.pickFiles(
|
|
type: FileType.custom,
|
|
allowMultiple: true,
|
|
allowedExtensions: widget.onlyImages ? ['jpg', 'jpeg', 'png'] : ['jpg', 'jpeg', 'png', 'pdf', 'doc', 'docx', 'xlsx', 'pptx'],
|
|
);
|
|
if (result != null) {
|
|
for (var path in result.paths) {
|
|
widget.files.add(File(path));
|
|
}
|
|
setState(() {});
|
|
}
|
|
}
|
|
|
|
onFilePicker() async {
|
|
if (widget.files.length >= 5) {
|
|
Fluttertoast.showToast(msg: context.translation.maxImagesNumberIs5);
|
|
return;
|
|
}
|
|
ImageSource source = await showDialog(
|
|
context: context,
|
|
builder: (dialogContext) => CupertinoAlertDialog(
|
|
actions: <Widget>[
|
|
TextButton(
|
|
child: Text(context.translation.pickFromCamera),
|
|
onPressed: () {
|
|
Navigator.of(dialogContext).pop(ImageSource.camera);
|
|
},
|
|
),
|
|
TextButton(
|
|
child: Text(context.translation.pickFromGallery),
|
|
onPressed: () {
|
|
Navigator.of(dialogContext).pop(ImageSource.gallery);
|
|
},
|
|
),
|
|
TextButton(
|
|
child: Text(context.translation.pickFromFiles),
|
|
onPressed: () async {
|
|
await fromFilePicker();
|
|
Navigator.pop(context);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
);
|
|
if (source == null) return;
|
|
|
|
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.files.add(fileImage);
|
|
setState(() {});
|
|
}
|
|
}
|
|
|
|
setState(() {});
|
|
}
|
|
}
|