2022-08-31 12:10:58 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-08-31 10:09:36 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-09-05 11:13:07 +02:00
|
|
|
import 'package:flutter_image_picker/flutter_image_picker.dart';
|
2022-09-06 11:28:53 +02:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
|
2022-09-08 14:23:46 +02:00
|
|
|
final imageProvider =
|
|
|
|
StateNotifierProvider.autoDispose<ImageNotifier, Uint8List?>((ref) {
|
|
|
|
return ImageNotifier();
|
2022-09-06 11:28:53 +02:00
|
|
|
});
|
2022-08-31 10:09:36 +02:00
|
|
|
|
2022-09-08 14:23:46 +02:00
|
|
|
class ImageNotifier extends StateNotifier<Uint8List?> {
|
|
|
|
ImageNotifier() : super(null);
|
|
|
|
|
|
|
|
Uint8List? image;
|
|
|
|
|
|
|
|
void changeImage(Uint8List newImage) {
|
|
|
|
state = image = newImage;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cleanImage() {
|
|
|
|
state = image = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 10:09:36 +02:00
|
|
|
void main() {
|
2022-09-06 11:28:53 +02:00
|
|
|
runApp(const ProviderScope(child: ImagePickerExample()));
|
2022-08-31 10:09:36 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 11:28:53 +02:00
|
|
|
class ImagePickerExample extends ConsumerWidget {
|
2022-08-31 10:09:36 +02:00
|
|
|
const ImagePickerExample({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
2022-09-06 11:28:53 +02:00
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
2022-08-31 10:09:36 +02:00
|
|
|
return MaterialApp(
|
2022-08-31 16:54:45 +02:00
|
|
|
title: 'Flutter Image Picker Example',
|
2022-08-31 10:09:36 +02:00
|
|
|
theme: ThemeData(
|
|
|
|
primarySwatch: Colors.blue,
|
|
|
|
),
|
|
|
|
home: const ImagePickerExampleHomePage(
|
|
|
|
title: 'Flutter Image Picker Example'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 11:28:53 +02:00
|
|
|
class ImagePickerExampleHomePage extends ConsumerStatefulWidget {
|
2022-08-31 10:09:36 +02:00
|
|
|
const ImagePickerExampleHomePage({Key? key, required this.title})
|
|
|
|
: super(key: key);
|
|
|
|
|
|
|
|
final String title;
|
|
|
|
|
|
|
|
@override
|
2022-09-06 11:28:53 +02:00
|
|
|
ImagePickerExampleHomePageState createState() =>
|
|
|
|
ImagePickerExampleHomePageState();
|
2022-08-31 10:09:36 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 11:28:53 +02:00
|
|
|
class ImagePickerExampleHomePageState
|
|
|
|
extends ConsumerState<ImagePickerExampleHomePage> {
|
2022-08-31 16:54:45 +02:00
|
|
|
final double whiteSpace = 20;
|
|
|
|
final double imageWidth = 300;
|
2022-09-01 10:22:32 +02:00
|
|
|
final String placeholder = 'assets/images/placeholder.png';
|
|
|
|
final String imageAlreadyDisplayedMessage =
|
2022-09-01 17:00:59 +02:00
|
|
|
'The selected image is already being displayed!';
|
2022-08-31 10:09:36 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-09-06 11:28:53 +02:00
|
|
|
final imageWatcher = ref.watch(imageProvider);
|
|
|
|
|
2022-08-31 10:09:36 +02:00
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: Text(widget.title),
|
2022-09-06 11:28:53 +02:00
|
|
|
actions: [
|
|
|
|
IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
ref.invalidate(imageProvider);
|
|
|
|
},
|
|
|
|
icon: const Icon(Icons.delete))
|
|
|
|
],
|
2022-08-31 10:09:36 +02:00
|
|
|
),
|
|
|
|
body: Center(
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: <Widget>[
|
2022-09-08 14:23:46 +02:00
|
|
|
ProviderScope(
|
|
|
|
child: Column(children: [
|
2022-09-06 11:28:53 +02:00
|
|
|
if (imageWatcher == null) ...[
|
2022-08-31 14:57:01 +02:00
|
|
|
Image.asset(
|
2022-09-01 10:22:32 +02:00
|
|
|
placeholder,
|
2022-08-31 14:57:01 +02:00
|
|
|
width: imageWidth,
|
|
|
|
height: imageWidth,
|
|
|
|
)
|
2022-08-31 10:09:36 +02:00
|
|
|
] else ...[
|
2022-08-31 14:57:01 +02:00
|
|
|
Image.memory(
|
2022-09-06 11:28:53 +02:00
|
|
|
imageWatcher,
|
2022-08-31 14:57:01 +02:00
|
|
|
width: imageWidth,
|
|
|
|
height: imageWidth,
|
|
|
|
)
|
2022-08-31 10:09:36 +02:00
|
|
|
]
|
2022-09-08 14:23:46 +02:00
|
|
|
])),
|
2022-08-31 14:57:01 +02:00
|
|
|
SizedBox(height: whiteSpace),
|
2022-08-31 10:09:36 +02:00
|
|
|
const Text(
|
2022-08-31 12:33:44 +02:00
|
|
|
'Pick an image or make a photo!',
|
2022-08-31 10:09:36 +02:00
|
|
|
),
|
2022-08-31 14:57:01 +02:00
|
|
|
SizedBox(height: whiteSpace / 2),
|
2022-08-31 10:09:36 +02:00
|
|
|
ElevatedButton(
|
2022-09-01 14:35:38 +02:00
|
|
|
onPressed: pickImage,
|
|
|
|
key: const Key("PickImageButton"),
|
|
|
|
child: const Text('Pick Image')),
|
2022-08-31 14:57:01 +02:00
|
|
|
SizedBox(height: whiteSpace),
|
2022-08-31 10:09:36 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-01 10:22:32 +02:00
|
|
|
/// The [pickImage] function is used to show the usage of the Image Picker Package.
|
2022-09-01 16:44:53 +02:00
|
|
|
/// The most important part is the [ImagePicker] call.
|
2022-09-01 17:07:53 +02:00
|
|
|
/// You can add a custom [ImagePickerTheme] to the [ImagePicker] if you want to change some of the UI.
|
|
|
|
/// An example on how to do that is: ImagePickerTheme(imagePickerTheme: const ImagePickerTheme(title: "Image Picker")).
|
2022-09-01 17:09:40 +02:00
|
|
|
/// As a whole you get `ImagePicker(ImagePickerTheme(imagePickerTheme: const ImagePickerTheme(title: "Image Picker")))`
|
2022-09-01 16:44:53 +02:00
|
|
|
/// Check the README for all possible parameters you can add in the [ImagePickerTheme].
|
2022-09-08 14:23:46 +02:00
|
|
|
/// You can also add a custom Button as a Widget to the Image Picker Dialog.
|
2022-09-01 10:22:32 +02:00
|
|
|
/// This function saves the image in a variable and if it's different than the current image it will get displayed in the application.
|
|
|
|
/// When the same image is chosen there will be a snackbar popping up to let you know it's already being displayed.
|
2022-08-31 11:45:12 +02:00
|
|
|
void pickImage() async {
|
2022-09-01 15:06:47 +02:00
|
|
|
Uint8List? imageInBytes = await showModalBottomSheet<Uint8List?>(
|
2022-09-05 09:21:35 +02:00
|
|
|
context: context,
|
2022-09-06 14:22:28 +02:00
|
|
|
backgroundColor: Colors.white,
|
2022-09-05 09:21:35 +02:00
|
|
|
builder: (BuildContext context) => const ImagePicker());
|
2022-08-31 12:31:54 +02:00
|
|
|
if (imageInBytes != null) {
|
2022-09-06 11:28:53 +02:00
|
|
|
if (!listEquals(ref.read(imageProvider), imageInBytes)) {
|
2022-09-08 14:23:46 +02:00
|
|
|
ref.read(imageProvider.notifier).changeImage(imageInBytes);
|
2022-08-31 12:31:54 +02:00
|
|
|
} else {
|
2022-09-01 10:22:32 +02:00
|
|
|
if (!mounted) return;
|
2022-08-31 12:31:54 +02:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
2022-09-01 10:22:32 +02:00
|
|
|
SnackBar(content: Text(imageAlreadyDisplayedMessage)),
|
2022-08-31 12:31:54 +02:00
|
|
|
);
|
|
|
|
}
|
2022-08-31 10:09:36 +02:00
|
|
|
}
|
2022-09-08 14:23:46 +02:00
|
|
|
imageInBytes = null;
|
2022-08-31 10:09:36 +02:00
|
|
|
}
|
|
|
|
}
|