import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:vnoteapp/pages/contract/package_list/controller.dart';

import '../../../../consts/styles.dart';

class CapturePortraitPage extends GetView<ServicePackageContractController> {
  const CapturePortraitPage({super.key});
  Future<String> convertImageToBase64(XFile image) async {
    List<int> imageBytes = await image.readAsBytes();
    String base64Image = base64Encode(imageBytes);
    return base64Image;
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () async {
        XFile? image =
            await ImagePicker().pickImage(source: ImageSource.camera);
        controller.state.userImage = image;
        String base64Image = await convertImageToBase64(image!);
        controller.base64Image = base64Image;
      },
      child: Center(
        child: Container(
          width: 300,
          height: 300,
          decoration: BoxDecoration(
            border: Border.all(
              color: Colors.grey,
            ),
            borderRadius: GlobalStyles.borderRadius,
          ),
          child: controller.state.userImage != null
              ? Image.file(
                  File(controller.state.userImage!.path),
                )
              : const Icon(
                  Icons.add,
                  size: 100,
                  color: Colors.grey,
                ),
        ),
      ),
    );
  }
}
