Fix context menu web examples (#120104)

The context menu examples on the docs site now work on the web.
This commit is contained in:
Justin McCandless
2023-02-06 15:51:11 -08:00
committed by GitHub
parent ec524ed068
commit 575ced6c5a
12 changed files with 169 additions and 18 deletions

View File

@@ -7,15 +7,21 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(const MyApp());
/// A builder that includes an Offset to draw the context menu at.
typedef ContextMenuBuilder = Widget Function(BuildContext context, Offset offset);
class MyApp extends StatelessWidget {
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
void _showDialog (BuildContext context) {
Navigator.of(context).push(
DialogRoute<void>(
@@ -26,6 +32,24 @@ class MyApp extends StatelessWidget {
);
}
@override
void initState() {
super.initState();
// On web, disable the browser's context menu since this example uses a custom
// Flutter-rendered context menu.
if (kIsWeb) {
BrowserContextMenu.disableContextMenu();
}
}
@override
void dispose() {
if (kIsWeb) {
BrowserContextMenu.enableContextMenu();
}
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
@@ -58,7 +82,7 @@ class MyApp extends StatelessWidget {
child: ListView(
children: <Widget>[
Container(height: 20.0),
const Text('Right click or long press anywhere (not just on this text!) to show the custom menu.'),
const Text('Right click (desktop) or long press (mobile) anywhere, not just on this text, to show the custom menu.'),
],
),
),