Daniel Salas DG
Daniel Salas DG

Reputation: 109

Flutter - Paste text over other text already written in a TextField

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),
          )
        ],
      ),
    );
  }

Upvotes: 0

Views: 1426

Answers (1)

you must add the copied value to the current value.

Try this

setState(() {
   this.paste = '${this.paste}, $value';
});

Upvotes: 1

Related Questions