I have an application written on a flutter in which I take a photo, turn this photo into a base64 and then send. But I have a problem I can't focus on the camera. So I'm new to flutter, I can't solve this problem myself. I will be grateful for your help. My goal is to focus on that place when you tap the screen. Here is my code:
import 'dart:async';
import 'dart:io';
import 'dart:convert';
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
class PicturePreview extends StatefulWidget {
final CameraDescription camera;
const PicturePreview(this.camera, {Key? key}) : super(key: key);
@override
_PicturePreviewState createState() => _PicturePreviewState();
}
class _PicturePreviewState extends State<PicturePreview> {
late CameraController _controller;
Future<void>? _initializeControllerFuture;
@override
late String _imageB64;
late File _image;
void initState() {
super.initState();
// To display the current output from the Camera,
// create a CameraController.
_controller = CameraController(
// Get a specific camera from the list of available cameras.
widget.camera,
// Define the resolution to use.
ResolutionPreset.low,
);
// Next, initialize the controller. This returns a Future.
_initializeControllerFuture = _controller.initialize();
}
@override
void dispose() {
// Dispose of the controller when the widget is disposed.
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder<void>(
future: _initializeControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
// If the Future is complete, display the preview.
return CameraPreview(_controller);
} else {
// Otherwise, display a loading indicator.
return Center(child: CircularProgressIndicator());
}
},
),
floatingActionButton: 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;
// Attempt to take a picture and get the file `image`
// where it was saved.
final image = await _controller.takePicture();
_image = File(image.path);
List<int> imageBytes = _image.readAsBytesSync();
_imageB64 = base64Encode(imageBytes);
Navigator.pop(context, _imageB64);
} catch (e) {
// If an error occurs, log the error to the console.
print(e);
}
},
),
);
}
}