Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For custom crop painter #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

/* Begin PBXBuildFile section */
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
2D5378261FAA1A9400D5DBA9 /* flutter_assets in Resources */ = {isa = PBXBuildFile; fileRef = 2D5378251FAA1A9400D5DBA9 /* flutter_assets */; };
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
3B80C3941E831B6300D905FE /* App.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; };
3B80C3951E831B6300D905FE /* App.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
Expand Down Expand Up @@ -41,7 +40,6 @@
/* Begin PBXFileReference section */
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
2D5378251FAA1A9400D5DBA9 /* flutter_assets */ = {isa = PBXFileReference; lastKnownFileType = folder; name = flutter_assets; path = Flutter/flutter_assets; sourceTree = SOURCE_ROOT; };
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
3B80C3931E831B6300D905FE /* App.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = App.framework; path = Flutter/App.framework; sourceTree = "<group>"; };
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = "<group>"; };
Expand Down Expand Up @@ -76,7 +74,6 @@
9740EEB11CF90186004384FC /* Flutter */ = {
isa = PBXGroup;
children = (
2D5378251FAA1A9400D5DBA9 /* flutter_assets */,
3B80C3931E831B6300D905FE /* App.framework */,
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */,
9740EEBA1CF902C7004384FC /* Flutter.framework */,
Expand Down Expand Up @@ -212,7 +209,6 @@
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */,
9740EEB41CF90195004384FC /* Debug.xcconfig in Resources */,
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */,
2D5378261FAA1A9400D5DBA9 /* flutter_assets in Resources */,
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
72 changes: 70 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_crop/image_crop.dart';

import 'plain_crop_painter.dart';

// Hidden import to let `flutter packages pub publish --dry-run` complete without errors
// FIXME: uncomment to try out example code
// import 'package:image_picker/image_picker.dart';
Expand All @@ -30,6 +32,8 @@ class _MyAppState extends State<MyApp> {
File _file;
File _sample;
File _lastCropped;
bool _fixedCrop = false;
bool _plainCrop = false;

@override
void dispose() {
Expand Down Expand Up @@ -58,10 +62,71 @@ class _MyAppState extends State<MyApp> {
}

Widget _buildCroppingImage() {
CropPainterBuilder cropPainterBuilder;

if (_plainCrop) {
cropPainterBuilder = (state) {
return PlainCropPainter(state: state);
};
}

return Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
width: 140,
child: FlatButton(
child: Text(
_fixedCrop ? 'try adjustable' : 'try fixed',
style: Theme
.of(context)
.textTheme
.button
.copyWith(color: _plainCrop ? Colors.grey : Colors.white),
),
onPressed: () {
if(_plainCrop) return;
setState(() {
_fixedCrop = !_fixedCrop;
cropKey.currentState.adjustable = !_fixedCrop;
});
},
),
),
Container(
width: 140,
child: FlatButton(
child: Text(
_plainCrop ? 'try default crop' : 'try plain crop',
style: Theme
.of(context)
.textTheme
.button
.copyWith(color: Colors.white),
),
onPressed: () {
setState(() {
_plainCrop = !_plainCrop;
if(_plainCrop){
cropKey.currentState.adjustable = false;
}else{
cropKey.currentState.adjustable = !_fixedCrop;
}
});
},
),
),
],
),
Expanded(
child: Crop.file(_sample, key: cropKey),
child: Crop.file(
_sample,
key: cropKey,
aspectRatio: 1.0 / 1.0,
cropPainterBuilder: cropPainterBuilder,
),
),
Container(
padding: const EdgeInsets.only(top: 20.0),
Expand All @@ -72,7 +137,10 @@ class _MyAppState extends State<MyApp> {
FlatButton(
child: Text(
'Crop Image',
style: Theme.of(context).textTheme.button.copyWith(color: Colors.white),
style: Theme.of(context)
.textTheme
.button
.copyWith(color: Colors.white),
),
onPressed: () => _cropImage(),
),
Expand Down
73 changes: 73 additions & 0 deletions example/lib/plain_crop_painter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import 'dart:ui' as ui;
import 'package:image_crop/image_crop.dart';
import 'package:flutter/widgets.dart';

const _kCropOverlayActiveOpacity = 0.3;
const _kCropOverlayInactiveOpacity = 0.7;

class PlainCropPainter extends AbstractCropPainter {
PlainCropPainter({
CropPaintState state,
}) : super(
state: state);

@override
void paint(Canvas canvas, Size size) {
final rect = Rect.fromLTWH(
0,
0,
size.width,
size.height,
);

canvas.save();
canvas.translate(rect.left, rect.top);

final paint = Paint()..isAntiAlias = false;

if (state.image != null) {
final src = Rect.fromLTWH(
0.0,
0.0,
state.image.width.toDouble(),
state.image.height.toDouble(),
);
final dst = Rect.fromLTWH(
rect.width * state.area.left - state.image.width * state.view.left * state.scale * state.ratio,
rect.height * state.area.top - state.image.height * state.view.top * state.scale * state.ratio,
state.image.width * state.scale * state.ratio,
state.image.height * state.scale * state.ratio,
);

canvas.save();
canvas.clipRect(Rect.fromLTWH(0.0, 0.0, rect.width, rect.height));
canvas.drawImageRect(state.image, src, dst, paint);
canvas.restore();
}

paint.color = Color.fromRGBO(
0x0,
0x0,
0x0,
_kCropOverlayActiveOpacity * state.active +
_kCropOverlayInactiveOpacity * (1.0 - state.active));
final boundaries = Rect.fromLTWH(
rect.width * state.area.left,
rect.height * state.area.top,
rect.width * state.area.width,
rect.height * state.area.height,
);
canvas.drawRect(Rect.fromLTRB(0.0, 0.0, rect.width, boundaries.top), paint);
canvas.drawRect(
Rect.fromLTRB(0.0, boundaries.bottom, rect.width, rect.height), paint);
canvas.drawRect(
Rect.fromLTRB(0.0, boundaries.top, boundaries.left, boundaries.bottom),
paint);
canvas.drawRect(
Rect.fromLTRB(
boundaries.right, boundaries.top, rect.width, boundaries.bottom),
paint);

canvas.restore();
}
}
Loading