cameraOrGallery static method

Future<List<ImagePickerData>> cameraOrGallery(
  1. BuildContext context, {
  2. bool useCropper = false,
  3. bool useComprasor = false,
  4. int length = 1,
  5. bool? bottomSheetUI,
  6. int? maxWidth,
  7. int? maxHeight,
  8. String cameraText = 'Camera',
  9. String galleryText = 'Gallery',
  10. String toolbarFolderTitle = "Folder",
  11. String toolbarImageTitle = "Tap to select",
  12. String toolbarDoneButtonText = "DONE",
  13. bool usecameraInGallery = false,
  14. bool enableLogInGallery = false,
  15. bool folderModeGallery = true,
  16. List<ImagePickerData> oldImages = const <ImagePickerData>[],
  17. List<ImagePickerData> excloudImages = const <ImagePickerData>[],
  18. CropAspectRatio? croperAspectRatio,
  19. List<CropAspectRatioPreset> croperAspectRatioPresets = const [CropAspectRatioPreset.original, CropAspectRatioPreset.square, CropAspectRatioPreset.ratio3x2, CropAspectRatioPreset.ratio4x3, CropAspectRatioPreset.ratio16x9],
  20. CropStyle cropStyle = CropStyle.rectangle,
  21. ImageCompressFormat compressCroperFormat = ImageCompressFormat.jpg,
  22. int compressCroperQuality = 90,
  23. AndroidUiSettings? androidCroperUiSettings,
  24. IOSUiSettings? iosCroperUiSettings,
})

Implementation

