I have to copy data of a certian field to the clipboard when the textbutton.icon is pressed.How can I do that?
CodePudding user response:
Have you tried googling?
First google result:
import 'package:flutter/services.dart';
onTap: () {
Clipboard.setData(ClipboardData(text: "your text"));
},
source: https://www.codegrepper.com/code-examples/dart/how to copy text to clipboard flutter
CodePudding user response:
Try below code, refer setData
and ClipboardData
or you can be use clipboard
package
import below library/package in your code
import 'package:flutter/services.dart';
Your Widget:
InkWell(
onTap: () {
Clipboard.setData(
ClipboardData(
text: "Your Copy text",
),
);
},
child: Text("Your Copy text"),
),
CodePudding user response:
Please try below code
Using ClipBoard
// copy to clipboard
Future<void> _copyToClipboard() async {
await Clipboard.setData(ClipboardData(text: "Text to be copied"));
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('Copied to clipboard'),
));
}
IconButton(
icon: const Icon(Icons.copy),
onPressed: _copyToClipboard,
),
// Get Data From System Clipboard
String _textValue = '';
void _getClipboard() async {
ClipboardData data = await Clipboard.getData(Clipboard.kTextPlain);
setState(() {
_textValue = data.text;
});
}
// or You can replace Clipboard.kTextPlain with text/plain to getData // from Clipboard.
ClipboardData data = await Clipboard.getData('text/plain');
Using Selectable Text
SelectableText(
"text to be copied" ?? "",
style: TextStyle(color: Colors.blue, fontWeight: FontWeight.bold, fontSize: 48),
textAlign: TextAlign.center,
toolbarOptions: ToolbarOptions(copy: true, selectAll: true),
),