image_picker_kit 1.0.1
image_picker_kit: ^1.0.1 copied to clipboard
A complete, all-in-one Flutter plugin for image workflow: pick, crop, and compress. This package provides native implementation (MethodChannel) for picking and compressing, while integrating the 'imag [...]
example/lib/main.dart
import 'dart:io'; // Required for File objects
import 'package:flutter/material.dart';
import 'dart:async';
// Import the public facade of your plugin
import 'package:image_picker_kit/image_picker_kit.dart';
void main() {
runApp(const MyApp());
}
/// The main application widget for the example app.
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
/// The main state for the example app.
class _MyAppState extends State<MyApp> {
/// Create an instance of the main plugin class.
/// This is the public API to interact with.
final ImagePickerKit _pickerKit = ImagePickerKit();
/// A message to display the current status (e.g., "Loading...", "SUCCESS").
String _resultMessage = 'No image chosen yet';
/// The final [File] result from the plugin, used for the [Image.file] preview.
File? _imageFile;
/// Calls the "Pick Only" flow using the generic helper.
Future<void> _pickImage() async {
await _handlePickingLogic(
() => _pickerKit.pickImageFromGallery(compressQuality: 10),
);
}
/// Calls the "Pick and Crop" flow using the generic helper.
Future<void> _pickAndCropImage() async {
await _handlePickingLogic(
() => _pickerKit.pickAndCropImageFromGallery(compressQuality: 10),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Media Picker Kit Example')),
body: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Button to test "Pick + Compress"
ElevatedButton(
onPressed: _pickImage,
child: const Text('Pick (Only) & Compress'),
),
const SizedBox(height: 20),
// Button to test "Pick + Crop + Compress"
ElevatedButton(
onPressed: _pickAndCropImage,
child: const Text('Pick, Crop & Compress'),
),
const SizedBox(height: 20),
// Show the current status message
Text(_resultMessage),
const SizedBox(height: 20),
// Show the final image preview
if (_imageFile != null)
Image.file(
_imageFile!,
height: 300,
width: 300,
fit: BoxFit.cover,
)
else
// Placeholder container
Container(
height: 300,
width: 300,
color: Colors.grey[300],
alignment: Alignment.center,
child: const Text('Image Preview'),
),
],
),
),
),
),
);
}
/// A generic helper function to run a picking task and update the UI.
///
/// This centralizes the boilerplate code (setting loading/success/cancel states)
/// into one place. It takes the specific picking function as an argument.
Future<void> _handlePickingLogic(
Future<File?> Function() pickingFuture,
) async {
// Reset the UI to a loading state
setState(() {
_resultMessage = 'Loading...';
_imageFile = null;
});
final File? resultFile;
try {
// Execute the provided function (e.g., _pickerKit.pickImageFromGallery)
resultFile = await pickingFuture();
} catch (e) {
// Handle any unexpected errors from the plugin
setState(() {
_resultMessage = 'ERROR: $e';
_imageFile = null;
});
return;
}
// Update the UI based on whether we got a file or null (cancellation)
setState(() {
if (resultFile != null) {
_resultMessage = 'SUCCESS';
_imageFile = resultFile;
} else {
_resultMessage = 'CANCELLED';
_imageFile = null;
}
});
}
}