Untitled

mail@pastecode.io avatar
unknown
dart
2 years ago
15 kB
3
Indexable
Never
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_absolute_path/flutter_absolute_path.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:mobile/app/Dto/add_supplier_dto.dart';
import 'package:mobile/app/Dto/upload_supplier_document_dto.dart';
import 'package:mobile/app/global_widgets/circular_block_button_widget.dart';
import 'package:mobile/app/modules/payment/controllers/payment_info_controller.dart';
import 'package:mobile/app/modules/payment/views/edit_payment_info_view.dart';
import 'package:mobile/app/modules/payment/widgets/page_step_container.dart';
import 'package:mobile/app/providers/api_provider.dart';
import 'package:mobile/common/ui.dart';
import 'package:multi_image_picker2/multi_image_picker2.dart';

class LicenseView extends GetView<PaymentController> {
  int pageStep = 3;
  final PaymentController controller = Get.find();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Ui.parseColor("#F5F5F5"),
      appBar: AppBar(
        elevation: 0,
        backgroundColor: Colors.transparent,
        brightness: Brightness.light,
        iconTheme: new IconThemeData(color: Colors.black),
        title: Text(
          "Payment Info".tr,
          style: Get.theme.textTheme.headline4
              .copyWith(fontWeight: FontWeight.bold),
        ),
        centerTitle: true,
      ),
      body: Obx(
        () => Column(
          children: [
            Container(
              height: Get.height * 0.1,
              alignment: Alignment.center,
              child: Stack(
                children: [
                  Container(
                      alignment: Alignment.center,
                      width: Get.width * 0.8,
                      child: Divider(thickness: 1.0)),
                  Container(
                    width: Get.width * 0.8,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        PageStepContainer(
                          step: 1,
                          currentStep: pageStep,
                        ),
                        PageStepContainer(
                          step: 2,
                          currentStep: pageStep,
                        ),
                        PageStepContainer(
                          step: 3,
                          currentStep: pageStep,
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(
              height: 35.0,
            ),
            Container(
              padding:
                  EdgeInsets.only(top: 14, bottom: 14, left: 20, right: 20),
              margin: EdgeInsets.only(left: 40, right: 40),
              width: double.infinity,
              decoration: BoxDecoration(
                  color: Get.theme.primaryColor,
                  borderRadius: BorderRadius.vertical(top: Radius.circular(15)),
                  boxShadow: [
                    BoxShadow(
                        color: Get.theme.focusColor.withOpacity(0.1),
                        blurRadius: 10,
                        offset: Offset(0, 5)),
                  ],
                  border: Border.all(
                      color: Get.theme.focusColor.withOpacity(0.05))),
              alignment: Alignment.center,
              child: Text(
                "Licence".tr,
                style: Get.theme.textTheme.headline4.merge(
                  TextStyle(
                      color: Colors.black,
                      fontWeight: FontWeight.bold,
                      fontSize: 15),
                ),
              ),
            ),
            Container(
              padding:
                  EdgeInsets.only(top: 14, bottom: 14, left: 20, right: 20),
              margin: EdgeInsets.only(left: 40, right: 40),
              width: double.infinity,
              decoration: BoxDecoration(
                color: Get.theme.primaryColor,
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Icon(
                    Icons.info,
                    color: Get.theme.focusColor,
                  ),
                  SizedBox(
                    width: 10.0,
                  ),
                  Expanded(
                    child: Text(
                      "Upload freelance licence".tr,
                      style: Get.textTheme.headline4
                          .copyWith(fontWeight: FontWeight.bold),
                    ),
                  ),
                ],
              ),
            ),
            Container(
              padding:
                  EdgeInsets.only(top: 14, bottom: 14, left: 20, right: 20),
              margin: EdgeInsets.only(left: 40, right: 40),
              width: double.infinity,
              decoration: BoxDecoration(
                color: Get.theme.primaryColor,
              ),
              child: Obx(() => controller.licenceFile.value.path.trim() != ''
                  ? Card(
                      elevation: 2,
                      child: ListTile(
                        title: Text(
                          controller.licenceFile.value.path.substring(controller
                                  .licenceFile.value.path
                                  .lastIndexOf('/') +
                              1),
                          style: TextStyle(color: Get.theme.focusColor),
                        ),
                        trailing: IconButton(
                          onPressed: () {
                            controller.licenceFile.value = File('');
                          },
                          icon: Icon(Icons.delete),
                        ),
                      ),
                    )
                  : Container()),
            ),
            Container(
              padding:
                  EdgeInsets.only(top: 14, bottom: 14, left: 20, right: 20),
              margin: EdgeInsets.only(left: 40, right: 40),
              width: double.infinity,
              decoration: BoxDecoration(
                color: Get.theme.primaryColor,
              ),
              child: Visibility(
                visible: controller.licenceFile.value.path.trim() == '',
                child: CircularBlockButtonWidget(
                  color: Get.theme.focusColor,
                  text: Text(
                    "Select".tr,
                    style: Get.textTheme.bodyText2.copyWith(
                        color: Colors.white, fontWeight: FontWeight.bold),
                  ),
                  onPressed: () async {
                            showDialog(
                            context: context,
                            builder: (BuildContext context) => CupertinoAlertDialog(
                              title: Text("License".tr),
                              content: Text('Choose photo/file for your license'.tr),
                              actions: [
                                CupertinoDialogAction(
                                  isDefaultAction: true,
                                  child: Text('Select from photos'.tr),
                                  onPressed: () async{
                                    Navigator.pop(context);
                      
                                    List<Asset> profilePic1 = <Asset>[].obs;

                                    await MultiImagePicker.pickImages(
                                        maxImages: 1,
                                        enableCamera: true,
                                        selectedAssets: profilePic1,
                                        cupertinoOptions: CupertinoOptions(
                                          takePhotoIcon: "chat",
                                        ),
                                        materialOptions: MaterialOptions(
                                          actionBarColor: "#339A58",
                                          statusBarColor: "#339A58",
                                          allViewTitle: "All Photos",
                                          useDetailsView: false,
                                          selectCircleStrokeColor: "#339A58",
                                        ),
                                      ).then((value) async {

                                        final filePath = await FlutterAbsolutePath.getAbsolutePath(value[0].identifier);

                                        File tempFile = File(filePath);

                                        if (tempFile.existsSync()) {

                                          File cropped = await ImageCropper.cropImage(
                                              sourcePath: tempFile.path,
                                              compressQuality: 100,
                                              compressFormat: ImageCompressFormat.jpg,
                                              androidUiSettings: AndroidUiSettings(
                                                  toolbarColor: Get.theme.primaryColor,
                                                  toolbarTitle: "Crop Image",
                                                  statusBarColor: Colors.black,
                                                  backgroundColor: Colors.white10,
                                                  activeControlsWidgetColor: Get.theme.focusColor));
                                          
                                          controller.licenceFile.value = cropped;

                                        }

                                      });
                                  },
                                ),
                                CupertinoDialogAction(
                                  isDefaultAction: true,
                                  child: Text('Select from files'.tr),
                                  onPressed: () async {
                                    Navigator.of(context).pop();
                                    print('Licence File Selection');
                                    FilePickerResult result =
                                        await FilePicker.platform.pickFiles(
                                      type: FileType.custom,
                                      allowedExtensions: [
                                        'jpg',
                                        'pdf',
                                        'png',
                                        'jpeg',
                                      ],
                                    );

                                    if (result != null) {
                                      controller.licenceFile.value =
                                          File(result.files.single.path);
                                    } else {
                                      // User canceled the picker
                                    }
                                  },
                                ),
                                CupertinoDialogAction(
                                  isDefaultAction: true,
                                  child: Text('Cancel'.tr),
                                  onPressed: () async {
                                    Navigator.of(context).pop();
                                  },
                                )
                              ],
                            ),
                          );
                            
                          },
                ).paddingSymmetric(horizontal: 50, vertical: 10),
              ),
            ),
            Container(
              padding:
                  EdgeInsets.only(top: 14, bottom: 14, left: 20, right: 20),
              margin: EdgeInsets.only(left: 40, right: 40),
              width: double.infinity,
              decoration: BoxDecoration(
                  color: Get.theme.primaryColor,
                  borderRadius:
                      BorderRadius.vertical(bottom: Radius.circular(15)),
                  boxShadow: [
                    BoxShadow(
                        color: Get.theme.focusColor.withOpacity(0.1),
                        blurRadius: 10,
                        offset: Offset(0, 5)),
                  ],
                  border: Border.all(
                      color: Get.theme.focusColor.withOpacity(0.05))),
              alignment: Alignment.center,
              child: Container(
                width: Get.width,
                child: CircularBlockButtonWidget(
                  onPressed: () async {
                    if (controller.licenceFile.value.path.trim() == '') {
                      Get.showSnackbar(Ui.ErrorSnackBar(
                        title: "Failed".tr,
                        message: "enterLicenseFile".tr,
                      ));
                      return;
                    }

                    UploadSupplierDocument licenceData = UploadSupplierDocument(
                        filePath: controller.licenceFile.value.path,
                        fileType: 1,
                        fileName: controller.licenceFile.value.path.substring(
                            controller.licenceFile.value.path.lastIndexOf('/') +
                                1));

                    Get.dialog(
                      Center(
                        child: CircularProgressIndicator(
                          valueColor: AlwaysStoppedAnimation<Color>(
                            Get.theme.accentColor,
                          ),
                        ),
                      ),
                      barrierDismissible: true,
                    );

                    await ApiProvider(httpClient: Dio())
                        .uploadSupllierDocsFromAccountSettings(
                            licenceData,
                            controller.currentUser.appUser.value.provider
                                .supplierCode,
                            controller.currentUser.appUser.value.id);
                  },
                  color: Get.theme.accentColor,
                  text: Text(
                    "Next".tr,
                    style: Get.theme.textTheme.headline5
                        .merge(TextStyle(color: Get.theme.primaryColor)),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}