0

if user Select a text there should be quicknote menu will popup so I Need to show a popup menu when user select text in my flutter project .

I Need to show a popup menu when user select text in my flutter project ,can any body give me any example for this

1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Sep 21, 2023 at 9:56

1 Answer 1

0
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Text Selection Menu Example'),
        ),
        body: TextSelectionMenuDemo(),
      ),
    );
  }
}

class TextSelectionMenuDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: SelectableText(
        'Select this text to show the popup menu.',
        style: TextStyle(fontSize: 18.0),
        onTap: () {
          // Handle text selection here (if needed).
        },
        onSelectionChanged: (TextSelection selection, SelectionChangedCause cause) {
          if (selection.baseOffset != selection.extentOffset) {
            // Text is selected, show the popup menu.
            showTextPopupMenu(context);
          }
        },
      ),
    );
  }

  void showTextPopupMenu(BuildContext context) {
    final RenderBox overlay = Overlay.of(context).context.findRenderObject() as RenderBox;

    final RenderBox text = context.findRenderObject() as RenderBox;
    final Offset offset = text.localToGlobal(Offset.zero, ancestor: overlay);

    final TextSelection selection = TextSelection(baseOffset: 0, extentOffset: 0);
    final List<PopupMenuEntry<String>> popupItems = [
      PopupMenuItem<String>(
        value: 'Copy',
        child: Text('Copy'),
      ),
      PopupMenuItem<String>(
        value: 'Cut',
        child: Text('Cut'),
      ),
      PopupMenuItem<String>(
        value: 'Paste',
        child: Text('Paste'),
      ),
    ];

    showMenu<String>(
      context: context,
      position: RelativeRect.fromRect(offset & text.size, Offset.zero & overlay.size),
      items: popupItems,
    ).then((String? selectedValue) {
      if (selectedValue != null) {
        // Handle selected menu item here.
        print('Selected: $selectedValue');
      }
    });
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.