diff --git a/app/allergy/lib/screens/cameraPage.dart b/app/allergy/lib/screens/cameraPage.dart index 2094504..834a640 100644 --- a/app/allergy/lib/screens/cameraPage.dart +++ b/app/allergy/lib/screens/cameraPage.dart @@ -1,17 +1,114 @@ import 'package:camera/camera.dart'; +import 'package:flutter/material.dart'; import 'package:flutter/src/widgets/framework.dart'; import 'package:flutter/src/widgets/placeholder.dart'; class CameraPage extends StatefulWidget { - const CameraPage({super.key, this.cameras}); - final List? cameras; + const CameraPage((Key?key, required this.cameras)) : super(key: key); + + final ListCameraDescription>? cameras; + @override - State createState() => _CameraPageState(); +State createState() => _CameraPageState(); } class _CameraPageState extends State { + 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 picture = await _cameraController.takePicture(); + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => PreviewPage( + picture: picture, + ))); + } on CameraException catch (e) { + debugPrint('Error occured 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 const Placeholder(); + return Scaffold( + body: SafeArea( + child: Stack(children: [ + (_cameraController.value.isInitialized) + ? CameraPreview(_cameraController) + : Container( + color: Colors.black, + child: const Center(child: CircularProgressIndicator())), + Align( + alignment: Alignment.bottomCenter, + child: Container( + height: MediaQuery.of(context).size.height * 0.20, + decoration: const BoxDecoration( + borderRadius: BorderRadius.vertical(top: Radius.circular(24)), + color: Colors.black), + child: + Row(crossAxisAlignment: CrossAxisAlignment.center, children: [ + Expanded( + child: IconButton( + padding: EdgeInsets.zero, + iconSize: 30, + icon: Icon( + _isRearCameraSelected + ? CupertinoIcons.switch_camera + : CupertinoIcons.switch_camera_solid, + color: Colors.white), + onPressed: () { + setState( + () => _isRearCameraSelected = !_isRearCameraSelected); + initCamera(widget.cameras![_isRearCameraSelected ? 0 : 1]); + }, + )), + Expanded( + child: IconButton( + onPressed: takePicture, + iconSize: 50, + padding: EdgeInsets.zero, + constraints: const BoxConstraints(), + icon: const Icon(Icons.circle, color: Colors.white), + )), + const Spacer(), + ]), + )), + ]), + )); } -} +} \ No newline at end of file diff --git a/app/allergy/lib/screens/preview_page.dart b/app/allergy/lib/screens/preview_page.dart new file mode 100644 index 0000000..a5c5aad --- /dev/null +++ b/app/allergy/lib/screens/preview_page.dart @@ -0,0 +1,23 @@ +import 'package:camera/camera.dart'; +import 'package:flutter/material.dart'; +import 'dart:io'; + +class PreviewPage extends StatelessWidget { + const PreviewPage({Key? key, required this.picture}) : super(key: key); + + final XFile picture; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar(title: const Text('Preview Page')), + body: Center( + child: Column(mainAxisSize: MainAxisSize.min, children: [ + Image.file(File(picture.path), fit: BoxFit.cover, width: 250), + const SizedBox(height: 24), + Text(picture.name) + ]), + ), + ); + } +} \ No newline at end of file