Untitled
unknown
plain_text
2 years ago
9.7 kB
6
Indexable
import 'dart:io';
import 'dart:math';
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:path_provider/path_provider.dart';
import 'package:stop_watch_timer/stop_watch_timer.dart';
import 'package:the_citizen_app/main.dart';
import 'package:the_citizen_app/src/config/base.dart';
import 'package:the_citizen_app/src/helpers/k_log.dart';
import 'package:the_citizen_app/src/helpers/loading.dart';
import 'package:toast/toast.dart';
import '../../call/CameraView.dart';
import '../../config/app_color.dart';
import '../../config/app_theme.dart';
import '../../helpers/image_compress.dart';
import '../../helpers/route.dart';
import '../../widgets/custom_review_widget.dart';
// late List<CameraDescription> cameras;
// ignore: must_be_immutable
class CameraPage extends StatefulWidget {
final String? title;
CameraPage({super.key, required this.title});
@override
_CameraPageState createState() => _CameraPageState();
}
class _CameraPageState extends State<CameraPage> with Base {
final StopWatchTimer _stopWatchTimer = StopWatchTimer(); // Create instance.
late CameraController _cameraController;
Future<void>? cameraValue;
bool isRecoring = false;
bool flash = false;
bool iscamerafront = true;
bool isCaptured = false;
double transform = 0;
@override
void initState() {
super.initState();
initilizeCam();
}
void initilizeCam() {
_cameraController = CameraController(cameras[0], ResolutionPreset.medium);
_cameraController.initialize().then((_) {
if (!mounted) {
return;
}
setState(() {});
}).catchError((Object e) {
if (e is CameraException) {
switch (e.code) {
case 'CameraAccessDenied':
// Handle access errors here.
break;
default:
// Handle other errors here.
break;
}
}
});
}
@override
void dispose() async {
super.dispose();
_cameraController.dispose();
await _stopWatchTimer.dispose();
}
@override
Widget build(BuildContext context) {
// final shoutC = Get.put(ShoutController());
ToastContext().init(context);
// if (!_cameraController.value.isInitialized) {
// return Container();
// }
return Scaffold(
appBar: AppBar(
title: Text(
'Take Photo',
style: TextStyle(
fontFamily: 'Manrope',
fontSize: 18.0,
color: AppTheme.appBarTextColor,
fontWeight: FontWeight.w500,
),
),
// Text('Take photo'),
backgroundColor: AppColors.buttonColor,
actions: [
ReviewIconWidget(screenCode: '0001', screenTitle: 'Camera Screen'),
],
),
body: Stack(
children: [
// FutureBuilder(
// future: cameraValue,
// builder: (context, snapshot) {
// if (snapshot.connectionState == ConnectionState.done) {
// return Container(
// width: MediaQuery.of(context).size.width,
// height: MediaQuery.of(context).size.height,
// child: AspectRatio(
// aspectRatio: _cameraController.value.aspectRatio,
// // aspectRatio: 16 / 9,
// child: CameraPreview(_cameraController),
// ),
// );
// } else {
// return Center(
// child: CircularProgressIndicator(
// color: Colors.white,
// ),
// );
// }
// },
// ),
_cameraController.value.isInitialized
? Container(
width: Get.width,
height: Get.height,
child: AspectRatio(
aspectRatio: _cameraController.value.aspectRatio != null ? _cameraController.value.aspectRatio : 16 / 9,
// aspectRatio: 16 / 9,
child: CameraPreview(_cameraController),
),
)
: Loading(
color: Colors.white,
),
if (isCaptured)
Center(
child: CircularProgressIndicator(),
),
Positioned(
bottom: 0.0,
child: Container(
color: AppColors.buttonColor,
padding: EdgeInsets.only(top: 5, bottom: 5),
width: MediaQuery.of(context).size.width,
child: Column(
children: [
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
IconButton(
icon: Icon(
flash ? Icons.flash_on : Icons.flash_off,
color: Colors.white,
size: 25,
),
highlightColor: Colors.grey.withOpacity(.5),
onPressed: () {
setState(() {
flash = !flash;
});
flash ? _cameraController.setFlashMode(FlashMode.torch) : _cameraController.setFlashMode(FlashMode.off);
},
),
FloatingActionButton(
child: Icon(Icons.camera_alt),
backgroundColor: Colors.blueGrey,
onPressed: () {
if (shoutC.imagefiles.length <= 2 && assignTaskC.imagefiles.length <= 2) {
_onCapturePressed(context);
} else {
back();
Toast.show(
"You can't attach more than three pictures. Delete previously taken pictures to take a new one.",
duration: Toast.lengthLong,
gravity: Toast.bottom,
backgroundColor: Colors.black54,
);
}
},
),
IconButton(
icon: Transform.rotate(
angle: transform,
child: Icon(
Icons.flip_camera_ios_rounded,
color: Colors.white,
size: 25,
),
),
highlightColor: Colors.blueAccent,
onPressed: () async {
setState(() {
iscamerafront = !iscamerafront;
transform = transform + pi;
});
int cameraPos = iscamerafront ? 0 : 1;
_cameraController = CameraController(
cameras[cameraPos],
ResolutionPreset.medium,
);
cameraValue = _cameraController.initialize();
},
),
],
),
SizedBox(height: 4),
],
),
),
),
// if (shoutC.imagefiles.isNotEmpty)
],
),
);
}
void _onCapturePressed(context) async {
// kLog(widget.title);
final targetPath = (await getTemporaryDirectory()).path + '${DateTime.now()}.jpg';
try {
setState(() {
isCaptured = true;
});
// var image = File(path);
XFile picture = await _cameraController.takePicture();
await picture.saveTo(targetPath);
File? image = await compressCameraImageAndGetFile(picture, targetPath);
// File? image = await File(targetPath);
if (widget.title == 'assignTask') {
push(CameraViewPage(
path: image!.path,
path1: image,
title: widget.title!,
));
// assignTaskC.imagefiles.add(image);
}
if (widget.title == 'reportanincident') {
if (shoutC.selectedSubCategory.value!.aiModelUploaded! && shoutC.selectedSubCategory.value!.aiValidationApplied!) {
// kLog('********************nah*************');
// await aiC.imageRecognition(imagePath: image!.path, context: context);
// if (widget.title != 'assignTask')
shoutC.imagefiles.add(image);
} else {
// kLog('********************ggg*************');
push(
CameraViewPage(
path: image!.path,
path1: image,
title: widget.title!,
),
);
// if (widget.title != 'assignTask') shoutC.imagefiles.add(image);
}
}
setState(() {
isCaptured = false;
});
} catch (e) {
setState(() {
isCaptured = false;
});
// If an error occurs, log the error to the console.
kLog(e);
}
}
// IconData _getCameraLensIcon(CameraLensDirection direction) {
// switch (direction) {
// case CameraLensDirection.back:
// return Icons.flip_camera_ios_sharp;
// case CameraLensDirection.front:
// return Icons.flip_camera_ios_sharp;
// case CameraLensDirection.external:
// return Icons.camera_alt;
// default:
// return Icons.flip_camera_ios_sharp;
// }
// }
}
Editor is loading...
Leave a Comment