Skip to content

Commit

Permalink
Add enableOnlyInDebugMode flag
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasWanke committed Jul 18, 2022
1 parent e29c856 commit f662e57
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ MaterialApp(
)
```

> The debug overlay only works in debug mode and is not included in your widget tree in profile or release mode.
> The debug overlay only works in debug mode and is not included in your widget tree in profile or release mode unless you pass `enableOnlyInDebugMode: false`.
There are two ways to open the debug overlay:

Expand Down
45 changes: 28 additions & 17 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.enableOnlyInDebugMode = true,
}) : super(key: DebugOverlayState.key);

static final helpers = ValueNotifier<List<Widget>>([
Expand All @@ -31,7 +32,7 @@ class DebugOverlay extends StatefulWidget {
/// 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
/// widgets.
/// widgets unless [enableOnlyInDebugMode] is set to `false`.
///
/// This is usually used as the [WidgetsApp.builder]/[MaterialApp.builder]/
/// [CupertinoApp.builder]:
Expand All @@ -46,15 +47,17 @@ class DebugOverlay extends StatefulWidget {
///
/// You can open the overlay by shaking your phone (if [showOnShake] is
/// `true`) or by calling [show] or [hide].
static TransitionBuilder builder({bool showOnShake = true}) {
// ignore: omit_local_variable_types
TransitionBuilder builder = (context, child) => child ?? SizedBox();
assert(() {
builder = (context, child) =>
DebugOverlay(showOnShake: showOnShake, child: child);
return true;
}());
return builder;
static TransitionBuilder builder({
bool showOnShake = true,
bool enableOnlyInDebugMode = true,
}) {
return _isInDebugMode || !enableOnlyInDebugMode
? (context, child) => DebugOverlay(
showOnShake: showOnShake,
enableOnlyInDebugMode: enableOnlyInDebugMode,
child: child,
)
: (context, child) => child ?? SizedBox();
}

static void show() => DebugOverlayState.key.currentState!.show();
Expand All @@ -63,6 +66,7 @@ class DebugOverlay extends StatefulWidget {
final Widget? child;

final bool showOnShake;
final bool enableOnlyInDebugMode;

@override
DebugOverlayState createState() => DebugOverlayState();
Expand Down Expand Up @@ -109,15 +113,13 @@ class DebugOverlayState extends State<DebugOverlay> {

@override
Widget build(BuildContext context) {
Widget? bottomSheet;
assert(() {
if (!_isVisible) return true;
bottomSheet = _buildBottomSheet();
return true;
}());
final bottomSheet =
_isVisible && (_isInDebugMode || !widget.enableOnlyInDebugMode)
? _buildBottomSheet()
: null;
return Stack(children: [
if (widget.child != null) widget.child!,
if (bottomSheet != null) Positioned.fill(child: bottomSheet!),
if (bottomSheet != null) Positioned.fill(child: bottomSheet),
]);
}

Expand Down Expand Up @@ -157,6 +159,15 @@ class DebugOverlayState extends State<DebugOverlay> {
}
}

bool get _isInDebugMode {
var isInDebugMode = false;
assert(() {
isInDebugMode = true;
return true;
}());
return isInDebugMode;
}

class DebugOverlayContent extends StatelessWidget {
const DebugOverlayContent({this.scrollController, this.onClose});

Expand Down

0 comments on commit f662e57

Please sign in to comment.