joaopaulofcc

Untitled

Oct 21st, 2020
1,159
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:image_cropper/image_cropper.dart';
  4. import 'package:image_picker/image_picker.dart';
  5.  
  6. void main() {
  7.   runApp(MyApp());
  8. }
  9.  
  10. class MyApp extends StatelessWidget {
  11.   @override
  12.   Widget build(BuildContext context) {
  13.     return MaterialApp(
  14.       title: 'Foto App',
  15.       theme: ThemeData(
  16.         primarySwatch: Colors.blue,
  17.         visualDensity: VisualDensity.adaptivePlatformDensity,
  18.       ),
  19.       home: CameraScreen(),
  20.     );
  21.   }
  22. }
  23.  
  24. class CameraScreen extends StatefulWidget {
  25.   @override
  26.   _CameraScreenState createState() => _CameraScreenState();
  27. }
  28.  
  29. class _CameraScreenState extends State<CameraScreen> {
  30.   final picker = ImagePicker();
  31.   File _selectedImage;
  32.   bool _inProcess = false;
  33.  
  34.   getImage(ImageSource src) async {
  35.     this.setState(() {
  36.       _inProcess = true;
  37.     });
  38.     final pickedFile = await picker.getImage(source: src);
  39.     if (pickedFile != null) {
  40.       File cropped = await ImageCropper.cropImage(
  41.         sourcePath: pickedFile.path,
  42.         aspectRatio: CropAspectRatio(ratioX: 1, ratioY: 1),
  43.         compressQuality: 100,
  44.         maxHeight: 700,
  45.         maxWidth: 700,
  46.         compressFormat: ImageCompressFormat.jpg,
  47.       );
  48.       setState(() {
  49.         _selectedImage = cropped;
  50.         _inProcess = false;
  51.       });
  52.     } else {
  53.       setState(() {
  54.         _inProcess = false;
  55.       });
  56.     }
  57.   }
  58.  
  59.   @override
  60.   Widget build(BuildContext context) {
  61.     return Scaffold(
  62.       appBar: AppBar(
  63.         title: Text('Camera'),
  64.       ),
  65.       body: (_inProcess)
  66.           ? Container(
  67.               color: Colors.white,
  68.               height: double.infinity,
  69.               width: double.infinity,
  70.               alignment: Alignment.center,
  71.               child: SizedBox(
  72.                 width: 70,
  73.                 height: 70,
  74.                 child: CircularProgressIndicator(
  75.                   strokeWidth: 5.0,
  76.                   backgroundColor: Theme.of(context).primaryColor,
  77.                 ),
  78.               ),
  79.             )
  80.           : Column(
  81.               mainAxisAlignment: MainAxisAlignment.center,
  82.               children: <Widget>[
  83.                 _selectedImage != null
  84.                     ? Image.file(
  85.                         _selectedImage,
  86.                         width: 250,
  87.                         height: 250,
  88.                         fit: BoxFit.cover,
  89.                       )
  90.                     : Container(
  91.                         width: 250,
  92.                         height: 250,
  93.                         child: Icon(
  94.                           Icons.camera_alt,
  95.                           size: 200,
  96.                           color: Colors.grey,
  97.                         ),
  98.                       ),
  99.                 Row(
  100.                   mainAxisAlignment: MainAxisAlignment.center,
  101.                   children: <Widget>[
  102.                     RaisedButton(
  103.                       onPressed: () {
  104.                         getImage(ImageSource.camera);
  105.                       },
  106.                       child: Text('Camera'),
  107.                     ),
  108.                     RaisedButton(
  109.                       onPressed: () {
  110.                         getImage(ImageSource.gallery);
  111.                       },
  112.                       child: Text('Galeria'),
  113.                     ),
  114.                   ],
  115.                 )
  116.               ],
  117.             ),
  118.     );
  119.   }
  120. }
  121. s
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×