Ad

Flutter - Paste Text Over Other Text Already Written In A TextField

- 1 answer

I have a program that copies and pastes text in flutter, the problem is that when I copy the text and paste it, it replaces the one that was previously written, in this case I want to write over the text already written before and that it is pasted with a space, like a normal copy and paste. I hope your help, thank you.

TextEditingController textEditingController;
String paste = '';

_textField() {
    return SliverToBoxAdapter(
      child: Padding(
        padding: EdgeInsets.all(30.0),
        child: TextFormField(
         
          controller: textEditingController,
        
        ),
      ),
    );
  }

_buttons() {
    return SliverToBoxAdapter(
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              IconButton(
                icon: Icon(Icons.content_copy),
                onPressed: () async {
                  await FlutterClipboard.copy(textEditingController.text);

                  Scaffold.of(context).showSnackBar(
                    SnackBar(content: Text('✓   Copied to Clipboard')),
                  );
                },
              ),
              IconButton(
                icon: Icon(Icons.paste),
                onPressed: () async {
                  final value = await FlutterClipboard.paste();

                  setState(() {
                    this.paste = value;
                  });
                },
              )
            ],
          ),
          SizedBox(
            height: 20,
          ),
          Text(
            'Clipboard Text',
            style: TextStyle(fontSize: 20),
          )
        ],
      ),
    );
  }

Ad

Answer

you must add the copied value to the current value.

Try this

setState(() {
   this.paste = '${this.paste}, $value';
});
Ad
source: stackoverflow.com
Ad