clipboardCopyableText static method

Widget clipboardCopyableText(
  1. BuildContext context,
  2. String text, {
  3. void onCopied(
    1. String text
    )?,
  4. TextStyle? style,
})

Creates a text widget with a copy-to-clipboard button.

Parameters:

  • context: The build context to access theme data
  • text: The text to display and copy
  • onCopied: Optional callback invoked after text is copied
  • style: Optional text style. Defaults to caption style

Returns a Row containing the text and a copy button.

Implementation

static Widget clipboardCopyableText(BuildContext context, String text,
    {void Function(String text)? onCopied, TextStyle? style}) {
  style = style ?? captionStyle(context);
  return Row(children: [
    Text(text, style: style),
    IconButton(
        visualDensity: VisualDensity.compact,
        onPressed: () {
          Clipboard.setData(ClipboardData(text: text));
          if (onCopied != null) {
            onCopied(text);
          }
        },
        icon: Icon(Icons.copy,
            size: style.fontSize, color: Theme.of(context).primaryColor),
        tooltip: 'Click to copy')
  ]);
}