import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primaryColor: Colors.green),
home: const GalleryAccess(),
debugShowCheckedModeBanner: false,
);
}
}
class GalleryAccess extends StatefulWidget {
const GalleryAccess({super.key});
@override
State<GalleryAccess> createState() => _GalleryAccessState();
}
class _GalleryAccessState extends State<GalleryAccess> {
File? galleryFile;
final picker = ImagePicker();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Gallery Access and Camera'),
backgroundColor: Colors.green,
foregroundColor: Colors.white,
actions: const [
Text(
"GFG",
textScaleFactor: 3,
style: TextStyle(color: Colors.white),
),
],
),
body: Builder(
builder: (BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
foregroundColor: Colors.white),
child: const Text('Select Image from Gallery and Camera'),
onPressed: () {
_showPicker(context: context);
},
),
const SizedBox(
height: 20,
),
SizedBox(
height: 200.0,
width: 300.0,
child: galleryFile == null
? const Center(child: Text('Sorry nothing selected!!'))
: Center(child: Image.file(galleryFile!)),
),
],
),
);
},
),
);
}
void _showPicker({
required BuildContext context,
}) {
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return SafeArea(
child: Wrap(
children: <Widget>[
ListTile(
leading: const Icon(Icons.photo_library),
title: const Text('Photo Library'),
onTap: () {
getImage(ImageSource.gallery);
Navigator.of(context).pop();
},
),
ListTile(
leading: const Icon(Icons.photo_camera),
title: const Text('Camera'),
onTap: () {
getImage(ImageSource.camera);
Navigator.of(context).pop();
},
),
],
),
);
},
);
}
Future getImage(
ImageSource img,
) async {
// pick image from gallary
final pickedFile = await picker.pickImage(source: img);
// store it in a valid variable
XFile? xfilePick = pickedFile;
setState(
() {
if (xfilePick != null) {
// store that in global variable galleryFile in the form of File
galleryFile = File(pickedFile!.path);
} else {
ScaffoldMessenger.of(context).showSnackBar(// is this context <<<
const SnackBar(content: Text('Nothing is selected')));
}
},
);
}
}