fahimkamal63

Image Picker Flatter

Nov 30th, 2021
751
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'dart:io';
  2.  
  3. import 'package:flutter/material.dart';
  4. import 'package:image_picker/image_picker.dart';
  5.  
  6. class ImagePickerClass extends StatefulWidget {
  7.   const ImagePickerClass({Key? key}) : super(key: key);
  8.  
  9.   @override
  10.   _ImagePickerClassState createState() => _ImagePickerClassState();
  11. }
  12.  
  13. class _ImagePickerClassState extends State<ImagePickerClass> {
  14.   File? _image;
  15.  
  16.   Future CameraImage() async {
  17.     final picker = ImagePicker();
  18.     final pickFile = await picker.getImage(
  19.       source: ImageSource.camera,
  20.       maxHeight: 300,
  21.       maxWidth: 300
  22.     );
  23.     setState(() {
  24.       _image = File(pickFile!.path);
  25.     });
  26.   }
  27.  
  28.   Future GalleryImage() async {
  29.     final picker = ImagePicker();
  30.     final pickFile = await picker.getImage(
  31.         source: ImageSource.gallery,
  32.         maxHeight: 300,
  33.         maxWidth: 300
  34.     );
  35.     setState(() {
  36.       _image = File(pickFile!.path);
  37.     });
  38.   }
  39.  
  40.   @override
  41.   Widget build(BuildContext context) {
  42.     return Scaffold(
  43.       appBar: AppBar(title: Text("Image Picker"), centerTitle: true,),
  44.       body: Container(
  45.         child: Column(
  46.           children: [
  47.             Container(
  48.               height: 300,
  49.               width: 300,
  50.               child: _image == null? Text("Pick your Picture") :Image.file(_image!),
  51.             ),
  52.             RaisedButton(
  53.                 onPressed: CameraImage,
  54.               child: Icon(Icons.camera),
  55.             ),
  56.             RaisedButton(
  57.               onPressed: GalleryImage,
  58.               child: Icon(Icons.image),
  59.             )
  60.           ],
  61.         ),
  62.       ),
  63.     );
  64.   }
  65. }
  66.  
RAW Paste Data