Untitled

mail@pastecode.io avatar
unknown
dart
a year ago
7.3 kB
1
Indexable
Never
import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';

import '../../../../services/api_service.dart';
import '../../../../utilities/constants/app_colors.dart';

class MeetingRequestController extends GetxController {
  // final AuthManager authManager = Get.find();
  final ApiService apiService = Get.find();
  final formKey = GlobalKey<FormState>();

  TextEditingController meetingTitleController = TextEditingController();
  TextEditingController meetingDescriptionController = TextEditingController();
  RxBool isFileAttached = false.obs;
  RxBool isAddedAlready = false.obs;
  final RxList<File> listOfAttachments = <File>[].obs;
  Rx<FilePickerResult?> result = Rx<FilePickerResult?>(null);

  PlatformFile? attachment;

  RxInt index = 0.obs;

  @override
  void onInit() {
    meetingTitleController = TextEditingController();
    meetingDescriptionController = TextEditingController();
    super.onInit();
  }

  void fileAddedAlready() {
    isAddedAlready.value = true;
  }

  Future<void> pickPDFfile() async {
    if (await Permission.storage.request().isGranted) {
      result.value = await FilePicker.platform.pickFiles(
        type: FileType.custom,
        allowMultiple: true,
        allowedExtensions: ['pdf'],
      );
      // isFileAttached = true.obs;
      if (result.value != null) {
        listOfAttachments
            .addAll(result.value!.names.map((path) => File(path!)).toList());
      }

      attachment = result.value!.files.first;
    } else {
      Get.dialog(AlertDialog(
        title: Text('permission required'.tr,
            style: Get.textTheme.headlineSmall
                ?.copyWith(color: AppColors.denim, fontSize: 17)),
        content: Text(
          'please grant storage permission to use file picker.'.tr,
          style: Get.textTheme.headlineSmall
              ?.copyWith(color: AppColors.denim, fontSize: 14),
        ),
        actions: [
          TextButton(
            onPressed: () => Get.back(),
            child: Text('cancel'.tr,
                style: Get.textTheme.headlineSmall
                    ?.copyWith(color: AppColors.denim, fontSize: 14)),
          ),
          TextButton(
            onPressed: () => openAppSettings(),
            child: Text('SETTINGS'.tr,
                style: Get.textTheme.headlineSmall
                    ?.copyWith(color: AppColors.denim, fontSize: 14)),
          ),
        ],
      ));
    }
    index.value = 0;

    //viewFile(file!);
  }

  // viewFile(PlatformFile file) {
  //   OpenFile.open(file.name);
  // }

  pickPNGfile() async {
    if (await Permission.storage.request().isGranted) {
      result.value = await FilePicker.platform.pickFiles(
          type: FileType.custom,
          allowedExtensions: ['png'],
          allowMultiple: true);
      // isFileAttached = true.obs;

      if (result.value != null) {
        File file = File(result.value!.files.single.path!);
        listOfAttachments.add(file);
      }

      attachment = result.value!.files.first;
    } else {
      Get.dialog(AlertDialog(
        title: Text('permission required'.tr,
            style: Get.textTheme.headlineSmall
                ?.copyWith(color: AppColors.denim, fontSize: 17)),
        content: Text(
          'please grant storage permission to use file picker.'.tr,
          style: Get.textTheme.headlineSmall
              ?.copyWith(color: AppColors.denim, fontSize: 14),
        ),
        actions: [
          TextButton(
            onPressed: () => Get.back(),
            child: Text('cancel'.tr,
                style: Get.textTheme.headlineSmall
                    ?.copyWith(color: AppColors.denim, fontSize: 14)),
          ),
          TextButton(
            onPressed: () => openAppSettings(),
            child: Text('SETTINGS'.tr,
                style: Get.textTheme.headlineSmall
                    ?.copyWith(color: AppColors.denim, fontSize: 14)),
          ),
        ],
      ));
    }
    index.value = 1;
  }

  pickMP3file() async {
    if (await Permission.storage.request().isGranted) {
      result.value = await FilePicker.platform.pickFiles(
          type: FileType.custom,
          allowedExtensions: ['mp3'],
          allowMultiple: true);
      // isFileAttached = true.obs;

      if (result.value != null) {
        // File file = File(result.value!.files.single.path!);
        listOfAttachments
            .addAll(result.value!.names.map((path) => File(path!)).toList());
      }

      attachment = result.value!.files.first;
    } else {
      Get.dialog(AlertDialog(
        title: Text('permission required'.tr,
            style: Get.textTheme.headlineSmall
                ?.copyWith(color: AppColors.denim, fontSize: 17)),
        content: Text(
          'please grant storage permission to use file picker.'.tr,
          style: Get.textTheme.headlineSmall
              ?.copyWith(color: AppColors.denim, fontSize: 14),
        ),
        actions: [
          TextButton(
            onPressed: () => Get.back(),
            child: Text('cancel'.tr,
                style: Get.textTheme.headlineSmall
                    ?.copyWith(color: AppColors.denim, fontSize: 14)),
          ),
          TextButton(
            onPressed: () => openAppSettings(),
            child: Text('SETTINGS'.tr,
                style: Get.textTheme.headlineSmall
                    ?.copyWith(color: AppColors.denim, fontSize: 14)),
          ),
        ],
      ));
    }
    index.value = 2;
  }

  pickAnyFile() async {
    if (await Permission.storage.request().isGranted) {
      result.value = await FilePicker.platform
          .pickFiles(type: FileType.any, allowMultiple: true);
      // isFileAttached = true.obs;

      if (result.value != null) {
        // File file = File(result.value!.files.single.path!);
        listOfAttachments
            .addAll(result.value!.names.map((path) => File(path!)).toList());
      }

      attachment = result.value!.files.first;
    } else {
      Get.dialog(AlertDialog(
        title: Text('permission required'.tr,
            style: Get.textTheme.headlineSmall
                ?.copyWith(color: AppColors.denim, fontSize: 17)),
        content: Text(
          'please grant storage permission to use file picker.'.tr,
          style: Get.textTheme.headlineSmall
              ?.copyWith(color: AppColors.denim, fontSize: 14),
        ),
        actions: [
          TextButton(
            onPressed: () => Get.back(),
            child: Text('cancel'.tr,
                style: Get.textTheme.headlineSmall
                    ?.copyWith(color: AppColors.denim, fontSize: 14)),
          ),
          TextButton(
            onPressed: () => openAppSettings(),
            child: Text('SETTINGS'.tr,
                style: Get.textTheme.headlineSmall
                    ?.copyWith(color: AppColors.denim, fontSize: 14)),
          ),
        ],
      ));
    }
    index.value = 3;
  }
}