Add WidgetsApp.debugShowWidgetInspectorOverride again (deprecated) (#145334)

`WidgetsApp.debugShowWidgetInspectorOverride` was replaced with ` WidgetsBinding.instance.debugShowWidgetInspectorOverrideNotifier` in https://github.com/flutter/flutter/pull/144029.

The old API was removed, not deprecated.

It is used by some [open-source projects](https://github.com/search?q=WidgetsApp.debugShowWidgetInspectorOverride&type=code), thus I'm making the effort of bringing the API back as deprecated.

Fixes https://github.com/flutter/flutter/issues/145333
This commit is contained in:
Pascal Welsch 2024-03-21 18:43:05 +01:00 committed by GitHub
parent fd18e5ce48
commit 4bbe6d594d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 121 additions and 0 deletions

View file

@ -1193,6 +1193,36 @@ class WidgetsApp extends StatefulWidget {
/// Used by the `showPerformanceOverlay` VM service extension.
static bool showPerformanceOverlayOverride = false;
/// If true, forces the widget inspector to be visible.
///
/// Deprecated.
/// Use WidgetsBinding.instance.debugShowWidgetInspectorOverrideNotifier.value
/// instead.
///
/// Overrides the `debugShowWidgetInspector` value set in [WidgetsApp].
///
/// Used by the `debugShowWidgetInspector` debugging extension.
///
/// The inspector allows the selection of a location on your device or emulator
/// and view what widgets and render objects associated with it. An outline of
/// the selected widget and some summary information is shown on device and
/// more detailed information is shown in the IDE or DevTools.
@Deprecated(
'Use WidgetsBinding.instance.debugShowWidgetInspectorOverrideNotifier.value instead. '
'This feature was deprecated after v3.20.0-14.0.pre.',
)
static bool get debugShowWidgetInspectorOverride {
return WidgetsBinding.instance.debugShowWidgetInspectorOverrideNotifier.value;
}
@Deprecated(
'Use WidgetsBinding.instance.debugShowWidgetInspectorOverrideNotifier.value instead. '
'This feature was deprecated after v3.20.0-14.0.pre.',
)
static set debugShowWidgetInspectorOverride(bool value) {
WidgetsBinding.instance.debugShowWidgetInspectorOverrideNotifier.value = value;
}
/// If false, prevents the debug banner from being visible.
///
/// Used by the `debugAllowBanner` VM service extension.

View file

@ -4198,6 +4198,97 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService {
expect(WidgetsBinding.instance.debugShowWidgetInspectorOverride, isFalse);
});
testWidgets('ext.flutter.inspector.show via WidgetsApp.debugShowWidgetInspectorOverride', (WidgetTester tester) async {
final Iterable<Map<Object, Object?>> extensionChangedEvents = service.getServiceExtensionStateChangedEvents('ext.flutter.inspector.show');
Map<Object, Object?> extensionChangedEvent;
int debugShowChangeCounter = 0;
final GlobalKey key = GlobalKey();
await tester.pumpWidget(
WidgetsApp(
key: key,
builder: (BuildContext context, Widget? child) {
return const Placeholder();
},
color: const Color(0xFF123456),
),
);
final ValueListenableBuilder<bool> valueListenableBuilderWidget = tester.widget(
find.byType(ValueListenableBuilder<bool>),
);
void debugShowWidgetInspectorOverrideCallback() {
debugShowChangeCounter++;
}
WidgetsApp.debugShowWidgetInspectorOverride = false;
valueListenableBuilderWidget.valueListenable.addListener(debugShowWidgetInspectorOverrideCallback);
service.rebuildCount = 0;
expect(extensionChangedEvents, isEmpty);
expect(
await service.testBoolExtension(
WidgetInspectorServiceExtensions.show.name,
<String, String>{'enabled': 'true'},
),
equals('true'),
);
expect(extensionChangedEvents.length, equals(1));
extensionChangedEvent = extensionChangedEvents.last;
expect(extensionChangedEvent['extension'], equals('ext.flutter.inspector.show'));
expect(extensionChangedEvent['value'], isTrue);
expect(service.rebuildCount, equals(0)); // Should not be force rebuilt.
expect(debugShowChangeCounter, equals(1));
expect(
await service.testBoolExtension(
WidgetInspectorServiceExtensions.show.name,
<String, String>{},
),
equals('true'),
);
expect(WidgetsApp.debugShowWidgetInspectorOverride, isTrue);
expect(extensionChangedEvents.length, equals(1));
expect(service.rebuildCount, equals(0)); // Should not be force rebuilt.
expect(debugShowChangeCounter, equals(1));
expect(
await service.testBoolExtension(
WidgetInspectorServiceExtensions.show.name,
<String, String>{'enabled': 'true'},
),
equals('true'),
);
expect(extensionChangedEvents.length, equals(2));
extensionChangedEvent = extensionChangedEvents.last;
expect(extensionChangedEvent['extension'], equals('ext.flutter.inspector.show'));
expect(extensionChangedEvent['value'], isTrue);
expect(service.rebuildCount, equals(0)); // Should not be force rebuilt.
expect(debugShowChangeCounter, equals(1));
expect(
await service.testBoolExtension(
WidgetInspectorServiceExtensions.show.name,
<String, String>{'enabled': 'false'},
),
equals('false'),
);
expect(extensionChangedEvents.length, equals(3));
extensionChangedEvent = extensionChangedEvents.last;
expect(extensionChangedEvent['extension'], equals('ext.flutter.inspector.show'));
expect(extensionChangedEvent['value'], isFalse);
expect(service.rebuildCount, equals(0)); // Should not be force rebuilt.
expect(debugShowChangeCounter, equals(2));
expect(
await service.testBoolExtension(
WidgetInspectorServiceExtensions.show.name,
<String, String>{},
),
equals('false'),
);
expect(extensionChangedEvents.length, equals(3));
expect(service.rebuildCount, equals(0)); // Should not be force rebuilt.
expect(debugShowChangeCounter, equals(2));
expect(WidgetsApp.debugShowWidgetInspectorOverride, isFalse);
});
testWidgets('ext.flutter.inspector.screenshot', (WidgetTester tester) async {
final GlobalKey outerContainerKey = GlobalKey();
final GlobalKey paddingKey = GlobalKey();