Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bkhezry/801e56486bdbb613aed7b97f961051b5 to your computer and use it in GitHub Desktop.
Save bkhezry/801e56486bdbb613aed7b97f961051b5 to your computer and use it in GitHub Desktop.
import 'dart:async';
import 'dart:io';
import 'package:camera/camera.dart';
import 'package:get/get.dart';
class CapturePersonalPictureController extends GetxController {
CapturePersonalPictureController({required this.returnDataFunction});
late CameraController cameraController;
Future<void>? initializeControllerFuture;
final Function(File file) returnDataFunction;
FlashMode flashMode = FlashMode.off;
@override
Future<void> onInit() async {
super.onInit();
await initCamera();
}
Future<void> initCamera() async {
final cameras = await availableCameras();
final selectedCamera = cameras.firstWhere((element) => element.lensDirection == CameraLensDirection.front);
cameraController = CameraController(
selectedCamera,
ResolutionPreset.medium,
imageFormatGroup: ImageFormatGroup.jpeg,
enableAudio: false,
);
initializeControllerFuture = cameraController.initialize();
update();
}
@override
void onClose() {
cameraController.dispose();
super.onClose();
Get.closeAllSnackbars();
}
Future<void> takePicture() async {
if (cameraController.value.isInitialized) {
final file = await cameraController.takePicture();
Get.back();
returnDataFunction(File(file.path));
}
}
Future<void> toggleFlash() async {
if (cameraController.value.flashMode == FlashMode.off) {
await cameraController.setFlashMode(FlashMode.torch);
flashMode = FlashMode.torch;
} else {
await cameraController.setFlashMode(FlashMode.off);
flashMode = FlashMode.off;
}
update();
}
}
import 'dart:io';
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'capture_personal_picture_controller.dart';
class CapturePersonalPictureScreen extends StatelessWidget {
const CapturePersonalPictureScreen({
required this.returnDataFunction,
required this.visualTutorialFunction,
required this.audioTutorialFunction,
required this.stopAudioPlayer,
Key? key,
}) : super(key: key);
final Function(File file) returnDataFunction;
final Function() visualTutorialFunction;
final Function() audioTutorialFunction;
final Function() stopAudioPlayer;
@override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.rtl,
child: GetBuilder<CapturePersonalPictureController>(
init: CapturePersonalPictureController(returnDataFunction: returnDataFunction),
builder: (controller) {
return Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
child: FutureBuilder<void>(
future: controller.initializeControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
// If the Future is complete, display the preview.
return Stack(
alignment: Alignment.center,
children: [
CameraPreview(controller.cameraController),
Stack(
fit: StackFit.expand,
children: [
ColorFiltered(
colorFilter: ColorFilter.mode(Colors.black.withOpacity(0.6), BlendMode.srcOut),
// This one will create the magic
child: Stack(
fit: StackFit.expand,
children: [
Container(
decoration: const BoxDecoration(
color: Colors.black12,
), // This one will handle background + difference out
),
Align(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: ClipOval(
child: Container(
width: Get.width * 0.6,
height: Get.height * 0.37,
decoration: const BoxDecoration(
color: Colors.red,
),
),
),
),
),
],
),
),
],
),
Positioned(
bottom: 0.0,
child: Column(
children: [
InkWell(
onTap: () async {
await stopAudioPlayer();
controller.takePicture();
},
child: SvgPicture.asset(
'assets/icons/ic_capture.svg',
height: 80.0,
),
),
const SizedBox(height: 12.0),
Container(
width: Get.width,
decoration: const BoxDecoration(color: Colors.blueGrey),
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 32.0, vertical: Get.height / 36),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: ElevatedButton(
onPressed: () async {
await stopAudioPlayer();
visualTutorialFunction();
},
style: ElevatedButton.styleFrom(
padding: EdgeInsets.zero,
elevation: 0,
backgroundColor: Colors.white24,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SvgPicture.asset(
'assets/icons/ic_capture_visual_tutorial.svg',
width: 24.0,
colorFilter:
const ColorFilter.mode(Colors.white, BlendMode.srcIn),
),
const SizedBox(width: 8.0),
const Text(
'راهنمای تصویری',
style: TextStyle(
fontSize: 12.0,
fontWeight: FontWeight.w400,
color: Colors.white,
),
),
],
),
),
),
const SizedBox(width: 12.0),
Expanded(
child: ElevatedButton(
onPressed: () {
audioTutorialFunction();
},
style: ElevatedButton.styleFrom(
padding: EdgeInsets.zero,
elevation: 0,
backgroundColor: Colors.white24,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SvgPicture.asset(
'assets/icons/ic_capture_voice_tutorial.svg',
width: 24.0,
),
const SizedBox(width: 8.0),
const Text(
'راهنمای صوتی',
style: TextStyle(
fontSize: 12.0,
fontWeight: FontWeight.w400,
color: Colors.white,
),
),
],
),
),
),
],
),
),
),
],
),
),
Positioned(
top: 0.0,
child: SizedBox(
width: Get.width,
child: Column(
children: [
Container(
width: Get.width,
decoration: const BoxDecoration(
borderRadius: BorderRadius.vertical(bottom: Radius.circular(10.0)),
color: Colors.blueGrey,
),
child: Center(
child: Padding(
padding:
const EdgeInsets.only(bottom: 20.0, top: 44.0, left: 24.0, right: 24.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Material(
color: Colors.transparent,
child: InkWell(
onTap: () {
Get.back();
},
borderRadius: BorderRadius.circular(40),
child: Padding(
padding: const EdgeInsets.all(4.0),
child: SvgPicture.asset(
'assets/icons/ic_close_bold.svg',
height: 24.0,
width: 24.0,
colorFilter:
const ColorFilter.mode(Colors.white, BlendMode.srcIn),
),
),
),
),
Material(
color: Colors.transparent,
child: InkWell(
onTap: () {
controller.toggleFlash();
},
borderRadius: BorderRadius.circular(40),
child: Padding(
padding: const EdgeInsets.all(4.0),
child: SvgPicture.asset(
controller.flashMode == FlashMode.off
? 'assets/icons/ic_flash_slash_bold.svg'
: 'assets/icons/ic_flash_bold.svg',
height: 24.0,
width: 24.0,
colorFilter:
const ColorFilter.mode(Colors.white, BlendMode.srcIn),
),
),
),
),
],
),
),
),
),
const SizedBox(height: 32.0),
const Padding(
padding: EdgeInsets.symmetric(horizontal: 16.0),
child: Text(
'لطفا چهره خود را در کادر مشخص شده قرار دهید و دکمه را انتخاب نمایید',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
color: Colors.white70,
),
),
),
],
),
),
),
],
);
} else {
// Otherwise, display a loading indicator.
return const Center(child: CircularProgressIndicator());
}
},
),
),
],
),
);
}),
);
}
}
Future<void> showTakePersonalPhotoScreen() async {
await stopPlayer();
Get.to(() => CapturePersonalPictureScreen(
returnDataFunction: (File file) async {
stopPlayer();
final CroppedFile? croppedFile = await ImageCropper().cropImage(
sourcePath: file.path,
aspectRatio: const CropAspectRatio(ratioY: 4, ratioX: 3),
compressQuality:
await AppUtil.getFileCompressQuality(file: file, maxFileSize: Constants.personalImageMaxSize),
uiSettings: [
AndroidUiSettings(
toolbarTitle: 'برش عکس',
toolbarColor: Colors.deepOrange,
toolbarWidgetColor: Colors.white,
initAspectRatio: CropAspectRatioPreset.ratio4x3,
lockAspectRatio: false),
IOSUiSettings(
aspectRatioLockDimensionSwapEnabled: true,
)
],
);
if (croppedFile != null) {
final File tempUserFile = File(croppedFile.path);
update();
Get.to(() => TakePersonalPhotoSampleScreen(
userFile: tempUserFile,
returnCallback: (File? file) {
selectedPersonalPicture = file;
update();
},
));
}
},
audioTutorialFunction: () {
playSound(helperVoiceType: HelperVoiceType.personalPhotoCamera);
},
visualTutorialFunction: () {
showHelperScreen(helperType: HelperType.personalImage);
},
stopAudioPlayer: stopPlayer,
));
}
environment:
sdk: ">=3.2.0 <4.0.0"
dependencies:
flutter:
sdk: flutter
camera: ^0.10.5+5
camera_android: ^0.10.8+14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment