Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[SelectionOverlay]Move the debug statement to the scope of the assertion. #108508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 2, 2022
Merged
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
2 changes: 1 addition & 1 deletion examples/api/lib/widgets/overlay/overlay.0.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class _OverlayExampleState extends State<OverlayExample> {
);

// Add the OverlayEntry to the Overlay.
Overlay.of(context)!.insert(overlayEntry!);
Overlay.of(context, debugRequiredFor: widget)!.insert(overlayEntry!);
}

// Remove the OverlayEntry.
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/cupertino/context_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ class _CupertinoContextMenuState extends State<CupertinoContextMenu> with Ticker
);
},
);
Overlay.of(context, rootOverlay: true)!.insert(_lastOverlayEntry!);
Overlay.of(context, rootOverlay: true, debugRequiredFor: widget)!.insert(_lastOverlayEntry!);
_openController.forward();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ class _BottomNavigationBarState extends State<BottomNavigationBar> with TickerPr
assert(debugCheckHasDirectionality(context));
assert(debugCheckHasMaterialLocalizations(context));
assert(debugCheckHasMediaQuery(context));
assert(Overlay.of(context, debugRequiredFor: widget) != null);
assert(debugCheckHasOverlay(context));

final BottomNavigationBarThemeData bottomTheme = BottomNavigationBarTheme.of(context);
final BottomNavigationBarLandscapeLayout layout = widget.landscapeLayout
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/material/range_slider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ class _RangeSliderState extends State<RangeSlider> with TickerProviderStateMixin
);
},
);
Overlay.of(context)!.insert(overlayEntry!);
Overlay.of(context, debugRequiredFor: widget)!.insert(overlayEntry!);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/material/slider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ class _SliderState extends State<Slider> with TickerProviderStateMixin {
);
},
);
Overlay.of(context)!.insert(overlayEntry!);
Overlay.of(context, debugRequiredFor: widget)!.insert(overlayEntry!);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/material/tooltip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ class TooltipState extends State<Tooltip> with SingleTickerProviderStateMixin {
if (_tooltipMessage.isEmpty) {
return widget.child ?? const SizedBox();
}
assert(Overlay.of(context, debugRequiredFor: widget) != null);
assert(debugCheckHasOverlay(context));
final ThemeData theme = Theme.of(context);
final TooltipThemeData tooltipTheme = TooltipTheme.of(context);
final TextStyle defaultTextStyle;
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/widgets/autocomplete.dart
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ class _RawAutocompleteState<T extends Object> extends State<RawAutocomplete<T>>
);
},
);
Overlay.of(context, rootOverlay: true)!.insert(newFloatingOptions);
Overlay.of(context, rootOverlay: true, debugRequiredFor: widget)!.insert(newFloatingOptions);
_floatingOptions = newFloatingOptions;
} else {
_floatingOptions = null;
Expand Down
3 changes: 2 additions & 1 deletion packages/flutter/lib/src/widgets/drag_target.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:flutter/services.dart';

import 'basic.dart';
import 'binding.dart';
import 'debug.dart';
import 'framework.dart';
import 'media_query.dart';
import 'overlay.dart';
Expand Down Expand Up @@ -598,7 +599,7 @@ class _DraggableState<T extends Object> extends State<Draggable<T>> {

@override
Widget build(BuildContext context) {
assert(Overlay.of(context, debugRequiredFor: widget, rootOverlay: widget.rootOverlay) != null);
assert(debugCheckHasOverlay(context));
final bool canDrag = widget.maxSimultaneousDrags == null ||
_activeCount < widget.maxSimultaneousDrags!;
final bool showChild = _activeCount == 0 || widget.childWhenDragging == null;
Expand Down
6 changes: 3 additions & 3 deletions packages/flutter/lib/src/widgets/reorderable_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ class SliverReorderableListState extends State<SliverReorderableList> with Ticke
);
_dragInfo!.startDrag();

final OverlayState overlay = Overlay.of(context)!;
final OverlayState overlay = Overlay.of(context, debugRequiredFor: widget)!;
assert(_overlayEntry == null);
_overlayEntry = OverlayEntry(builder: _dragInfo!.createProxy);
overlay.insert(_overlayEntry!);
Expand Down Expand Up @@ -897,7 +897,7 @@ class SliverReorderableListState extends State<SliverReorderableList> with Ticke
}
final Widget child = widget.itemBuilder(context, index);
assert(child.key != null, 'All list items must have a key');
final OverlayState overlay = Overlay.of(context)!;
final OverlayState overlay = Overlay.of(context, debugRequiredFor: widget)!;
return _ReorderableItem(
key: _ReorderableItemGlobalKey(child.key!, index, this),
index: index,
Expand Down Expand Up @@ -1284,7 +1284,7 @@ class _DragInfo extends Drag {
}

Offset _overlayOrigin(BuildContext context) {
final OverlayState overlay = Overlay.of(context)!;
final OverlayState overlay = Overlay.of(context, debugRequiredFor: context.widget)!;
final RenderBox overlayBox = overlay.context.findRenderObject()! as RenderBox;
return overlayBox.localToGlobal(Offset.zero);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/flutter/lib/src/widgets/selectable_region.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:flutter/services.dart';

import 'actions.dart';
import 'basic.dart';
import 'debug.dart';
import 'focus_manager.dart';
import 'focus_scope.dart';
import 'framework.dart';
Expand Down Expand Up @@ -804,7 +805,7 @@ class _SelectableRegionState extends State<SelectableRegion> with TextSelectionD

@override
Widget build(BuildContext context) {
assert(Overlay.of(context, debugRequiredFor: widget) != null);
assert(debugCheckHasOverlay(context));
return CompositedTransformTarget(
link: _toolbarLayerLink,
child: RawGestureDetector(
Expand Down
12 changes: 3 additions & 9 deletions packages/flutter/lib/src/widgets/text_selection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'basic.dart';
import 'binding.dart';
import 'constants.dart';
import 'container.dart';
import 'debug.dart';
import 'editable_text.dart';
import 'framework.dart';
import 'gesture_detector.dart';
Expand Down Expand Up @@ -616,15 +617,8 @@ class SelectionOverlay {
_endHandleType = endHandleType,
_lineHeightAtEnd = lineHeightAtEnd,
_selectionEndpoints = selectionEndpoints,
_toolbarLocation = toolbarLocation {
final OverlayState? overlay = Overlay.of(context, rootOverlay: true);
assert(
overlay != null,
'No Overlay widget exists above $context.\n'
'Usually the Navigator created by WidgetsApp provides the overlay. Perhaps your '
'app content was created above the Navigator with the WidgetsApp builder parameter.',
);
}
_toolbarLocation = toolbarLocation,
assert(debugCheckHasOverlay(context));

/// The context in which the selection handles should appear.
///
Expand Down
31 changes: 31 additions & 0 deletions packages/flutter/test/widgets/selectable_text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,37 @@ void main() {
);
}

testWidgets('throw if no Overlay widget exists above', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: MediaQuery(
data: MediaQueryData(size: Size(800.0, 600.0)),
child: Center(
child: Material(
child: SelectableText('I love Flutter!'),
),
),
),
),
);

final Offset textFieldStart = tester.getTopLeft(find.byType(SelectableText));
final TestGesture gesture = await tester.startGesture(textFieldStart, kind: PointerDeviceKind.mouse);
await tester.pump(const Duration(seconds: 2));
await gesture.up();
await tester.pumpAndSettle();

final FlutterError error = tester.takeException() as FlutterError;
expect(
error.message,
contains('EditableText widgets require an Overlay widget ancestor'),
);

await tester.pumpWidget(const SizedBox.shrink());
expect(tester.takeException(), isNotNull); // side effect exception
});

testWidgets('Do not crash when remove SelectableText during handle drag', (WidgetTester tester) async {
// Regression test https://github.com/flutter/flutter/issues/108242
bool isShow = true;
Expand Down