Search code examples
flutterbuttontextfieldcopy-paste

Flutter - how can I add the button to Paste selected text in textfield?


I want to implement button, by clicking that button I want to paste the select text (From any source Exp - from the message app) into the textfield. We have a plugin for make a button to copy the text, do we have anything similar to paste the text.


Solution

  • If the text you want to select is from the same app, you can achieve this using Clipboard

    TextEditingController textFieldController = new TextEditingController();
    Clipboard.setData(new ClipboardData(text: "copied text")); // copy text
    
    onPressed() async { // onPress function of button
      ClipboardData data = await Clipboard.getData('text/plain');
      setState(() {
        textFieldController.text = data.text.toString(); // this will paste "copied text" to textFieldController
      });
    }