camera.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import 'package:camera/camera.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:telnow_mobile_new/src/utils/ui_service.dart';
  4. class OpenCamera extends StatefulWidget {
  5. final List<CameraDescription>? cameras;
  6. const OpenCamera({required this.cameras, super.key});
  7. @override
  8. State<OpenCamera> createState() => _OpenCameraState();
  9. }
  10. class _OpenCameraState extends State<OpenCamera> {
  11. late CameraController _cameraController;
  12. bool _isRearCameraSelected = true;
  13. @override
  14. void dispose() {
  15. _cameraController.dispose();
  16. super.dispose();
  17. }
  18. @override
  19. void initState() {
  20. super.initState();
  21. initCamera(widget.cameras![0]);
  22. }
  23. Future takePicture() async {
  24. if (!_cameraController.value.isInitialized) {
  25. return null;
  26. }
  27. if (_cameraController.value.isTakingPicture) {
  28. return null;
  29. }
  30. try {
  31. await _cameraController.setFlashMode(FlashMode.off);
  32. XFile pickedFile = await _cameraController.takePicture();
  33. UIService.navigatorKey.currentState?.pop(pickedFile);
  34. // Navigator.of(context).pop(pickedFile);
  35. } on CameraException catch (e) {
  36. debugPrint('Error occurred while taking picture: $e');
  37. return null;
  38. }
  39. }
  40. Future initCamera(CameraDescription cameraDescription) async {
  41. _cameraController =
  42. CameraController(cameraDescription, ResolutionPreset.high);
  43. try {
  44. await _cameraController.initialize().then((_) {
  45. if (!mounted) return;
  46. setState(() {});
  47. });
  48. } on CameraException catch (e) {
  49. debugPrint("camera error $e");
  50. }
  51. }
  52. @override
  53. Widget build(BuildContext context) {
  54. return Scaffold(
  55. body: SafeArea(
  56. child: SizedBox(
  57. width: double.infinity, height: double.infinity,
  58. child: Stack(children: [
  59. (_cameraController.value.isInitialized) ? CameraPreview(_cameraController) : Container(color: Colors.black, child: const Center(child: CircularProgressIndicator())),
  60. Align(
  61. alignment: Alignment.bottomCenter,
  62. child: Container(
  63. width: double.infinity,
  64. height: MediaQuery.of(context).size.height * 0.15,
  65. decoration: const BoxDecoration(color: Colors.black),
  66. child: Row(crossAxisAlignment: CrossAxisAlignment.center, children: [
  67. Expanded(
  68. child: IconButton(
  69. padding: EdgeInsets.zero,
  70. iconSize: 30,
  71. icon: Icon(_isRearCameraSelected ? Icons.cameraswitch_outlined : Icons.cameraswitch_rounded, color: Colors.white),
  72. onPressed: () {
  73. setState(() => _isRearCameraSelected = !_isRearCameraSelected);
  74. initCamera(widget.cameras![_isRearCameraSelected ? 0 : 1]);
  75. },
  76. )
  77. ),
  78. Expanded(
  79. child: IconButton(
  80. onPressed: takePicture,
  81. iconSize: 60,
  82. padding: EdgeInsets.zero,
  83. constraints: const BoxConstraints(),
  84. icon: const Icon(Icons.circle, color: Colors.white),
  85. )
  86. ),
  87. const Spacer(),
  88. ]),
  89. )),
  90. ]),
  91. ),
  92. )
  93. );
  94. }
  95. }