skip to Main Content

I work on reading book app in flutter, i make the text selectable text, but the problem when i want share the selected text, thier is any way to do it ?

I try use onselectionchanged in selectable to capture the selected text but this not work

2

Answers


  1. Chosen as BEST ANSWER
    SelectableText(
            contextMenuBuilder: (context, editableTextState) {
                final TextEditingValue value = editableTextState.textEditingValue;
                          
                                        // Get text selected
                                        final textSeleted = value.selection.textInside(value.text);
                          
                                        // Get button default of text selection
                                        final List<ContextMenuButtonItem> buttonItems =
                                            editableTextState.contextMenuButtonItems;
                          
                                        // Add new button share
                                        buttonItems.insert(
                                            0,
                                            ContextMenuButtonItem(
                                              label: 'share',
                                              onPressed: () {
                                                // final Uri smsLaunchUri = Uri(
                                                //   scheme: 'sms',
                                                //   path: '07730303670',
                                                //   queryParameters: <String, String>{
                                                //     'body': textSeleted,
                                                //   },
                                                // );
                          
                                                // // Launch sms with text selected
                                                // launchUrl(smsLaunchUri);
                          
                                                // // Close context menu
                                                print("done");
                                                ContextMenuController.removeAny();
                                              },
                                            ));
                          
                                        return AdaptiveTextSelectionToolbar.buttonItems(
                                          anchors: editableTextState.contextMenuAnchors,
                                          buttonItems: buttonItems,
                                        );
                                      },
                                      "${currentSection.content}",
                                    )
    

  2. onselectionchanged should work for you, although it is executed every time the user changes the selection, which I don’t think is what you want, the normal thing is to add a button to the context menu with the one you want to share in your case.

    use contextMenuBuilder,
    note: this example use url_launcher for send sms with the text, you need run

    flutter pub add url_launcher

    example:

    import 'package:flutter/material.dart';
    import 'package:url_launcher/url_launcher.dart';
    
    void main() => runApp(const MyApp());
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Material App',
          home: Scaffold(
            appBar: AppBar(
              title: const Text('Material App Bar'),
            ),
            body: Center(
              child: SelectableText(
                contextMenuBuilder: (context, editableTextState) {
                  final TextEditingValue value = editableTextState.textEditingValue;
    
                  // Get text selected
                  final textSeleted = value.selection.textInside(value.text);
    
                  // Get button default of text selection
                  final List<ContextMenuButtonItem> buttonItems =
                      editableTextState.contextMenuButtonItems;
    
                  // Add new button share
                  buttonItems.insert(
                      0,
                      ContextMenuButtonItem(
                        label: 'share',
                        onPressed: () {
                          final Uri smsLaunchUri = Uri(
                            scheme: 'sms',
                            path: '011122355',
                            queryParameters: <String, String>{
                              'body': textSeleted,
                            },
                          );
    
                          // Launch sms with text selected
                          launchUrl(smsLaunchUri);
    
                          // Close context menu
                          ContextMenuController.removeAny();
                        },
                      ));
    
                  return AdaptiveTextSelectionToolbar.buttonItems(
                    anchors: editableTextState.contextMenuAnchors,
                    buttonItems: buttonItems,
                  );
                },
                'Hello World text bla bla 123',
              ),
            ),
          ),
        );
      }
    }
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search