Advertisement
ismaeil

ImagePicker

May 3rd, 2021
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.26 KB | None | 0 0
  1. import 'dart:io';
  2. import 'package:image_picker/image_picker.dart';
  3. import 'package:flutter/material.dart';
  4.  
  5. void main() {
  6.   runApp(MyApp());
  7. }
  8.  
  9. class MyApp extends StatelessWidget {
  10.   @override
  11.   Widget build(BuildContext context) {
  12.     return MaterialApp(
  13.       home: MyHomePage(),
  14.     );
  15.   }
  16. }
  17.  
  18. class MyHomePage extends StatefulWidget {
  19.   @override
  20.   _MyHomePageState createState() => _MyHomePageState();
  21. }
  22.  
  23. class _MyHomePageState extends State<MyHomePage> {
  24.   File _image;
  25.   final picker = ImagePicker();
  26.  
  27.   Future getImg() async {
  28.     final pickedfile = await picker.getImage(source: ImageSource.camera);
  29.     final File file = File(pickedfile.path);
  30.     setState(() {
  31.       if (pickedfile != null) {
  32.         _image = file;
  33.       } else {
  34.         print('no image');
  35.       }
  36.     });
  37.   }
  38.  
  39.   @override
  40.   Widget build(BuildContext context) {
  41.     return Scaffold(
  42.       appBar: AppBar(),
  43.       body: Center(
  44.         child: (_image == null) ? Text('no image') : Image.file(_image),
  45.       ),
  46.       floatingActionButton: FloatingActionButton(
  47.         onPressed: () => getImg(),
  48.         tooltip: 'pick image',
  49.         child: Icon(Icons.add_a_photo),
  50.       ), // This trailing comma makes auto-formatting nicer for build methods.
  51.     );
  52.   }
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement