selectable_region_toolbar_builder.0.dart 2.83 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This example demonstrates a custom context menu in non-editable text using
// SelectionArea.

8
import 'package:flutter/foundation.dart';
9
import 'package:flutter/material.dart';
10
import 'package:flutter/services.dart';
11

12
void main() => runApp(const SelectableRegionToolbarBuilderExampleApp());
13

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

17 18
class SelectableRegionToolbarBuilderExampleApp extends StatefulWidget {
  const SelectableRegionToolbarBuilderExampleApp({super.key});
19

20
  @override
21
  State<SelectableRegionToolbarBuilderExampleApp> createState() => _SelectableRegionToolbarBuilderExampleAppState();
22 23
}

24 25
class _SelectableRegionToolbarBuilderExampleAppState extends State<SelectableRegionToolbarBuilderExampleApp> {
  void _showDialog(BuildContext context) {
26 27 28
    Navigator.of(context).push(
      DialogRoute<void>(
        context: context,
29
        builder: (BuildContext context) => const AlertDialog(title: Text('You clicked print!')),
30 31 32 33
      ),
    );
  }

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  @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();
  }

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Context menu anywhere'),
        ),
        body: Center(
          child: SizedBox(
            width: 200.0,
            child: SelectionArea(
              contextMenuBuilder: (
                BuildContext context,
                SelectableRegionState selectableRegionState,
              ) {
                return AdaptiveTextSelectionToolbar.buttonItems(
                  anchors: selectableRegionState.contextMenuAnchors,
                  buttonItems: <ContextMenuButtonItem>[
                    ...selectableRegionState.contextMenuButtonItems,
                    ContextMenuButtonItem(
                      onPressed: () {
                        ContextMenuController.removeAny();
                        _showDialog(context);
                      },
                      label: 'Print',
                    ),
                  ],
                );
              },
              child: ListView(
                children: const <Widget>[
                  SizedBox(height: 20.0),
                  Text(text),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}