Skip to content

Instantly share code, notes, and snippets.

@BoshiLee
Created December 12, 2019 08:24
Show Gist options
  • Save BoshiLee/b809791bfd2e082f151ca5a65ba5bacf to your computer and use it in GitHub Desktop.
Save BoshiLee/b809791bfd2e082f151ca5a65ba5bacf to your computer and use it in GitHub Desktop.
FloatingActionButton(
child: Icon(Icons.camera_alt),
// Provide an onPressed callback.
onPressed: () async {
// Take the Picture in a try / catch block. If anything goes wrong,
// catch the error.
try {
// Ensure that the camera is initialized.
await _initializeControllerFuture;
// Construct the path where the image should be saved using the path
// package.
final path = join(
// Store the picture in the temp directory.
// Find the temp directory using the `path_provider` plugin.
(await getTemporaryDirectory()).path,
'${DateTime.now()}.png',
);
// Attempt to take a picture and log where it's been saved.
await _controller.takePicture(path);
} catch (e) {
// If an error occurs, log the error to the console.
print(e);
}
},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment