Skip to content

Commit 575ced6

Browse files
authored
Fix context menu web examples (#120104)
The context menu examples on the docs site now work on the web.
1 parent ec524ed commit 575ced6

12 files changed

+169
-18
lines changed

examples/api/lib/material/context_menu/context_menu_controller.0.dart

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,21 @@
77

88
import 'package:flutter/foundation.dart';
99
import 'package:flutter/material.dart';
10+
import 'package:flutter/services.dart';
1011

1112
void main() => runApp(const MyApp());
1213

1314
/// A builder that includes an Offset to draw the context menu at.
1415
typedef ContextMenuBuilder = Widget Function(BuildContext context, Offset offset);
1516

16-
class MyApp extends StatelessWidget {
17+
class MyApp extends StatefulWidget {
1718
const MyApp({super.key});
1819

20+
@override
21+
State<MyApp> createState() => _MyAppState();
22+
}
23+
24+
class _MyAppState extends State<MyApp> {
1925
void _showDialog (BuildContext context) {
2026
Navigator.of(context).push(
2127
DialogRoute<void>(
@@ -26,6 +32,24 @@ class MyApp extends StatelessWidget {
2632
);
2733
}
2834

35+
@override
36+
void initState() {
37+
super.initState();
38+
// On web, disable the browser's context menu since this example uses a custom
39+
// Flutter-rendered context menu.
40+
if (kIsWeb) {
41+
BrowserContextMenu.disableContextMenu();
42+
}
43+
}
44+
45+
@override
46+
void dispose() {
47+
if (kIsWeb) {
48+
BrowserContextMenu.enableContextMenu();
49+
}
50+
super.dispose();
51+
}
52+
2953
@override
3054
Widget build(BuildContext context) {
3155
return MaterialApp(
@@ -58,7 +82,7 @@ class MyApp extends StatelessWidget {
5882
child: ListView(
5983
children: <Widget>[
6084
Container(height: 20.0),
61-
const Text('Right click or long press anywhere (not just on this text!) to show the custom menu.'),
85+
const Text('Right click (desktop) or long press (mobile) anywhere, not just on this text, to show the custom menu.'),
6286
],
6387
),
6488
),

examples/api/lib/material/context_menu/editable_text_toolbar_builder.0.dart

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,42 @@
66
// appearance.
77

88
import 'package:flutter/cupertino.dart';
9+
import 'package:flutter/foundation.dart';
910
import 'package:flutter/material.dart';
11+
import 'package:flutter/services.dart';
1012

11-
void main() => runApp(MyApp());
13+
void main() => runApp(const MyApp());
1214

13-
class MyApp extends StatelessWidget {
14-
MyApp({super.key});
15+
class MyApp extends StatefulWidget {
16+
const MyApp({super.key});
1517

18+
@override
19+
State<MyApp> createState() => _MyAppState();
20+
}
21+
22+
class _MyAppState extends State<MyApp> {
1623
final TextEditingController _controller = TextEditingController(
17-
text: 'Right click or long press to see the menu with custom buttons.',
24+
text: 'Right click (desktop) or long press (mobile) to see the menu with custom buttons.',
1825
);
1926

27+
@override
28+
void initState() {
29+
super.initState();
30+
// On web, disable the browser's context menu since this example uses a custom
31+
// Flutter-rendered context menu.
32+
if (kIsWeb) {
33+
BrowserContextMenu.disableContextMenu();
34+
}
35+
}
36+
37+
@override
38+
void dispose() {
39+
if (kIsWeb) {
40+
BrowserContextMenu.enableContextMenu();
41+
}
42+
super.dispose();
43+
}
44+
2045
@override
2146
Widget build(BuildContext context) {
2247
return MaterialApp(

examples/api/lib/material/context_menu/editable_text_toolbar_builder.1.dart

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@
55
// This example demonstrates showing a custom context menu only when some
66
// narrowly defined text is selected.
77

8+
import 'package:flutter/foundation.dart';
89
import 'package:flutter/material.dart';
10+
import 'package:flutter/services.dart';
911

10-
void main() => runApp(MyApp());
12+
void main() => runApp(const MyApp());
1113

1214
const String emailAddress = '[email protected]';
1315
const String text = 'Select the email address and open the menu: $emailAddress';
1416

15-
class MyApp extends StatelessWidget {
16-
MyApp({super.key});
17+
class MyApp extends StatefulWidget {
18+
const MyApp({super.key});
19+
20+
@override
21+
State<MyApp> createState() => _MyAppState();
22+
}
23+
24+
class _MyAppState extends State<MyApp> {
1725

1826
final TextEditingController _controller = TextEditingController(
1927
text: text,
@@ -29,6 +37,24 @@ class MyApp extends StatelessWidget {
2937
);
3038
}
3139

40+
@override
41+
void initState() {
42+
super.initState();
43+
// On web, disable the browser's context menu since this example uses a custom
44+
// Flutter-rendered context menu.
45+
if (kIsWeb) {
46+
BrowserContextMenu.disableContextMenu();
47+
}
48+
}
49+
50+
@override
51+
void dispose() {
52+
if (kIsWeb) {
53+
BrowserContextMenu.enableContextMenu();
54+
}
55+
super.dispose();
56+
}
57+
3258
@override
3359
Widget build(BuildContext context) {
3460
return MaterialApp(

examples/api/lib/material/context_menu/editable_text_toolbar_builder.2.dart

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,42 @@
55
// This example demonstrates how to create a custom toolbar that retains the
66
// look of the default buttons for the current platform.
77

8+
import 'package:flutter/foundation.dart';
89
import 'package:flutter/material.dart';
10+
import 'package:flutter/services.dart';
911

10-
void main() => runApp(MyApp());
12+
void main() => runApp(const MyApp());
1113

12-
class MyApp extends StatelessWidget {
13-
MyApp({super.key});
14+
class MyApp extends StatefulWidget {
15+
const MyApp({super.key});
1416

17+
@override
18+
State<MyApp> createState() => _MyAppState();
19+
}
20+
21+
class _MyAppState extends State<MyApp> {
1522
final TextEditingController _controller = TextEditingController(
16-
text: 'Right click or long press to see the menu with a custom toolbar.',
23+
text: 'Right click (desktop) or long press (mobile) to see the menu with a custom toolbar.',
1724
);
1825

26+
@override
27+
void initState() {
28+
super.initState();
29+
// On web, disable the browser's context menu since this example uses a custom
30+
// Flutter-rendered context menu.
31+
if (kIsWeb) {
32+
BrowserContextMenu.disableContextMenu();
33+
}
34+
}
35+
36+
@override
37+
void dispose() {
38+
if (kIsWeb) {
39+
BrowserContextMenu.enableContextMenu();
40+
}
41+
super.dispose();
42+
}
43+
1944
@override
2045
Widget build(BuildContext context) {
2146
return MaterialApp(

examples/api/lib/material/context_menu/selectable_region_toolbar_builder.0.dart

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@
55
// This example demonstrates a custom context menu in non-editable text using
66
// SelectionArea.
77

8+
import 'package:flutter/foundation.dart';
89
import 'package:flutter/material.dart';
10+
import 'package:flutter/services.dart';
911

1012
void main() => runApp(const MyApp());
1113

12-
const String text = 'I am some text inside of SelectionArea. Right click or long press me to show the customized context menu.';
14+
const String text = 'I am some text inside of SelectionArea. Right click (desktop) or long press (mobile) me to show the customized context menu.';
1315

14-
class MyApp extends StatelessWidget {
16+
class MyApp extends StatefulWidget {
1517
const MyApp({super.key});
1618

19+
@override
20+
State<MyApp> createState() => _MyAppState();
21+
}
22+
23+
class _MyAppState extends State<MyApp> {
1724
void _showDialog (BuildContext context) {
1825
Navigator.of(context).push(
1926
DialogRoute<void>(
@@ -24,6 +31,24 @@ class MyApp extends StatelessWidget {
2431
);
2532
}
2633

34+
@override
35+
void initState() {
36+
super.initState();
37+
// On web, disable the browser's context menu since this example uses a custom
38+
// Flutter-rendered context menu.
39+
if (kIsWeb) {
40+
BrowserContextMenu.disableContextMenu();
41+
}
42+
}
43+
44+
@override
45+
void dispose() {
46+
if (kIsWeb) {
47+
BrowserContextMenu.enableContextMenu();
48+
}
49+
super.dispose();
50+
}
51+
2752
@override
2853
Widget build(BuildContext context) {
2954
return MaterialApp(

examples/api/test/material/context_menu/context_menu_controller.0_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:flutter/foundation.dart';
56
import 'package:flutter/gestures.dart';
67
import 'package:flutter/material.dart';
8+
import 'package:flutter/services.dart';
79
import 'package:flutter_api_samples/material/context_menu/context_menu_controller.0.dart' as example;
810
import 'package:flutter_test/flutter_test.dart';
911

@@ -13,6 +15,8 @@ void main() {
1315
const example.MyApp(),
1416
);
1517

18+
expect(BrowserContextMenu.enabled, !kIsWeb);
19+
1620
expect(find.byType(AdaptiveTextSelectionToolbar), findsNothing);
1721

1822
// Right clicking the middle of the app shows the custom context menu.

examples/api/test/material/context_menu/editable_text_toolbar_builder.0_test.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
// found in the LICENSE file.
44

55
import 'package:flutter/cupertino.dart';
6+
import 'package:flutter/foundation.dart';
67
import 'package:flutter/material.dart';
8+
import 'package:flutter/services.dart';
79
import 'package:flutter_api_samples/material/context_menu/editable_text_toolbar_builder.0.dart' as example;
810
import 'package:flutter_test/flutter_test.dart';
911

1012
void main() {
1113
testWidgets('showing and hiding the context menu in TextField with custom buttons', (WidgetTester tester) async {
1214
await tester.pumpWidget(
13-
example.MyApp(),
15+
const example.MyApp(),
1416
);
1517

18+
expect(BrowserContextMenu.enabled, !kIsWeb);
19+
1620
await tester.tap(find.byType(EditableText));
1721
await tester.pump();
1822

examples/api/test/material/context_menu/editable_text_toolbar_builder.1_test.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:flutter/foundation.dart';
56
import 'package:flutter/gestures.dart';
67
import 'package:flutter/material.dart';
8+
import 'package:flutter/services.dart';
79
import 'package:flutter_api_samples/material/context_menu/editable_text_toolbar_builder.1.dart' as example;
810
import 'package:flutter_test/flutter_test.dart';
911

1012
void main() {
1113
testWidgets('showing and hiding the custom context menu in TextField with a specific selection', (WidgetTester tester) async {
1214
await tester.pumpWidget(
13-
example.MyApp(),
15+
const example.MyApp(),
1416
);
1517

18+
expect(BrowserContextMenu.enabled, !kIsWeb);
19+
1620
expect(find.byType(AdaptiveTextSelectionToolbar), findsNothing);
1721

1822
// Right clicking the Text in the TextField shows the custom context menu,

examples/api/test/material/context_menu/editable_text_toolbar_builder.2_test.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
import 'package:flutter/cupertino.dart';
66
import 'package:flutter/foundation.dart';
77
import 'package:flutter/material.dart';
8+
import 'package:flutter/services.dart';
89
import 'package:flutter_api_samples/material/context_menu/editable_text_toolbar_builder.2.dart' as example;
910
import 'package:flutter_test/flutter_test.dart';
1011

1112
void main() {
1213
testWidgets('showing and hiding the context menu in TextField with a custom toolbar', (WidgetTester tester) async {
1314
await tester.pumpWidget(
14-
example.MyApp(),
15+
const example.MyApp(),
1516
);
1617

18+
expect(BrowserContextMenu.enabled, !kIsWeb);
19+
1720
await tester.tap(find.byType(EditableText));
1821
await tester.pump();
1922

examples/api/test/material/context_menu/selectable_region_toolbar_builder.0_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:flutter/foundation.dart';
56
import 'package:flutter/gestures.dart';
67
import 'package:flutter/material.dart';
8+
import 'package:flutter/services.dart';
79
import 'package:flutter_api_samples/material/context_menu/selectable_region_toolbar_builder.0.dart' as example;
810
import 'package:flutter_test/flutter_test.dart';
911

@@ -13,6 +15,8 @@ void main() {
1315
const example.MyApp(),
1416
);
1517

18+
expect(BrowserContextMenu.enabled, !kIsWeb);
19+
1620
// Allow the selection overlay geometry to be created.
1721
await tester.pump();
1822

0 commit comments

Comments
 (0)