【问题标题】:Image cropper not working in GetxController图像裁剪器在 GetxController 中不起作用
【发布时间】:2023-01-07 03:12:28
【问题描述】:

以下是我试图在其中实现的代码。未来的方法 pickImage 我猜这是有问题的方法。我正在使用 GetxController 扩展该类。该方法应使用图像裁剪器选择并裁剪所选图像,如果裁剪成功,则将裁剪后的图像设置为 imageFile 变量。

import 'dart:io';

import 'package:pamoja/app/data/const.dart';
import 'package:pamoja/app/data/firebase/firebase_functions.dart';
import 'package:pamoja/app/data/global_widgets/indicator.dart';
import 'package:pamoja/app/models/advert_model.dart';
import 'package:pamoja/app/modules/my_adverts/controllers/my_adverts_controller.dart';
import 'package:pamoja/app/routes/app_pages.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:image_cropper/image_cropper.dart';

class UploadBlogController extends GetxController {
  TextEditingController title = TextEditingController();
  TextEditingController description = TextEditingController();
  TextEditingController location = TextEditingController();
  TextEditingController category = TextEditingController();
  TextEditingController price = TextEditingController();
  TextEditingController phone = TextEditingController();
  final FirebaseFunctions _functions = FirebaseFunctions();
  File? imageFile;

  Future<void> pickImage() async {
    try {
      ImagePicker _picker = ImagePicker();

      await _picker.pickImage(source: ImageSource.gallery).then((value) async {
        if (value != null) {
          File? croppedFile = await ImageCropper().cropImage(
            sourcePath: value.path,
            aspectRatio: CropAspectRatio(ratioX: 1, ratioY: 1),
            compressQuality: 100,
            maxWidth: 700,
            maxHeight: 700,
            // saveCircleCroppedImage: true,
          );

          if (croppedFile != null) {
            imageFile = croppedFile;
            update();
          }
        }
      });
    } catch (e) {
      showAlert("$e");
    }
  }

  void createBlog() async {
    if (title.text.isNotEmpty && description.text.isNotEmpty) {
      if (imageFile != null && imageFile != "") {
        Indicator.showLoading();

        await _functions
            .uploadBlog(
          title.text,
          description.text,
          imageFile!,
          price.text,
          category.text,
          location.text,
          phone.text,
        )
            .then((value) {
          Indicator.closeLoading();
          showAlert("Your advert created sucessfully");
          // Get.back();
          Get.toNamed(Routes.HOME);
        });
      } else {
        showAlert("Image is required");
      }
    } else {
      showAlert("All fields are required");
    }
  }

  void editBlog(BlogsModel model) async {
    Indicator.showLoading();

    if (title.text.isNotEmpty && description.text.isNotEmpty) {
      if (imageFile == null) {
        Map<String, dynamic> map = {
          'title': title.text,
          'description': description.text,
        };

        await _functions.editBlog(model.id, map).then((value) {
          Get.toNamed(Routes.HOME);
          showAlert("Your ads Updated Sucessfully");
        });
      } else {
        String imageUrl = await _functions.uploadImage(imageFile!);

        Map<String, dynamic> map = {
          'title': title.text,
          'description': description.text,
          'img': imageUrl,
        };

        await _functions.editBlog(model.id, map).then((value) {
          Get.toNamed(Routes.HOME);
          showAlert("Your Advert Updated Sucessfully");
        });
      }
    } else {
      showAlert("All fields are required");
    }

    Indicator.closeLoading();
    updateData();
  }

  void updateData() {
    Get.back();
    Get.toNamed(Routes.HOME);
    if (Get.isRegistered<MyBlogsController>()) {
      final controller = Get.find<MyBlogsController>();

      controller.myBlogs = [];
      Indicator.showLoading();
      controller.getMyBlogData();
    }
  }
}

【问题讨论】:

    标签: flutter controller flutter-getx image-cropper flutter-image-picker


    【解决方案1】:

    我为我的整个应用程序创建了可重用的单音代码来选择媒体图像或视频。您可以尝试或根据您的要求进行修改。

    // Dart imports:
    import 'dart:io';
    
    // Package imports:
    import 'package:image_cropper/image_cropper.dart';
    import 'package:image_picker/image_picker.dart';
    
    // Project imports:
    import 'package:app/utils/export_utils.dart';
    
    class MediaPicker {
      MediaPicker._();
    
      static const supportedImageFormates = [".jpeg", ".png"];
      static const supportedVedioFormates = [".mp4"];
      static final picker = ImagePicker();
    
      // Single image / video selection
      static Future<File?> pickMedia({
        required ImageSource source,
        bool isVideo = false,
        bool isEditing = false,
      }) async {
        try {
          final pickMedia = !isVideo
              ? await picker.pickImage(source: source)
              : await picker.pickVideo(source: source);
    
          if (pickMedia != null) {
            return isEditing
                ? await editImage(file: File(pickMedia.path))
                : File(pickMedia.path);
          } else {
            return null;
          }
        } catch (ex) {
          "Pick Media error: $ex".printLog();
          return null;
        }
      }
    
      static Future<File> editImage({required File file}) async {
        final CroppedFile? croppedFile = await ImageCropper().cropImage(
          sourcePath: file.path,
          uiSettings: [
            AndroidUiSettings(
              toolbarTitle: 'Cropper',
              toolbarColor: ColorConst.themePrimaryColor,
              toolbarWidgetColor: ColorConst.white,
              initAspectRatio: CropAspectRatioPreset.original,
              lockAspectRatio: false,
            ),
            IOSUiSettings(
              title: 'Cropper',
            ),
          ],
        );
    
        if (croppedFile != null) {
          return File(croppedFile.path);
        } else {
          return file;
        }
      }
    }
    

    用法使用 Futureasync 方法在点击按钮时添加调用方法

    Future<void> btnPickImageTap() async {
        final File? selectImageFile = await MediaPicker.pickMedia(
          source: ImageSource.gallery, 
          isEditing: true, // Default false
        );
    }
    

    source: ImageSource.camera - to pick image from camera. 
    isEditing: false - avoid image cropping functionality.
    isVideo: to pick video from gallery / camera.
    

    确保为两个平台添加以下权限

    Andriod-AndroidManifest.xml

        <uses-permission android:name="android.permission.CAMERA"/> //Camera
       <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> // Read Storage 
    

    iOS亚军/info.plist

    <key>NSCameraUsageDescription</key>
        <string>App require camera permission to update profile pic</string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>App require gallery permission to update profile pic</string>
    

    正如image_cropper中提到的包,在AndroidManifest.xml&lt;applcaiton&gt;标签下添加波纹管代码

    <activity
      android:name="com.yalantis.ucrop.UCropActivity"
      android:screenOrientation="portrait"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多