Skip to content

Commit 3b666a0

Browse files
Add RawTooltipl example and test (#180722)
Fixes: #180318 Relands #180691 (because of CLA issues). ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Victor Sanni <[email protected]>
1 parent be2f3bd commit 3b666a0

3 files changed

Lines changed: 154 additions & 2 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
7+
/// Flutter code sample for [RawTooltip].
8+
9+
void main() => runApp(const RawTooltipExampleApp());
10+
11+
class RawTooltipExampleApp extends StatelessWidget {
12+
const RawTooltipExampleApp({super.key});
13+
14+
@override
15+
Widget build(BuildContext context) {
16+
return MaterialApp(
17+
home: const RawTooltipSample(title: 'RawTooltip Sample'),
18+
);
19+
}
20+
}
21+
22+
class RawTooltipSample extends StatelessWidget {
23+
const RawTooltipSample({super.key, required this.title});
24+
25+
final String title;
26+
27+
@override
28+
Widget build(BuildContext context) {
29+
final GlobalKey<RawTooltipState> rawTooltipKey =
30+
GlobalKey<RawTooltipState>();
31+
32+
return Scaffold(
33+
appBar: AppBar(title: Text(title)),
34+
body: Center(
35+
child: RawTooltip(
36+
// Provide a global key with the "RawTooltipState" type to show
37+
// the rawTooltip manually when trigger mode is set to manual.
38+
key: rawTooltipKey,
39+
semanticsTooltip: 'I am a RawTooltip message',
40+
triggerMode: TooltipTriggerMode.manual,
41+
positionDelegate: (TooltipPositionContext context) {
42+
// Use the context information to position the rawTooltip to the right of
43+
// the target.
44+
return Offset(
45+
context.target.dx + context.targetSize.width / 2,
46+
context.target.dy - context.tooltipSize.height / 2,
47+
);
48+
},
49+
tooltipBuilder: (BuildContext context, Animation<double> animation) {
50+
return FadeTransition(
51+
opacity: animation,
52+
child: Text('I am a RawTooltip message'),
53+
);
54+
},
55+
child: Container(
56+
height: 100,
57+
width: 200,
58+
color: Colors.blue,
59+
padding: const EdgeInsets.all(8),
60+
alignment: Alignment.center,
61+
child: const Text(
62+
'Hover over this box or\n'
63+
'tap on the FAB to show the tooltip',
64+
textAlign: TextAlign.center,
65+
style: TextStyle(color: Colors.white),
66+
),
67+
),
68+
),
69+
),
70+
floatingActionButton: FloatingActionButton.extended(
71+
onPressed: () async {
72+
// Show RawTooltip programmatically on button tap.
73+
rawTooltipKey.currentState?.ensureTooltipVisible();
74+
},
75+
label: const Text('Show RawTooltip'),
76+
),
77+
);
78+
}
79+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/gestures.dart';
6+
import 'package:flutter/material.dart';
7+
import 'package:flutter_api_samples/widgets/raw_tooltip/raw_tooltip.0.dart'
8+
as example;
9+
import 'package:flutter_test/flutter_test.dart';
10+
11+
void main() {
12+
testWidgets('RawTooltip text is visible when tapping button', (
13+
WidgetTester tester,
14+
) async {
15+
const String rawTooltipText = 'I am a RawTooltip message';
16+
17+
await tester.pumpWidget(const example.RawTooltipExampleApp());
18+
19+
// The tooltip is not visible before tapping the button.
20+
expect(find.text(rawTooltipText), findsNothing);
21+
// Tap on the button and wait for the tooltip to appear.
22+
await tester.tap(find.byType(FloatingActionButton));
23+
await tester.pump(const Duration(milliseconds: 10));
24+
expect(find.text(rawTooltipText), findsOneWidget);
25+
// Tap anywhere and wait for the tooltip to disappear.
26+
await tester.tap(find.byType(Scaffold));
27+
await tester.pump(const Duration(seconds: 1));
28+
expect(find.text(rawTooltipText), findsNothing);
29+
});
30+
31+
testWidgets('RawTooltip text appears when hovering over the container', (
32+
WidgetTester tester,
33+
) async {
34+
const String rawTooltipText = 'I am a RawTooltip message';
35+
36+
await tester.pumpWidget(const example.RawTooltipExampleApp());
37+
38+
expect(find.text(rawTooltipText), findsNothing);
39+
40+
final Finder containerFinder = find.byType(Container);
41+
expect(containerFinder, findsWidgets);
42+
43+
final Offset hoverTarget = tester.getCenter(containerFinder.first);
44+
45+
final TestGesture pointer = await tester.createGesture(
46+
kind: PointerDeviceKind.mouse,
47+
);
48+
await pointer.addPointer();
49+
await tester.pump();
50+
51+
await pointer.moveTo(hoverTarget);
52+
await tester.pumpAndSettle();
53+
54+
expect(find.text(rawTooltipText), findsOneWidget);
55+
56+
await pointer.moveTo(const Offset(0, 0));
57+
await tester.pumpAndSettle();
58+
expect(find.text(rawTooltipText), findsNothing);
59+
60+
await pointer.moveTo(hoverTarget);
61+
await tester.pumpAndSettle();
62+
63+
expect(find.text(rawTooltipText), findsOneWidget);
64+
65+
addTearDown(pointer.removePointer);
66+
});
67+
}

‎packages/flutter/lib/src/widgets/raw_tooltip.dart‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,14 @@ class _RenderExclusiveMouseRegion extends RenderMouseRegion {
226226
/// See also:
227227
///
228228
/// * [Tooltip], a Material-themed [RawTooltip].
229-
// TODO(victorsanni): https://github.com/flutter/flutter/issues/180318
230-
// Add an example of how to call ensureTooltipVisible.
229+
///
230+
/// {@tool dartpad}
231+
/// This example shows how [RawTooltip] can be shown manually with [TooltipTriggerMode.manual]
232+
/// by calling the [TooltipState.ensureTooltipVisible] function.
233+
///
234+
/// ** See code in examples/api/lib/widgets/raw_tooltip/raw_tooltip.0.dart **
235+
/// {@end-tool}
236+
///
231237
class RawTooltip extends StatefulWidget {
232238
/// Creates a raw tooltip.
233239
///

0 commit comments

Comments
 (0)