prefabs_media_picker 0.1.3
prefabs_media_picker: ^0.1.3 copied to clipboard
Media picker flutter package
prefabs_media_picker #
A Flutter package for picking and cropping images from camera or gallery with built-in localization support.
Features #
- 📸 Pick images from camera or gallery
- ✂️ Optional image cropping
- 🌍 Built-in localization (English)
- 🎨 Customizable UI labels
- 📱 Cross-platform support (iOS, Android)
Getting started #
Add this package to your pubspec.yaml:
dependencies:
prefabs_media_picker: ^0.1.0
Then run:
flutter pub get
Setup #
1. Add localization delegates #
In your MaterialApp, add the localization delegates:
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:prefabs_media_picker/media_picker.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: [
AppLocalizations.localizationsDelegates,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: AppLocalizations.supportedLocales,
home: MyHomePage(),
);
}
}
2. Platform-specific configuration #
iOS
Add the following keys to your Info.plist:
<key>NSCameraUsageDescription</key>
<string>We need access to your camera to take photos</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>We need access to your photo library to select images</string>
Android
Add the following permissions to your AndroidManifest.xml:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
Usage #
Basic usage #
Pick an image from camera or gallery:
import 'package:prefabs_media_picker/media_picker.dart';
// Simple image picker
final result = await MediaPicker.pickImage(context);
if (result != null) {
print('Image path: ${result.file.path}');
print('Image size: ${result.size} bytes');
print('Source: ${result.source}'); // MediaSource.camera or MediaSource.gallery
}
With Image cropping #
Enable image cropping after selection:
final result = await MediaPicker.pickImage(
context,
enableCropping: true,
);
if (result != null) {
// Use the cropped image
File croppedImage = result.file;
}
Custom labels #
Override the default localized labels:
final result = await MediaPicker.pickImage(
context,
enableCropping: true,
cropTitle: 'Edit your photo',
sheetTitle: 'Choose image source',
cameraLabel: 'Take photo',
galleryLabel: 'Photo library',
);
Full example #
import 'package:flutter/material.dart';
import 'package:prefabs_media_picker/media_picker.dart';
class ImagePickerExample extends StatefulWidget {
@override
_ImagePickerExampleState createState() => _ImagePickerExampleState();
}
class _ImagePickerExampleState extends State<ImagePickerExample> {
File? _selectedImage;
Future<void> _pickImage() async {
final result = await MediaPicker.pickImage(
context,
enableCropping: true,
);
if (result != null) {
setState(() {
_selectedImage = result.file;
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Image selected: ${result.size} bytes'),
),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Image picker example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (_selectedImage != null)
Container(
width: 300,
height: 300,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(12),
),
child: ClipRRect(
borderRadius: BorderRadius.circular(12),
child: Image.file(
_selectedImage!,
fit: BoxFit.cover,
),
),
)
else
Container(
width: 300,
height: 300,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(12),
),
child: Icon(
Icons.image,
size: 100,
color: Colors.grey,
),
),
SizedBox(height: 24),
ElevatedButton.icon(
onPressed: _pickImage,
icon: Icon(Icons.add_photo_alternate),
label: Text('Pick image'),
),
],
),
),
);
}
}
Localization #
Supported languages #
- English (en) - Default
The package automatically uses the correct translations based on your app's current locale.
Translation Keys #
| Key | English |
|---|---|
cropImage |
Crop image |
selectPhotoSource |
Select photo source |
camera |
Camera |
gallery |
Gallery |
API Reference #
MediaPicker.pickImage() #
static Future<MediaPickerResult?> pickImage(
BuildContext context, {
bool enableCropping = false,
String? cropTitle,
String? sheetTitle,
String? cameraLabel,
String? galleryLabel,
})
Parameters
context(required): BuildContext for showing dialogsenableCropping: Enable image cropping after selection (default:false)cropTitle: Optional custom title for the crop screen (overrides localization)sheetTitle: Optional custom title for the source selection sheet (overrides localization)cameraLabel: Optional custom label for camera option (overrides localization)galleryLabel: Optional custom label for gallery option (overrides localization)
Returns
Returns a MediaPickerResult? object or null if cancelled.
MediaPickerResult #
class MediaPickerResult {
final MediaType type; // MediaType.image
final MediaSource source; // MediaSource.camera or MediaSource.gallery
final File file; // The selected/cropped image file
final int size; // File size in bytes
}
MediaSource #
enum MediaSource {
camera,
gallery,
}
MediaType #
enum MediaType {
image,
}
Requirements #
- Flutter:
>=3.19.0 - Dart:
>=3.3.0 <4.0.0
Dependencies #
image_cropper: ^9.1.0image_picker: ^1.1.2flutter_localizations(from SDK)intl: 0.19.0
License #
BSD-3-Clause