Skip to content

Instantly share code, notes, and snippets.

@geeksilva97
Created May 23, 2021 17:56
Show Gist options
  • Save geeksilva97/66c6201e031444f45f5ff27d6ee674e1 to your computer and use it in GitHub Desktop.
Save geeksilva97/66c6201e031444f45f5ff27d6ee674e1 to your computer and use it in GitHub Desktop.
TakePhoto
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
class TakePhoto extends StatefulWidget {
final CameraDescription? camera;
TakePhoto({this.camera});
@override
_TakePhotoState createState() => _TakePhotoState();
}
class _TakePhotoState extends State<TakePhoto> {
late CameraController _controller;
late Future<void> _initializeControllerFuture;
@override
void initState() {
super.initState();
_controller = CameraController(
// Get a specific camera from the list of available cameras.
widget.camera as CameraDescription,
// Define the resolution to use.
ResolutionPreset.medium,
);
// Next, initialize the controller. This returns a Future.
_initializeControllerFuture = _controller.initialize();
}
Future<XFile?> takePicture() async {
if (_controller.value.isTakingPicture) {
return null;
}
try {
XFile file = await _controller.takePicture();
return file;
} on CameraException catch (e) {
print(e);
return null;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Take picture'),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
final file = await takePicture();
Navigator.of(context).pop(file != null ? file.path : null);
},
child: Icon(Icons.camera_alt),
),
body: FutureBuilder<void>(
future: _initializeControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
// If the Future is complete, display the preview.
return Container(
// height: MediaQuery.of(context).size.height,
child: CameraPreview(_controller),
);
} else {
// Otherwise, display a loading indicator.
return Center(child: CircularProgressIndicator());
}
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment