Unverified Commit d69ae363 authored by Jimmy Robert's avatar Jimmy Robert Committed by GitHub

Fix tooltip position. (#60479)

parent 357ed4e6
...@@ -284,8 +284,16 @@ class _TooltipState extends State<Tooltip> with SingleTickerProviderStateMixin { ...@@ -284,8 +284,16 @@ class _TooltipState extends State<Tooltip> with SingleTickerProviderStateMixin {
} }
void _createNewEntry() { void _createNewEntry() {
final OverlayState overlayState = Overlay.of(
context,
debugRequiredFor: widget,
);
final RenderBox box = context.findRenderObject() as RenderBox; final RenderBox box = context.findRenderObject() as RenderBox;
final Offset target = box.localToGlobal(box.size.center(Offset.zero)); final Offset target = box.localToGlobal(
box.size.center(Offset.zero),
ancestor: overlayState.context.findRenderObject(),
);
// We create this widget outside of the overlay entry's builder to prevent // We create this widget outside of the overlay entry's builder to prevent
// updated values from happening to leak into the overlay when the overlay // updated values from happening to leak into the overlay when the overlay
...@@ -309,7 +317,7 @@ class _TooltipState extends State<Tooltip> with SingleTickerProviderStateMixin { ...@@ -309,7 +317,7 @@ class _TooltipState extends State<Tooltip> with SingleTickerProviderStateMixin {
), ),
); );
_entry = OverlayEntry(builder: (BuildContext context) => overlay); _entry = OverlayEntry(builder: (BuildContext context) => overlay);
Overlay.of(context, debugRequiredFor: widget).insert(_entry); overlayState.insert(_entry);
SemanticsService.tooltip(widget.message); SemanticsService.tooltip(widget.message);
} }
......
...@@ -99,6 +99,68 @@ void main() { ...@@ -99,6 +99,68 @@ void main() {
expect(tipInGlobal.dy, 20.0); expect(tipInGlobal.dy, 20.0);
}); });
testWidgets('Does tooltip end up in the right place - center with padding outside overlay', (WidgetTester tester) async {
final GlobalKey key = GlobalKey();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Padding(
padding: const EdgeInsets.all(20),
child: Overlay(
initialEntries: <OverlayEntry>[
OverlayEntry(
builder: (BuildContext context) {
return Stack(
children: <Widget>[
Positioned(
left: 300.0,
top: 0.0,
child: Tooltip(
key: key,
message: tooltipText,
height: 20.0,
padding: const EdgeInsets.all(5.0),
verticalOffset: 20.0,
preferBelow: false,
child: const SizedBox(
width: 0.0,
height: 0.0,
),
),
),
],
);
},
),
],
),
),
),
);
(key.currentState as dynamic).ensureTooltipVisible(); // Before using "as dynamic" in your code, see note at the top of the file.
await tester.pump(const Duration(seconds: 2)); // faded in, show timer started (and at 0.0)
/************************ 800x600 screen
* ________________ * }- 20.0 padding outside overlay
* | o | * y=0
* | | | * }- 20.0 vertical offset, of which 10.0 is in the screen edge margin
* | +----+ | * \- (5.0 padding in height)
* | | | | * |- 20 height
* | +----+ | * /- (5.0 padding in height)
* |________________| *
* * } - 20.0 padding outside overlay
************************/
final RenderBox tip = tester.renderObject(
_findTooltipContainer(tooltipText),
);
final Offset tipInGlobal = tip.localToGlobal(tip.size.topCenter(Offset.zero));
// The exact position of the left side depends on the font the test framework
// happens to pick, so we don't test that.
expect(tipInGlobal.dx, 320.0);
expect(tipInGlobal.dy, 40.0);
});
testWidgets('Does tooltip end up in the right place - top left', (WidgetTester tester) async { testWidgets('Does tooltip end up in the right place - top left', (WidgetTester tester) async {
final GlobalKey key = GlobalKey(); final GlobalKey key = GlobalKey();
await tester.pumpWidget( await tester.pumpWidget(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment