Skip to content

Commit

Permalink
fix: old crop utility brought back (#1422)
Browse files Browse the repository at this point in the history
* added image_cropper package to depencency

* added the required changes to manifest.xml file

* working new cropper added

* styled the crop tool with the theme

* fine tune dark theme on crop tool

* removed unused dependencies

Co-authored-by: Marvin M <39344769+M123-dev@users.noreply.github.com>
  • Loading branch information
AshAman999 and M123-dev authored Apr 1, 2022
1 parent 475534c commit d820200
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 105 deletions.
4 changes: 4 additions & 0 deletions packages/smooth_app/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,9 @@
<meta-data
android:name="flutterEmbedding"
android:value="2" />
<activity
android:name="com.yalantis.ucrop.UCropActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
</application>
</manifest>
122 changes: 29 additions & 93 deletions packages/smooth_app/lib/pages/image_crop_page.dart
Original file line number Diff line number Diff line change
@@ -1,109 +1,45 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:crop_your_image/crop_your_image.dart';
import 'package:flutter/material.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/themes/theme_provider.dart';

Future<File?> startImageCropping(BuildContext context) async {
final Uint8List? bytes = await pickImage();

if (bytes == null) {
return null;
}

return Navigator.push<File?>(
context,
MaterialPageRoute<File?>(
builder: (BuildContext context) => ImageCropPage(imageBytes: bytes),
),
);
}

Future<Uint8List?> pickImage() async {
final bool isDarktheme =
Provider.of<ThemeProvider>(context, listen: false).isDarkMode(context);
final Color? themeColor = isDarktheme
? Colors.black
: Theme.of(context).appBarTheme.backgroundColor;
final ImagePicker picker = ImagePicker();

final XFile? pickedXFile = await picker.pickImage(
source: ImageSource.camera,
);
if (pickedXFile == null) {
// User didn't pick any image.
return null;
}

return pickedXFile.readAsBytes();
}

// build circular progress bar widget
Widget buildCircularProgressBar() {
return const Center(
child: CircularProgressIndicator(),
final File? croppedFile = await ImageCropper().cropImage(
sourcePath: pickedXFile.path,
aspectRatioPresets: <CropAspectRatioPreset>[
CropAspectRatioPreset.square,
CropAspectRatioPreset.ratio3x2,
CropAspectRatioPreset.original,
CropAspectRatioPreset.ratio4x3,
CropAspectRatioPreset.ratio16x9
],
androidUiSettings: AndroidUiSettings(
toolbarTitle: 'Edit Photo', // TODO(ashaan): Localize
initAspectRatio: CropAspectRatioPreset.original,
lockAspectRatio: false,
statusBarColor: themeColor,
toolbarColor: themeColor,
toolbarWidgetColor: Colors.white,
activeControlsWidgetColor: Theme.of(context).colorScheme.primary,
backgroundColor: themeColor,
),
iosUiSettings: const IOSUiSettings(
minimumAspectRatio: 1.0,
),
);
}

class ImageCropPage extends StatefulWidget {
const ImageCropPage({Key? key, required this.imageBytes}) : super(key: key);

final Uint8List imageBytes;

@override
State<ImageCropPage> createState() => _ImageCropPageState();
}

class _ImageCropPageState extends State<ImageCropPage> {
bool isCropping = false;
@override
Widget build(BuildContext context) {
final CropController _controller = CropController();
final ThemeData theme = Theme.of(context);
context.watch<ThemeProvider>();
return Scaffold(
body: isCropping
? buildCircularProgressBar()
: Crop(
image: widget.imageBytes,
controller: _controller,
onCropped: (Uint8List image) async {
final Directory tempDir = await getTemporaryDirectory();
final String tempPath = tempDir.path;
final String filePath = '$tempPath/upload_img_file.tmp';
final File file = await File(filePath).writeAsBytes(image);
setState(() {
isCropping = false;
});
Navigator.pop(context, file);
},
initialSize: 0.5,
baseColor: theme.colorScheme.primary,
maskColor: Colors.white.withAlpha(100),
cornerDotBuilder: (double size, EdgeAlignment edgeAlignment) =>
DotControl(color: theme.colorScheme.primary),
),
bottomNavigationBar: BottomAppBar(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(
icon: const Icon(Icons.cancel_outlined),
onPressed: () {
Navigator.pop(context);
},
),
IconButton(
icon: const Icon(Icons.done),
onPressed: () {
_controller.crop();
setState(() {
isCropping = true;
});
},
)
],
),
),
);
}
return croppedFile;
}
14 changes: 7 additions & 7 deletions packages/smooth_app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
crop_your_image:
dependency: "direct main"
description:
name: crop_your_image
url: "https://pub.dartlang.org"
source: hosted
version: "0.7.2"
cross_file:
dependency: transitive
description:
Expand Down Expand Up @@ -422,6 +415,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.3"
image_cropper:
dependency: "direct main"
description:
name: image_cropper
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.0"
image_picker:
dependency: "direct main"
description:
Expand Down
8 changes: 3 additions & 5 deletions packages/smooth_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ dependencies:
matomo_forever: ^1.0.0+1
modal_bottom_sheet: ^2.0.1
openfoodfacts: ^1.14.0
# openfoodfacts:
# path: ../../../openfoodfacts-dart
# openfoodfacts:
# path: ../../../openfoodfacts-dart
package_info_plus: ^1.4.0
permission_handler: ^9.2.0
photo_view: ^0.13.0
Expand All @@ -43,18 +43,16 @@ dependencies:
visibility_detector: ^0.2.2
camera: ^0.9.4+16
percent_indicator: ^4.0.0
crop_your_image: ^0.7.2
mailto: ^2.0.0
flutter_native_splash: ^2.1.1
# Fork from cli1005 with iOS fix cf: https://github.com/openfoodfacts/smooth-app/issues/1155
google_ml_barcode_scanner:
git:
url: https://github.com/cli1005/google_ml_barcode_scanner.git
ref: master
image_cropper: ^1.5.0
auto_size_text: ^3.0.0



dev_dependencies:
flutter_launcher_icons: ^0.9.2
flutter_test:
Expand Down

0 comments on commit d820200

Please sign in to comment.