122 lines
2.9 KiB
Dart
122 lines
2.9 KiB
Dart
library image_pickers;
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
|
|
enum GalleryMode {
|
|
image,
|
|
video,
|
|
}
|
|
|
|
class UIConfig {
|
|
const UIConfig({this.uiThemeColor});
|
|
|
|
final Color? uiThemeColor;
|
|
}
|
|
|
|
class CropConfig {
|
|
const CropConfig({this.enableCrop = true});
|
|
|
|
final bool enableCrop;
|
|
}
|
|
|
|
class Media {
|
|
const Media({this.path});
|
|
|
|
final String? path;
|
|
}
|
|
|
|
class ImagePickers {
|
|
static final ImagePicker _picker = ImagePicker();
|
|
|
|
static Future<List<Media>> pickerPaths({
|
|
UIConfig? uiConfig,
|
|
GalleryMode galleryMode = GalleryMode.image,
|
|
int selectCount = 1,
|
|
bool showCamera = false,
|
|
CropConfig? cropConfig,
|
|
}) async {
|
|
switch (galleryMode) {
|
|
case GalleryMode.video:
|
|
final video = await _picker.pickVideo(source: ImageSource.gallery);
|
|
return video == null ? const [] : [Media(path: video.path)];
|
|
case GalleryMode.image:
|
|
if (selectCount > 1) {
|
|
final images = await _picker.pickMultiImage(limit: selectCount);
|
|
return images.map((file) => Media(path: file.path)).toList();
|
|
}
|
|
|
|
final image = await _picker.pickImage(source: ImageSource.gallery);
|
|
return image == null ? const [] : [Media(path: image.path)];
|
|
}
|
|
}
|
|
|
|
static Future<void> previewImages(List<String> images, int index) async {
|
|
if (images.isEmpty) {
|
|
return;
|
|
}
|
|
|
|
await Get.to(
|
|
() => _ImagePreviewPage(
|
|
images: images,
|
|
initialIndex: index,
|
|
),
|
|
transition: Transition.fadeIn,
|
|
);
|
|
}
|
|
}
|
|
|
|
class _ImagePreviewPage extends StatefulWidget {
|
|
const _ImagePreviewPage({
|
|
required this.images,
|
|
required this.initialIndex,
|
|
});
|
|
|
|
final List<String> images;
|
|
final int initialIndex;
|
|
|
|
@override
|
|
State<_ImagePreviewPage> createState() => _ImagePreviewPageState();
|
|
}
|
|
|
|
class _ImagePreviewPageState extends State<_ImagePreviewPage> {
|
|
late final PageController _controller = PageController(initialPage: widget.initialIndex);
|
|
late int _currentIndex = widget.initialIndex;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: Colors.black,
|
|
appBar: AppBar(
|
|
backgroundColor: Colors.black,
|
|
elevation: 0,
|
|
title: Text('${_currentIndex + 1}/${widget.images.length}'),
|
|
),
|
|
body: PageView.builder(
|
|
controller: _controller,
|
|
itemCount: widget.images.length,
|
|
onPageChanged: (value) {
|
|
setState(() {
|
|
_currentIndex = value;
|
|
});
|
|
},
|
|
itemBuilder: (context, index) {
|
|
return InteractiveViewer(
|
|
minScale: 0.8,
|
|
maxScale: 4,
|
|
child: Center(
|
|
child: Image.file(
|
|
File(widget.images[index]),
|
|
fit: BoxFit.contain,
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|