Taking pictures with image_picker

Flutter does not communicate with the camera API directly, as this is a platform-level resource. The image_picker plugin, as its name suggests, helps with picking an image. It enables importing image files from the gallery and taking new pictures using the camera.

First, we add the dependency to the pubspec.yaml file and get it with the flutter packages get command:

dependencies:
image_picker: ^0.5.0 # Image picker

We control image picking in the same place as the user enters their display name after login, in the final step of the Stepper widget. When the user presses the small avatar image, the camera opens to take a picture:

// login_page.dart

// part of LoginScreenState class
void _importImage() async {
final image = await ImagePicker.pickImage(source: ImageSource.camera);
setState(() {
_imageFile = image;
});
}

This is done with the ImagePicker class. We use its pickImage() method to start the camera and take a picture (all managed by the plugin) that resolves the captured image to a file for our usage.

You can find the source code of login_page.dart on GitHub for a full example on how to use the image_picker plugin's. Also, it is important that you check the plugin's documentation, at https://pub.dartlang.org/packages/image_picker, as it requires some configuration to work.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.1.158