static Future<List<ImagePickerData>> cameraOrGallery(
  BuildContext context, {
  bool useCropper = false,
  bool useComprasor = false,
  int length = 1,
  bool? bottomSheetUI,
  int? maxWidth,
  int? maxHeight,
  String cameraText = 'Camera',
  String galleryText = 'Gallery',
  String toolbarFolderTitle = "Folder",
  String toolbarImageTitle = "Tap to select",
  String toolbarDoneButtonText = "DONE",
  bool usecameraInGallery = false,
  bool enableLogInGallery = false,
  bool folderModeGallery = true,
  List<ImagePickerData> oldImages = const <ImagePickerData>[],
  List<ImagePickerData> excloudImages = const <ImagePickerData>[],
  CropAspectRatio? croperAspectRatio,
  List<CropAspectRatioPreset> croperAspectRatioPresets = const [
    CropAspectRatioPreset.original,
    CropAspectRatioPreset.square,
    CropAspectRatioPreset.ratio3x2,
    CropAspectRatioPreset.ratio4x3,
    CropAspectRatioPreset.ratio16x9
  ],
  CropStyle cropStyle = CropStyle.rectangle,
  ImageCompressFormat compressCroperFormat = ImageCompressFormat.jpg,
  int compressCroperQuality = 90,
  AndroidUiSettings? androidCroperUiSettings,
  IOSUiSettings? iosCroperUiSettings,
}) async {
  assert(maxWidth == null || maxWidth > 0);
  assert(maxHeight == null || maxHeight > 0);
  assert(compressCroperQuality >= 0 && compressCroperQuality <= 100);
  // assert(useCropper != null);

  await CustomMultiImagepicker2._initCams();

  void camera() async {
    await per(type: _Handlerpermission.cam);
    final imageFile = await Navigator.of(context)
        .push<List<ImagePickerData>>(MaterialPageRoute(
            builder: (context) => _MyImagePicker(
                  useCroper: useCropper,
                  useComprasor: useComprasor,
                  androidUiSettings: androidCroperUiSettings,
                  aspectRatio: croperAspectRatio,
                  aspectRatioPresets: croperAspectRatioPresets,
                  compressFormat: compressCroperFormat,
                  compressQuality: compressCroperQuality,
                  cropStyle: cropStyle,
                  iosUiSettings: iosCroperUiSettings,
                  length: length,
                  oldImages: oldImages,
                  maxHeight: maxHeight,
                  maxWidth: maxWidth,
                )));
    Navigator.of(context).pop(imageFile);
  }

  void gallery() async {
    await per(
        type: usecameraInGallery
            ? _Handlerpermission.both
            : _Handlerpermission.storage);
    final List<dynamic> imgs = [];
    try {
      final resulte = await _channel.invokeListMethod('imagepicker', {
        'length': length,
        'toolbarFolderTitle': toolbarFolderTitle,
        'toolbarImageTitle': toolbarImageTitle,
        'toolbarDoneButtonText': toolbarDoneButtonText,
        'oldImages': oldImages.map((i) => i._orginal.map).toList(),
        'exuteImages': excloudImages.map((i) => i.orginal.map).toList(),
        'camera': usecameraInGallery,
        'enableLog': enableLogInGallery,
        'folderMode': folderModeGallery
      });
      if (resulte?.isNotEmpty ?? false) imgs.addAll(resulte!);
      // _channel.invokeMethod('end');
    } catch (err) {
      // _channel.invokeMethod('end');
      print(err);
    }
    final listOfImages = imgs
        .map((i) => ImagePickerData.frmMap(Map<String, dynamic>.from(i)))
        .toList();
    final dirti = await getTemporaryDirectory();
    for (ImagePickerData i in listOfImages) {
      final index =
          oldImages.indexWhere((oi) => oi.id == i.id && oi.url != null);
      if (index > -1) i.url = oldImages[index].url;
      // if (oldImages.any((oi) => oi.id == i.id && oi.url != null)) {
      // i.url = oldImages
      //         .firstWhere((oi) => oi.id == i.id && oi.url != null,
      //             orElse: () => null)
      //         ?.url ??
      //     '';
      // }
      if (useCropper) {
        if (oldImages.any((oi) => oi.id == i.id && oi.icCropped))
          i._icCropped = true; // ._crop(i.file);
        else {
          final file = await ImageCropper.cropImage(
            sourcePath: i.path,
            androidUiSettings: androidCroperUiSettings,
            aspectRatio: croperAspectRatio,
            aspectRatioPresets: croperAspectRatioPresets,
            compressFormat: compressCroperFormat,
            compressQuality: compressCroperQuality,
            cropStyle: cropStyle,
            iosUiSettings: iosCroperUiSettings,
            maxHeight: maxHeight,
            maxWidth: maxWidth,
          );
          if (file?.existsSync() ?? false)
            i._crop(file!);
          else {
            final fileName = basenameWithoutExtension(i.path);
            final targetdir = '${dirti.path}/$fileName${i.id}.jpg';
            final file2 = await FlutterImageCompress.compressAndGetFile(
              i.path,
              targetdir,
              format: CompressFormat.jpeg,
              quality: compressCroperQuality,
              minHeight: maxHeight == null ? 1080 : maxHeight,
              minWidth: maxHeight == null ? 1920 : maxHeight,
            );
            if (file2?.existsSync() ?? false) i._crop(file2!);
          }
        }
      } else if (useComprasor) {
        // for (ImagePickerData i in listOfImages) {
        if (oldImages.any((oi) => oi.id == i.id && oi.icCropped))
          i._icCropped = true; //._crop(i.file);
        else {
          final fileName = basenameWithoutExtension(i.path);
          final targetdir = '${dirti.path}/$fileName${i.id}.jpg';
          final file = await FlutterImageCompress.compressAndGetFile(
            i.path,
            targetdir,
            format: CompressFormat.jpeg,
            quality: compressCroperQuality,
            minHeight: maxHeight == null ? 1080 : maxHeight,
            minWidth: maxHeight == null ? 1920 : maxHeight,
          );
          if (file?.existsSync() ?? false) i._crop(file!);
        }
      }
    }
    Navigator.of(context).pop(listOfImages);
  }

  final builder = (context) {
    return ClipRRect(
      borderRadius: BorderRadius.vertical(top: Radius.circular(25)),
      child: BottomSheet(
        elevation: 0,
        enableDrag: true,
        backgroundColor: Colors.black12,
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.vertical(top: Radius.circular(25))),
        builder: (BuildContext context) {
          return Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Text(
                    '   Upload From',
                    style:
                        TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
                  ),
                  IconButton(
                    tooltip: 'Close',
                    icon: Icon(Icons.close),
                    onPressed: Navigator.of(context).pop,
                  ),
                ],
              ),
              Container(
                decoration: BoxDecoration(
                  color: Theme.of(context).dialogBackgroundColor,
                  borderRadius:
                      BorderRadius.vertical(top: Radius.circular(25)),
                  boxShadow: [
                    BoxShadow(
                        offset: Offset(0, 0),
                        blurRadius: 20,
                        spreadRadius: -5,
                        color: Colors.black),
                  ],
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    Expanded(
                      child: ButtonTheme(
                        buttonColor:
                            Theme.of(context).scaffoldBackgroundColor,
                        height: 70,
                        shape: RoundedRectangleBorder(
                          borderRadius:
                              BorderRadius.vertical(top: Radius.circular(25)),
                        ),
                        // minWidth: 70;
                        child: FlatButton.icon(
                          textColor: Theme.of(context).accentColor,
                          icon: Icon(Icons.camera_alt),
                          label: Text('Camera'),
                          onPressed: camera,
                        ),
                      ),
                    ),
                    Expanded(
                      child: ButtonTheme(
                        height: 70,
                        // minWidth: 70;
                        shape: RoundedRectangleBorder(
                          borderRadius:
                              BorderRadius.vertical(top: Radius.circular(25)),
                        ),
                        buttonColor:
                            Theme.of(context).scaffoldBackgroundColor,
                        child: FlatButton.icon(
                          textColor: Theme.of(context).accentColor,
                          icon: Icon(Icons.collections),
                          label: Text('Gallery'),
                          onPressed: gallery,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          );
        },
        onClosing: () {
          print('hi therer');
        },
      ),
    );
  };

  List<ImagePickerData> resulte = [];

  if (bottomSheetUI == true ||
      Theme.of(context).platform == TargetPlatform.iOS) {
    // resulte
    final r = await showCupertinoModalPopup<List<ImagePickerData>>(
        context: context,
        builder: (_context) {
          return CupertinoActionSheet(
            cancelButton: CupertinoActionSheetAction(
              child: Text('Cancel'),
              onPressed: Navigator.of(_context).pop,
            ),
            // Upload From
            message: Text('how do you want to Upload the Image ?'),
            title: Text('Upload From'),
            actions: <Widget>[
              CupertinoActionSheetAction(
                isDefaultAction: true,
                isDestructiveAction: true,
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Icon(Icons.photo_camera,
                        color: Theme.of(_context).primaryColor),
                    SizedBox(width: 8.0),
                    Text(cameraText,
                        style: TextStyle(
                            color: Theme.of(_context).primaryColor)),
                  ],
                ),
                onPressed: camera,
              ),
              CupertinoActionSheetAction(
                isDefaultAction: true,
                isDestructiveAction: true,
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Icon(Icons.collections,
                        color: Theme.of(_context).primaryColor),
                    SizedBox(width: 8.0),
                    Text(galleryText,
                        style: TextStyle(
                            color: Theme.of(_context).primaryColor)),
                  ],
                ),
                onPressed: gallery,
              ),
            ],
          );
        });
    if (r != null) resulte = r;
  } else if (bottomSheetUI == false ||
      Theme.of(context).platform == TargetPlatform.android) {
    final r = await showModalBottomSheet<List<ImagePickerData>>(
        context: context,
        isScrollControlled: false,
        elevation: 5,
        useRootNavigator: true,
        backgroundColor: Theme.of(context).scaffoldBackgroundColor,
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.vertical(top: Radius.circular(25))),
        builder: builder);
    if (r != null) resulte = r;
  }
  print(resulte);
  return resulte;
}