Skip to content

Commit

Permalink
Add debugOverlay.createShakeDetector
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasWanke committed Jan 24, 2023
1 parent 2435c7d commit f3cab7c
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions lib/src/debug_overlay.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class DebugOverlay extends StatefulWidget {
DebugOverlay({
required this.child,
this.showOnShake = true,
this.createShakeDetector = _defaultCreateShakeDetector,
this.enableOnlyInDebugMode = true,
}) : super(key: DebugOverlayState.key);

Expand All @@ -33,6 +34,9 @@ class DebugOverlay extends StatefulWidget {
helpers.value = [...helpers.value, debugHelper];
}

static ShakeDetector _defaultCreateShakeDetector(VoidCallback onPhoneShake) =>
ShakeDetector.waitForStart(onPhoneShake: onPhoneShake);

/// In debug mode, this returns a builder to add a [DebugOverlay] to your app.
///
/// In profile and release builds, the returned builder doesn't add any
Expand All @@ -53,11 +57,13 @@ class DebugOverlay extends StatefulWidget {
/// `true`) or by calling [show] or [hide].
static TransitionBuilder builder({
bool showOnShake = true,
ShakeDetectorCreator createShakeDetector = _defaultCreateShakeDetector,
bool enableOnlyInDebugMode = true,
}) {
return _isInDebugMode || !enableOnlyInDebugMode
? (context, child) => DebugOverlay(
showOnShake: showOnShake,
createShakeDetector: createShakeDetector,
enableOnlyInDebugMode: enableOnlyInDebugMode,
child: child,
)
Expand All @@ -70,12 +76,17 @@ class DebugOverlay extends StatefulWidget {
final Widget? child;

final bool showOnShake;
final ShakeDetectorCreator createShakeDetector;
final bool enableOnlyInDebugMode;

@override
DebugOverlayState createState() => DebugOverlayState();
}

typedef ShakeDetectorCreator = ShakeDetector Function(
VoidCallback onPhoneShake,
);

class DebugOverlayState extends State<DebugOverlay> {
static final key = GlobalKey<DebugOverlayState>();

Expand All @@ -94,24 +105,35 @@ class DebugOverlayState extends State<DebugOverlay> {
if (!oldWidget.showOnShake && widget.showOnShake) {
_configureShakeDetector();
} else if (oldWidget.showOnShake && !widget.showOnShake) {
_shakeDetector?.stopListening();
_shakeDetector = null;
assert(_shakeDetector != null);
_disposeShakeDetector();
} else if (widget.showOnShake &&
oldWidget.createShakeDetector != widget.createShakeDetector) {
assert(_shakeDetector != null);
_disposeShakeDetector();
_configureShakeDetector();
}
}

void _configureShakeDetector() {
assert(widget.showOnShake);
assert(_shakeDetector == null);

_shakeDetector = ShakeDetector.autoStart(onPhoneShake: show);
_shakeDetector ??= widget.createShakeDetector(show);
_shakeDetector!.startListening();
}

@override
void dispose() {
_shakeDetector?.stopListening();
_disposeShakeDetector();
super.dispose();
}

void _disposeShakeDetector() {
_shakeDetector?.stopListening();
_shakeDetector = null;
}

void show() => setState(() => _isVisible = true);
void hide() => setState(() => _isVisible = false);

Expand Down

0 comments on commit f3cab7c

Please sign in to comment.