2022-08-31 10:09:36 +02:00
|
|
|
import 'dart:typed_data';
|
|
|
|
|
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';
|
|
|
|
import 'package:flutter_image_picker/image_picker.dart';
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
runApp(const ImagePickerExample());
|
|
|
|
}
|
|
|
|
|
|
|
|
class ImagePickerExample extends StatelessWidget {
|
|
|
|
const ImagePickerExample({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
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'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ImagePickerExampleHomePage extends StatefulWidget {
|
|
|
|
const ImagePickerExampleHomePage({Key? key, required this.title})
|
|
|
|
: super(key: key);
|
|
|
|
|
|
|
|
final String title;
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<ImagePickerExampleHomePage> createState() =>
|
|
|
|
_ImagePickerExampleHomePageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ImagePickerExampleHomePageState
|
|
|
|
extends State<ImagePickerExampleHomePage> {
|
|
|
|
Uint8List? image;
|
2022-08-31 16:54:45 +02:00
|
|
|
final ImagePicker imagePicker = ImagePicker();
|
|
|
|
final double whiteSpace = 20;
|
|
|
|
final double imageWidth = 300;
|
2022-08-31 10:09:36 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: Text(widget.title),
|
|
|
|
),
|
|
|
|
body: Center(
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: <Widget>[
|
|
|
|
Column(children: [
|
|
|
|
if (image == null) ...[
|
2022-08-31 14:57:01 +02:00
|
|
|
Image.asset(
|
|
|
|
'assets/images/placeholder.png',
|
|
|
|
width: imageWidth,
|
|
|
|
height: imageWidth,
|
|
|
|
)
|
2022-08-31 10:09:36 +02:00
|
|
|
] else ...[
|
2022-08-31 14:57:01 +02:00
|
|
|
Image.memory(
|
|
|
|
image!,
|
|
|
|
width: imageWidth,
|
|
|
|
height: imageWidth,
|
|
|
|
)
|
2022-08-31 10:09:36 +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-08-31 14:57:01 +02:00
|
|
|
onPressed: pickImage, child: const Text('Pick Image')),
|
|
|
|
SizedBox(height: whiteSpace),
|
2022-08-31 10:09:36 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-31 11:45:12 +02:00
|
|
|
void pickImage() async {
|
2022-08-31 16:54:45 +02:00
|
|
|
Uint8List? imageInBytes = await imagePicker.showImagePickerDialog(context);
|
2022-08-31 12:31:54 +02:00
|
|
|
if (imageInBytes != null) {
|
|
|
|
if (!listEquals(image, imageInBytes)) {
|
|
|
|
setState(() {
|
|
|
|
image = imageInBytes;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
const SnackBar(
|
|
|
|
content: Text('Selected image is already being displayed!')),
|
|
|
|
);
|
|
|
|
}
|
2022-08-31 10:09:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|