| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- import 'package:camera/camera.dart';
- import 'package:flutter/material.dart';
- import 'package:telnow_mobile_new/src/utils/ui_service.dart';
- class OpenCamera extends StatefulWidget {
- final List<CameraDescription>? cameras;
- const OpenCamera({required this.cameras, super.key});
- @override
- State<OpenCamera> createState() => _OpenCameraState();
- }
- class _OpenCameraState extends State<OpenCamera> {
- late CameraController _cameraController;
- bool _isRearCameraSelected = true;
- @override
- void dispose() {
- _cameraController.dispose();
- super.dispose();
- }
- @override
- void initState() {
- super.initState();
- initCamera(widget.cameras![0]);
- }
- Future takePicture() async {
- if (!_cameraController.value.isInitialized) {
- return null;
- }
- if (_cameraController.value.isTakingPicture) {
- return null;
- }
- try {
- await _cameraController.setFlashMode(FlashMode.off);
- XFile pickedFile = await _cameraController.takePicture();
- UIService.navigatorKey.currentState?.pop(pickedFile);
- // Navigator.of(context).pop(pickedFile);
- } on CameraException catch (e) {
- debugPrint('Error occurred while taking picture: $e');
- return null;
- }
- }
- Future initCamera(CameraDescription cameraDescription) async {
- _cameraController =
- CameraController(cameraDescription, ResolutionPreset.high);
- try {
- await _cameraController.initialize().then((_) {
- if (!mounted) return;
- setState(() {});
- });
- } on CameraException catch (e) {
- debugPrint("camera error $e");
- }
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- body: SafeArea(
- child: SizedBox(
- width: double.infinity, height: double.infinity,
- child: Stack(children: [
- (_cameraController.value.isInitialized) ? CameraPreview(_cameraController) : Container(color: Colors.black, child: const Center(child: CircularProgressIndicator())),
- Align(
- alignment: Alignment.bottomCenter,
- child: Container(
- width: double.infinity,
- height: MediaQuery.of(context).size.height * 0.15,
- decoration: const BoxDecoration(color: Colors.black),
- child: Row(crossAxisAlignment: CrossAxisAlignment.center, children: [
- Expanded(
- child: IconButton(
- padding: EdgeInsets.zero,
- iconSize: 30,
- icon: Icon(_isRearCameraSelected ? Icons.cameraswitch_outlined : Icons.cameraswitch_rounded, color: Colors.white),
- onPressed: () {
- setState(() => _isRearCameraSelected = !_isRearCameraSelected);
- initCamera(widget.cameras![_isRearCameraSelected ? 0 : 1]);
- },
- )
- ),
- Expanded(
- child: IconButton(
- onPressed: takePicture,
- iconSize: 60,
- padding: EdgeInsets.zero,
- constraints: const BoxConstraints(),
- icon: const Icon(Icons.circle, color: Colors.white),
- )
- ),
- const Spacer(),
- ]),
- )),
- ]),
- ),
- )
- );
- }
- }
|