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.