mirror of
https://git.meshkee.com/applications/cssd.git
synced 2026-10-10 21:28:54 +03:30
250 lines
8.3 KiB
Dart
Executable File
250 lines
8.3 KiB
Dart
Executable File
// ignore_for_file: invalid_use_of_visible_for_testing_member, deprecated_member_use
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:cssd/providers/items.dart';
|
|
import 'package:file_picker/file_picker.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
class UploaderImage extends StatefulWidget {
|
|
final double deviceBlockSize;
|
|
final ItemProvider providerObject;
|
|
|
|
const UploaderImage({
|
|
super.key,
|
|
required this.deviceBlockSize,
|
|
required this.providerObject,
|
|
});
|
|
|
|
@override
|
|
State<UploaderImage> createState() => _UploaderImageState();
|
|
}
|
|
|
|
class _UploaderImageState extends State<UploaderImage> {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Container(
|
|
child: Directionality(
|
|
textDirection: TextDirection.rtl,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
|
children: <Widget>[
|
|
Container(
|
|
margin: EdgeInsets.only(top: 20),
|
|
height: widget.providerObject.catchImages.length != 0 ? 60 : 0,
|
|
child: ImageBox(
|
|
deviceBlockSize: widget.deviceBlockSize,
|
|
itemProvider: widget.providerObject,
|
|
),
|
|
),
|
|
SizedBox(
|
|
height: widget.providerObject.catchImages.length != 0 ? 10 : 0,
|
|
),
|
|
InkWell(
|
|
onTap: () async {
|
|
await loadAssetsFromFilePicker(context);
|
|
},
|
|
child: Container(
|
|
width: MediaQuery.of(context).size.width - 40,
|
|
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 30),
|
|
decoration: BoxDecoration(
|
|
borderRadius: BorderRadius.circular(5),
|
|
color: Colors.black,
|
|
),
|
|
child: Text(
|
|
'+ Add an image or file from device',
|
|
textDirection: TextDirection.ltr,
|
|
style: TextStyle(color: Colors.white),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
),
|
|
),
|
|
SizedBox(
|
|
height: 10,
|
|
),
|
|
InkWell(
|
|
onTap: () async {
|
|
await loadAssetsFromCamera(context);
|
|
},
|
|
child: Container(
|
|
width: MediaQuery.of(context).size.width - 40,
|
|
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 30),
|
|
decoration: BoxDecoration(
|
|
borderRadius: BorderRadius.circular(5),
|
|
color: Colors.black,
|
|
),
|
|
child: Text(
|
|
'+ Add an image from camera',
|
|
textDirection: TextDirection.ltr,
|
|
style: TextStyle(color: Colors.white),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> loadAssetsFromFilePicker(BuildContext context) async {
|
|
var providerObject = Provider.of<ItemProvider>(context, listen: false);
|
|
List<PlatformFile> files = providerObject.catchImages;
|
|
var image = await ImagePicker.platform.pickImage(
|
|
source: ImageSource.gallery,
|
|
imageQuality: 25,
|
|
maxHeight: 1080,
|
|
maxWidth: 1920,
|
|
);
|
|
print('Picked image size (bytes): ${await image?.path.length}');
|
|
if (image != null) {
|
|
files.add(
|
|
PlatformFile(
|
|
path: image.path,
|
|
name: "item_image",
|
|
bytes: File(image.path).readAsBytesSync(),
|
|
size: File(image.path).lengthSync(),
|
|
),
|
|
);
|
|
providerObject.setCatchImages(files);
|
|
providerObject.setImages();
|
|
}
|
|
}
|
|
|
|
Future<void> loadAssetsFromCamera(BuildContext context) async {
|
|
var providerObject = Provider.of<ItemProvider>(context, listen: false);
|
|
List<PlatformFile> _catchImages = providerObject.catchImages;
|
|
var image = await ImagePicker.platform.pickImage(
|
|
source: ImageSource.camera,
|
|
imageQuality: 25,
|
|
maxHeight: 1080,
|
|
maxWidth: 1920,
|
|
);
|
|
if (image != null) {
|
|
_catchImages.add(
|
|
PlatformFile(
|
|
path: image.path,
|
|
name: "item_image",
|
|
bytes: File(image.path).readAsBytesSync(),
|
|
size: File(image.path).lengthSync(),
|
|
),
|
|
);
|
|
providerObject.setCatchImages(_catchImages);
|
|
providerObject.setImages();
|
|
}
|
|
}
|
|
}
|
|
|
|
class ImageBox extends StatelessWidget {
|
|
const ImageBox({
|
|
Key? key,
|
|
required double deviceBlockSize,
|
|
required ItemProvider itemProvider,
|
|
}) : _deviceBlockSize = deviceBlockSize,
|
|
_providerObject = itemProvider,
|
|
super(key: key);
|
|
|
|
final double _deviceBlockSize;
|
|
final ItemProvider _providerObject;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return ListView.separated(
|
|
shrinkWrap: true,
|
|
scrollDirection: Axis.horizontal,
|
|
itemBuilder: (ctx, index) {
|
|
bool isPdf = _providerObject.catchImages[index].path!.endsWith('pdf');
|
|
bool isWord =
|
|
_providerObject.catchImages[index].path!.endsWith('docx') ||
|
|
_providerObject.catchImages[index].path!.endsWith('doc');
|
|
|
|
// print(_providerObject.catchImages[index].path.endsWith('jpg'));
|
|
// .substring(_providerObject.catchImages[index].path.length - 4));
|
|
return ClipRRect(
|
|
borderRadius: BorderRadius.circular(5),
|
|
child: Container(
|
|
width: 60,
|
|
height: 60,
|
|
child: Stack(
|
|
children: <Widget>[
|
|
Positioned(
|
|
top: 10,
|
|
left: 0,
|
|
child: GestureDetector(
|
|
onTap: () => isPdf
|
|
? print('pdf')
|
|
: isWord
|
|
? print('word')
|
|
: Navigator.of(context).push(
|
|
MaterialPageRoute(
|
|
builder: (_) => Container(
|
|
color: Colors.white,
|
|
child: Center(
|
|
child: Image.memory(
|
|
_providerObject
|
|
.catchImages[index].bytes!,
|
|
fit: BoxFit.contain,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
child: isPdf
|
|
? Image.asset(
|
|
'assets/Images/pdf.png',
|
|
width: 60,
|
|
height: 60,
|
|
fit: BoxFit.fill,
|
|
)
|
|
: isWord
|
|
? Image.asset(
|
|
'assets/Images/word.png',
|
|
width: 60,
|
|
height: 60,
|
|
fit: BoxFit.fill,
|
|
)
|
|
: Image.memory(
|
|
_providerObject.catchImages[index].bytes!,
|
|
// asset: _images[index],
|
|
width: 60,
|
|
height: 60,
|
|
fit: BoxFit.contain,
|
|
),
|
|
),
|
|
),
|
|
Positioned(
|
|
top: 0,
|
|
right: 0,
|
|
child: GestureDetector(
|
|
onTap: () {
|
|
// setState(() {
|
|
_providerObject.removeImage(index);
|
|
// });
|
|
},
|
|
child: CircleAvatar(
|
|
backgroundColor: Colors.red,
|
|
radius: 10,
|
|
child: Icon(
|
|
Icons.close,
|
|
size: 12,
|
|
color: Colors.white,
|
|
),
|
|
),
|
|
),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
itemCount: _providerObject.catchImages.length,
|
|
separatorBuilder: (BuildContext context, int index) => SizedBox(
|
|
width: 3 * _deviceBlockSize,
|
|
),
|
|
);
|
|
}
|
|
}
|