🤖
🤖
Here's the edited code with the flashlight icon button, capture button, and switch camera button added:
In this code, I added the flashlight icon button, capture button, and switch camera button inside the
Code:
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
// Define a global variable to store the camera instance
late CameraController _cameraController;
late List<CameraDescription> cameras;
// Get the available cameras
Future<void> getCameras() async {
cameras = await availableCameras();
// Set the first camera as the default
_cameraController = CameraController(cameras[0], ResolutionPreset.medium);
await _cameraController.initialize();
}
// Toggle flashlight
void toggleFlashlight() {
if (_cameraController.value.flashMode == FlashMode.off) {
_cameraController.setFlashMode(FlashMode.torch);
} else {
_cameraController.setFlashMode(FlashMode.off);
}
}
// Capture image
void captureImage() async {
final XFile imageFile = await _cameraController.takePicture();
// Do something with the image file
}
// Switch camera
void switchCamera() async {
final CameraDescription newCamera = _cameraController.description == cameras[0] ? cameras[1] : cameras[0];
await _cameraController.dispose();
_cameraController = CameraController(newCamera, ResolutionPreset.medium);
await _cameraController.initialize();
}
class CameraScreen extends StatefulWidget {
@override
_CameraScreenState createState() => _CameraScreenState();
}
class _CameraScreenState extends State<CameraScreen> {
bool isExpanded = false;
@override
void initState() {
super.initState();
getCameras();
}
@override
void dispose() {
_cameraController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
children: [
GestureDetector(
onTap: () {
setState(() {
isExpanded = !isExpanded;
});
},
child: AnimatedSize(
duration: const Duration(milliseconds: 200),
curve: Curves.easeInOut,
child: Container(
height: isExpanded ? MediaQuery.of(context).size.height * 0.7 : MediaQuery.of(context).size.height * 0.3,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.deepPurple,
),
child: isExpanded
? Column(
children: [
Expanded(
child: ClipRRect(
borderRadius: BorderRadius.circular(20),
child: CameraPreview(_cameraController), // Camera preview
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: Icon(
_cameraController.value.flashMode == FlashMode.off ? Icons.flash_off : Icons.flash_on,
color: Colors.white,
),
onPressed: toggleFlashlight,
),
IconButton(
icon: Icon(
Icons.camera,
color: Colors.white,
size: 48,
),
onPressed: captureImage,
),
IconButton(
icon: Icon(
Icons.switch_camera,
color: Colors.white,
),
onPressed: switchCamera,
),
],
),
],
)
: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.camera,
color: Colors.white,
size: 48,
),
SizedBox(height: 10),
Text(
'CAMERA',
style: TextStyle(color: Colors.white, fontSize: 22),
)
],
),
),
),
),
),
],
),
);
}
}
In this code, I added the flashlight icon button, capture button, and switch camera button inside the
Column widget when the camera preview is expanded. These buttons are displayed in a Row widget at the bottom of the camera preview. The onPressed callbacks for the buttons are linked to the respective functions: toggleFlashlight, captureImage, and switchCamera. When the container is tapped, the camera preview expands and the buttons become visible.