import 'dart:async'; import 'dart:io'; import 'package:camera/camera.dart'; import 'package:cathaypay_mobile/Register/register.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; import '../api/api.dart'; import '../utils/color_custom.dart'; import 'id_card_model.dart'; List CameraApp = []; IDCardModel? idCardFront; String? idCardBack; String idCardBackPath = ""; String idCardFrontPath = ""; // A screen that allows users to take a picture using a given camera. class CameraCardID extends StatefulWidget { CameraCardID({Key? key, required this.isPassport}) : super(key: key); final bool isPassport; @override TakePictureScreenState createState() => TakePictureScreenState(); } class TakePictureScreenState extends State { CameraController? _controller; Future? _initializeControllerFuture; var imageFront = ""; var imageBack = ""; var progressLoading = false; @override void initState() { super.initState(); // To display the current output from the Camera, // create a CameraController. _controller = CameraController( // Get a specific camera from the list of available cameras. CameraApp.first, // Define the resolution to use. ResolutionPreset.medium, ); // Next, initialize the controller. This returns a Future. _initializeControllerFuture = _controller?.initialize(); } @override void dispose() { // Dispose of the controller when the widget is disposed. _controller?.dispose(); super.dispose(); } postImage(File image) { setState(() { progressLoading = true; }); print("postImage"); Api.imageUpload(Api.image_id_card, image).then((value) => {backToResult(value)}); } postImageBack(File image) { setState(() { progressLoading = true; }); print("postImage"); Api.imageUpload(Api.image_id_card_back, image).then((value) => {backToResult(value)}); } postImagePath(File image, bool isBack) { Api.imageUploadPath(Api.uploadImage, image).then((value) => { if (value != null) {imagePathJson(value, isBack)} }); } imagePathJson(value, bool isBack) { value.forEach((v) { print(v.toString()); if (isBack) { setState(() { idCardBackPath = v["file_name"]; }); } else { setState(() { idCardFrontPath = v["file_name"]; }); } }); } refresh() { setState(() {}); } backToResult(value) { print(value); setState(() { progressLoading = false; }); if (value != null) { if (idCardFront == null) { idCardFront = IDCardModel.fromJson(value); } else { idCardBack = value["back_id_card"]; } } if (imageFront.isNotEmpty && imageBack.isNotEmpty) { List images = [imageFront, imageBack]; Navigator.pop(context, images); } } Widget cameraWidget(context) { final size = MediaQuery.of(context).size; final deviceRatio = size.width / size.height; return Stack(children: [ Center( child: Transform.scale( scale: _controller!.value.aspectRatio / deviceRatio, child: new AspectRatio( aspectRatio: _controller!.value.aspectRatio, child: new CameraPreview(_controller!), ), ), ), ]); } previewImage(String file) { showModalBottomSheet( context: context, isScrollControlled: false, useSafeArea: true, builder: (builder) { return Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ Row( children: [ Expanded(child: Container()), IconButton( onPressed: () { Navigator.of(context).pop(); }, icon: const Icon( Icons.clear, color: Colors.grey, ), ), ], ), Expanded( child: Image.file( File(file), fit: BoxFit.fitWidth, )), const SizedBox(height: 10,), InkWell( onTap: () { Navigator.of(context).pop(); setState(() { if (imageFront.isNotEmpty) { imageBack = file; postImageBack(File(imageBack)); postImagePath(File(imageBack), true); } else { imageFront = file; postImage(File(imageFront)); postImagePath(File(imageFront), false); } }); }, child: SizedBox( width: 150, child: Card( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(40), ), child: Container( padding: EdgeInsets.all(10), child: Text( "Confirm".tr(), textAlign: TextAlign.center, style: GoogleFonts.kanit( color: ColorCustom.greyBorder, fontSize: 20, fontWeight: FontWeight.w500, ), ), ), ), ), ), const SizedBox(height: 20,) ], ); }); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, appBar: AppBar( backgroundColor: Colors.transparent, actions: [ IconButton( onPressed: () { Navigator.of(context).pop(); }, icon: const Icon( Icons.clear, color: Colors.grey, ), ), ], elevation: 0, centerTitle: true, title: Text( "Verify your identity".tr(), textAlign: TextAlign.center, style: GoogleFonts.kanit( color: Colors.black, fontSize: 20, ), ), ), // appBar: AppBar(title: Text('Take a picture')), // Wait until the controller is initialized before displaying the // camera preview. Use a FutureBuilder to display a loading spinner // until the controller has finished initializing. body: SafeArea( child: FutureBuilder( future: _initializeControllerFuture, builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.done) { // If the Future is complete, display the preview. return Column( children: [ register_step(index: 1), Expanded( child: Stack( children: [ Container( width: double.infinity, child: new CameraPreview(_controller!), ), Container( width: double.infinity, color: Colors.black, padding: EdgeInsets.all(20), child: Text( 'Your information will be kept confidential in accordance with our Privacy Policy.'.tr(), style: TextStyle(color: Colors.white, fontSize: 18), ), ), Positioned.fill( child: Align( alignment: Alignment.bottomCenter, child: Container( width: double.infinity, color: Colors.black, padding: EdgeInsets.all(20), child: Text( imageFront.isEmpty ? 'Scan the ID card'.tr() : 'Scan the back of your ID card'.tr(), style: TextStyle(color: Colors.white, fontSize: 18), ), ), ), ), progressLoading ? Container( color: Colors.black54, child: Center( child: CircularProgressIndicator(), ), ) : Container() ], ), ), Container( margin: EdgeInsets.all(20), width: double.infinity, child: InkWell( onTap: () async { try { // Ensure that the camera is initialized. await _initializeControllerFuture; // Attempt to take a picture and get the file `image` // where it was saved. final image = await _controller!.takePicture(); previewImage(image.path); // If the picture was taken, display it on a new screen. // Navigator.push( // context, // MaterialPageRoute( // builder: (context) => CameraResultPage( // // Pass the automatically generated path to // // the DisplayPictureScreen widget. // // imagePath: image?.path, // ), // ), // ); } catch (e) { // If an error occurs, log the error to the console. print(e); } }, child: Container( padding: EdgeInsets.all(5), decoration: BoxDecoration( borderRadius: BorderRadius.circular(100), color: Color(0xff9d001b), ), child: Text( "take a photo".tr(), textAlign: TextAlign.center, style: GoogleFonts.kanit( color: Colors.white, fontSize: 20, fontWeight: FontWeight.w500, ), ), ), ), ), /*Container( padding: EdgeInsets.only(left: 10, right: 10, bottom: 20), child: Container( width: double.infinity, margin: EdgeInsets.only(top: 20, left: 20, right: 20), decoration: BoxDecoration( color: ColorCustom.primaryColor, borderRadius: BorderRadius.circular(8)), child: TextButton( onPressed: () async { // Take the Picture in a try / catch block. If anything goes wrong, // catch the error. try { // Ensure that the camera is initialized. await _initializeControllerFuture; // Attempt to take a picture and get the file `image` // where it was saved. final image = await _controller!.takePicture(); // If the picture was taken, display it on a new screen. // Navigator.push( // context, // MaterialPageRoute( // builder: (context) => CameraResultPage( // // Pass the automatically generated path to // // the DisplayPictureScreen widget. // // imagePath: image?.path, // ), // ), // ); } catch (e) { // If an error occurs, log the error to the console. print(e); } }, child: Text( 'แสกนบัตร', style: TextStyle(color: Colors.white, fontSize: 18), ), ), ), ),*/ ], ); } else { // Otherwise, display a loading indicator. return Center(child: CircularProgressIndicator()); } }, ), ), // floatingActionButton: FloatingActionButton( // child: Icon(Icons.camera_alt), // // Provide an onPressed callback. // onPressed: () async { // // Take the Picture in a try / catch block. If anything goes wrong, // // catch the error. // try { // // Ensure that the camera is initialized. // await _initializeControllerFuture; // // // Attempt to take a picture and get the file `image` // // where it was saved. // final image = await _controller.takePicture(); // // // If the picture was taken, display it on a new screen. // Navigator.push( // context, // MaterialPageRoute( // builder: (context) => DisplayPictureScreen( // // Pass the automatically generated path to // // the DisplayPictureScreen widget. // imagePath: image?.path, // ), // ), // ); // } catch (e) { // // If an error occurs, log the error to the console. // print(e); // } // }, // ), ); } }