Skip to content

Commit

Permalink
feat: add onColorChanged callback (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
talisk authored Sep 30, 2022
1 parent 5531f6f commit ad331d2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/src/widgets/color_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class _ColorButtonState extends State<ColorButton> with WidgetsBindingObserver {
EyeDrop.of(context).capture(context, (value) {
hidden = false;
_onEyePick(value);
});
}, null);
} catch (err) {
print('ERROR !!! _buildPickerOverlay $err');
}
Expand Down
12 changes: 10 additions & 2 deletions lib/src/widgets/eyedrop/eye_dropper_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class _EyeDropperModel {

ValueChanged<Color>? onColorSelected;

ValueChanged<Color>? onColorChanged;

_EyeDropperModel();
}

Expand Down Expand Up @@ -74,6 +76,7 @@ class EyeDrop extends InheritedWidget {
data.eyeOverlayEntry!.remove();
data.eyeOverlayEntry = null;
data.onColorSelected = null;
data.onColorChanged = null;
} catch (err) {
debugPrint('ERROR !!! _onPointerUp $err');
}
Expand All @@ -91,16 +94,21 @@ class EyeDrop extends InheritedWidget {
data.hoverColor = getPixelColor(data.snapshot!, offset);
data.hoverColors = getPixelColors(data.snapshot!, offset);
}

if (data.onColorChanged != null) {
data.onColorChanged!(data.hoverColors.center);
}
}

void capture(
BuildContext context, ValueChanged<Color> onColorSelected) async {
void capture(BuildContext context, ValueChanged<Color> onColorSelected,
ValueChanged<Color>? onColorChanged) async {
final renderer =
captureKey.currentContext?.findRenderObject() as RenderRepaintBoundary?;

if (renderer == null) return;

data.onColorSelected = onColorSelected;
data.onColorChanged = onColorChanged;

data.snapshot = await repaintBoundaryToImage(renderer);

Expand Down
6 changes: 5 additions & 1 deletion lib/src/widgets/eyedrop/eyedropper_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ class EyedropperButton extends StatelessWidget {
/// color selection callback
final ValueChanged<Color> onColor;

/// hover, and the color changed callback
final ValueChanged<Color>? onColorChanged;

const EyedropperButton({
required this.onColor,
this.onColorChanged,
this.icon = Icons.colorize,
this.iconColor = Colors.black54,
Key? key,
Expand All @@ -40,7 +44,7 @@ class EyedropperButton extends StatelessWidget {

void _onEyeDropperRequest(BuildContext context) {
try {
EyeDrop.of(context).capture(context, onColor);
EyeDrop.of(context).capture(context, onColor, onColorChanged);
} catch (err) {
throw Exception('EyeDrop capture error : $err');
}
Expand Down
6 changes: 5 additions & 1 deletion lib/src/widgets/eyedrop/eyedropper_button_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ class EyedropperButton extends StatelessWidget {
/// color selection callback
final ValueChanged<Color> onColor;

/// hover, and the color changed callback
final ValueChanged<Color>? onColorChanged;

/// verify if the button is in a CanvasKit context
bool get eyedropEnabled => js.context['flutterCanvasKit'] != null;

const EyedropperButton({
required this.onColor,
this.onColorChanged,
this.icon = Icons.colorize,
this.iconColor = Colors.blueGrey,
Key? key,
Expand All @@ -41,7 +45,7 @@ class EyedropperButton extends StatelessWidget {

void _onEyeDropperRequest(BuildContext context) {
try {
EyeDrop.of(context).capture(context, onColor);
EyeDrop.of(context).capture(context, onColor, onColorChanged);
} catch (err) {
throw Exception('EyeDrop capture error : $err');
}
Expand Down

0 comments on commit ad331d2

Please sign in to comment.