add second button in camera

This commit is contained in:
Valentin CZERYBA 2024-07-18 20:55:38 +02:00
parent e241a79289
commit d35e606ee7

View File

@ -73,54 +73,66 @@ class CameraState extends State<Camera> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar(title: const Text('Take a picture')), appBar: AppBar(title: const Text('Take a picture')),
// You must wait until the controller is initialized before displaying the // You must wait until the controller is initialized before displaying the
// camera preview. Use a FutureBuilder to display a loading spinner until the // camera preview. Use a FutureBuilder to display a loading spinner until the
// controller has finished initializing. // controller has finished initializing.
body: FutureBuilder<void>( body: FutureBuilder<void>(
future: _initializeControllerFuture, future: _initializeControllerFuture,
builder: (context, snapshot) { builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) { if (snapshot.connectionState == ConnectionState.done) {
// If the Future is complete, display the preview. // If the Future is complete, display the preview.
return CameraPreview(_controller); return CameraPreview(_controller);
} else { } else {
// Otherwise, display a loading indicator. // Otherwise, display a loading indicator.
return const Center(child: CircularProgressIndicator()); return const Center(child: CircularProgressIndicator());
} }
}, },
), ),
floatingActionButton: FloatingActionButton( floatingActionButton: Padding(
// Provide an onPressed callback. padding: const EdgeInsets.all(8.0),
onPressed: () async { child: Row(
// Take the Picture in a try / catch block. If anything goes wrong, mainAxisAlignment: MainAxisAlignment.center,
// catch the error. children: <Widget>[
try { FloatingActionButton(
// Ensure that the camera is initialized. backgroundColor: Colors.green,
await _initializeControllerFuture; onPressed: () => {},
child: Icon(Icons.play_arrow),
// Attempt to take a picture and get the file `image`
// where it was saved.
final image = await _controller.takePicture();
if (!context.mounted) return;
// If the picture was taken, display it on a new screen.
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => DisplayPictureScreen(
// Pass the automatically generated path to
// the DisplayPictureScreen widget.
imagePath: image.path,
), ),
), SizedBox(width: 40),
); FloatingActionButton(
} catch (e) { // Provide an onPressed callback.
// If an error occurs, log the error to the console. onPressed: () async {
print(e); // Take the Picture in a try / catch block. If anything goes wrong,
} // catch the error.
}, try {
child: const Icon(Icons.camera_alt), // Ensure that the camera is initialized.
), await _initializeControllerFuture;
);
// Attempt to take a picture and get the file `image`
// where it was saved.
final image = await _controller.takePicture();
if (!context.mounted) return;
// If the picture was taken, display it on a new screen.
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => DisplayPictureScreen(
// Pass the automatically generated path to
// the DisplayPictureScreen widget.
imagePath: image.path,
),
),
);
} catch (e) {
// If an error occurs, log the error to the console.
print(e);
}
},
child: const Icon(Icons.camera_alt),
)
],
)));
} }
} }