Created
July 11, 2020 20:01
-
-
Save ohashijr/194d47a206a5a52cb1357285279f4708 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:io'; | |
| import 'package:flutter/material.dart'; | |
| import 'package:image_cropper/image_cropper.dart'; | |
| import 'package:image_picker/image_picker.dart'; | |
| class CameraScreen extends StatefulWidget { | |
| @override | |
| _CameraScreenState createState() => _CameraScreenState(); | |
| } | |
| class _CameraScreenState extends State<CameraScreen> { | |
| final picker = ImagePicker(); | |
| File _selectedImage; | |
| bool _inProcess = false; | |
| getImage(ImageSource src) async { | |
| this.setState(() { | |
| _inProcess = true; | |
| }); | |
| final pickedFile = await picker.getImage(source: src); | |
| if (pickedFile != null) { | |
| File cropped = await ImageCropper.cropImage( | |
| sourcePath: pickedFile.path, | |
| aspectRatio: CropAspectRatio(ratioX: 1, ratioY: 1), | |
| compressQuality: 100, | |
| maxHeight: 700, | |
| maxWidth: 700, | |
| compressFormat: ImageCompressFormat.jpg, | |
| androidUiSettings: AndroidUiSettings( | |
| toolbarTitle: 'Camera', | |
| ), | |
| iosUiSettings: IOSUiSettings( | |
| title: 'Camera', | |
| ), | |
| ); | |
| setState(() { | |
| _selectedImage = cropped; | |
| _inProcess = false; | |
| }); | |
| } else { | |
| setState(() { | |
| _inProcess = false; | |
| }); | |
| } | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: Text('Camera'), | |
| ), | |
| body: (_inProcess) | |
| ? Container( | |
| color: Colors.white, | |
| height: double.infinity, | |
| width: double.infinity, | |
| alignment: Alignment.center, | |
| child: SizedBox( | |
| width: 70, | |
| height: 70, | |
| child: CircularProgressIndicator( | |
| strokeWidth: 5.0, | |
| backgroundColor: Theme.of(context).primaryColor, | |
| ), | |
| ), | |
| ) | |
| : Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: <Widget>[ | |
| _selectedImage != null | |
| ? Image.file( | |
| _selectedImage, | |
| width: 250, | |
| height: 250, | |
| fit: BoxFit.cover, | |
| ) | |
| : Container( | |
| width: 250, | |
| height: 250, | |
| child: Icon( | |
| Icons.camera_alt, | |
| size: 200, | |
| color: Colors.grey, | |
| ), | |
| ), | |
| Row( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: <Widget>[ | |
| RaisedButton( | |
| onPressed: () { | |
| getImage(ImageSource.camera); | |
| }, | |
| child: Text('Camera'), | |
| ), | |
| RaisedButton( | |
| onPressed: () { | |
| getImage(ImageSource.gallery); | |
| }, | |
| child: Text('Galeria'), | |
| ), | |
| ], | |
| ) | |
| ], | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment