Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 102 additions & 5 deletions app/allergy/lib/screens/cameraPage.dart
Original file line number Diff line number Diff line change
@@ -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<CameraDescription>? cameras;
const CameraPage((Key?key, required this.cameras)) : super(key: key);

final ListCameraDescription>? cameras;

@override
State<CameraPage> createState() => _CameraPageState();
State<CameraPage> createState() => _CameraPageState();
}

class _CameraPageState extends State<CameraPage> {
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(),
]),
)),
]),
));
}
}
}
23 changes: 23 additions & 0 deletions app/allergy/lib/screens/preview_page.dart
Original file line number Diff line number Diff line change
@@ -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)
]),
),
);
}
}