Files
photobooth_app/lib/modules/camera/camera_screen.dart
2026-01-20 16:34:54 +07:00

117 lines
3.6 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import 'package:photobooth_mobile_app/core/camera/camera_service.dart';
import 'package:photobooth_mobile_app/modules/camera/outside_dim_mask.dart';
import 'package:photobooth_mobile_app/modules/preview/preview_screen.dart';
class CameraCaptureScreen extends StatefulWidget {
final String frameAsset;
const CameraCaptureScreen({super.key, required this.frameAsset});
@override
State<CameraCaptureScreen> createState() => _CameraCaptureScreenState();
}
class _CameraCaptureScreenState extends State<CameraCaptureScreen> {
final CameraService _cameraService = CameraService();
bool _ready = false;
@override
void initState() {
super.initState();
_init();
}
Future<void> _init() async {
await _cameraService.init();
setState(() => _ready = true);
}
@override
void dispose() {
_cameraService.dispose();
super.dispose();
}
Future<void> _takePicture() async {
final XFile raw = await _cameraService.takePicture();
Navigator.pushReplacement(
context,
CupertinoPageRoute(
builder: (_) => PreviewScreen(
imagePath: raw.path,
frameAsset: widget.frameAsset,
isFrontCamera: _cameraService.isFront,
),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: SafeArea(
child: !_ready
? const Center(child: CupertinoActivityIndicator())
: Column(
children: [
Stack(
children: [
CameraPreview(_cameraService.controller!),
const Positioned.fill(child: OutsideDimMask(opacity: 1)),
Positioned.fill(
child: IgnorePointer(
child: AspectRatio(
aspectRatio: 148 / 210,
child: Image.asset(
widget.frameAsset,
fit: BoxFit.contain,
),
),
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
IconButton(
icon: Icon(
Icons.cameraswitch,
color: Colors.white,
size: 28,
),
onPressed: () async {
await _cameraService.switchCamera();
setState(() {});
},
),
GestureDetector(
onTap: _takePicture,
child: Container(
width: 72,
height: 72,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(color: Colors.white, width: 4),
),
),
),
IconButton(
onPressed: () => Navigator.pop(context),
icon: Icon(Icons.close, color: Colors.white, size: 28),
),
],
),
],
),
),
);
}
}