Skip to content
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
11 changes: 6 additions & 5 deletions example/lib/basic.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';

import 'package:scratcher/controller.dart';
import 'package:scratcher/scratcher.dart';

class BasicScreen extends StatefulWidget {
Expand All @@ -14,6 +14,7 @@ class _BasicScreenState extends State<BasicScreen> {
bool enabled = true;
double? size;
final key = GlobalKey<ScratcherState>();
final controller = new ScratcherController();

@override
Widget build(BuildContext context) {
Expand All @@ -26,9 +27,8 @@ class _BasicScreenState extends State<BasicScreen> {
ElevatedButton(
child: const Text('Reset'),
onPressed: () {
key.currentState?.reset(
duration: const Duration(milliseconds: 2000),
);
controller.clearPoints(
duration: Duration(milliseconds: 2000));
setState(() => thresholdReached = false);
},
),
Expand All @@ -49,7 +49,7 @@ class _BasicScreenState extends State<BasicScreen> {
ElevatedButton(
child: const Text('Reveal'),
onPressed: () {
key.currentState?.reveal(
controller.reveal(
duration: const Duration(milliseconds: 2000),
);
},
Expand Down Expand Up @@ -82,6 +82,7 @@ class _BasicScreenState extends State<BasicScreen> {
width: size,
child: Scratcher(
key: key,
controller: controller,
enabled: enabled,
brushSize: brushSize,
threshold: 30,
Expand Down
26 changes: 26 additions & 0 deletions lib/controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:flutter/widgets.dart';
import 'package:scratcher/utils.dart';

class ScratcherController extends ChangeNotifier {
List<ScratchPoint?> points = [];
bool? revealed;
Duration? duration;

void clearPoints({Duration? duration}) {
points = [];
revealed = false;
this.duration = duration;
notifyListeners();
revealed = false;
this.duration = null;
}

void reveal({Duration? duration}) {
revealed = true;
this.duration = duration;
notifyListeners();
revealed = false;
this.duration = null;
}

}
28 changes: 22 additions & 6 deletions lib/widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:scratcher/controller.dart';
import 'package:scratcher/utils.dart';

import 'painter.dart';
Expand Down Expand Up @@ -38,6 +39,7 @@ class Scratcher extends StatefulWidget {
Scratcher({
Key? key,
required this.child,
this.controller,
this.enabled = true,
this.threshold,
this.brushSize = 25,
Expand Down Expand Up @@ -92,6 +94,8 @@ class Scratcher extends StatefulWidget {
/// Callback called when scratching ends
final VoidCallback? onScratchEnd;

final ScratcherController? controller;

@override
ScratcherState createState() => ScratcherState();
}
Expand Down Expand Up @@ -124,7 +128,16 @@ class ScratcherState extends State<Scratcher> {
} else {
_imageLoader = _loadImage(widget.image!);
}

if (widget.controller != null) {
widget.controller?.addListener(() {
if (widget.controller!.revealed == true) {
reveal(duration: widget.controller?.duration);
} else if (widget.controller!.revealed == false) {
reset(duration: widget.controller?.duration);
}
points = widget.controller!.points;
});
}
super.initState();
}

Expand All @@ -140,15 +153,17 @@ class ScratcherState extends State<Scratcher> {
? (details) {
widget.onScratchStart?.call();
if (widget.enabled) {
_addPoint(details.localPosition);
final newPoints = _addPoint(details.localPosition);
widget.controller?.points = newPoints!;
}
}
: null,
onPanUpdate: canScratch
? (details) {
widget.onScratchUpdate?.call();
if (widget.enabled) {
_addPoint(details.localPosition);
final newPoints = _addPoint(details.localPosition);
widget.controller?.points = newPoints!;
}
}
: null,
Expand Down Expand Up @@ -224,10 +239,10 @@ class ScratcherState extends State<Scratcher> {
return distance <= radius;
}

void _addPoint(Offset position) {
List<ScratchPoint?>? _addPoint(Offset position) {
// Ignore when same point is reported multiple times in a row
if (_lastPosition == position) {
return;
return null;
}
_lastPosition = position;

Expand All @@ -236,7 +251,7 @@ class ScratcherState extends State<Scratcher> {
// Ignore when starting point of new line has been already scratched
if (points.isNotEmpty && points.contains(point)) {
if (points.last == null) {
return;
return null;
} else {
point = null;
}
Expand Down Expand Up @@ -273,6 +288,7 @@ class ScratcherState extends State<Scratcher> {
isFinished = true;
}
}
return points;
}

void _setCheckpoints(Size size) {
Expand Down