clipboard_image_picker

A Flutter plugin to retrieve images from the clipboard on iOS and Android. Enables pasting images directly from clipboard into your Flutter apps.

Features

  • ✅ Retrieve images from clipboard on iOS and Android
  • ✅ Check if clipboard contains an image
  • ✅ Simple and easy-to-use API
  • ✅ Cross-platform support

Installation

Add this to your package's pubspec.yaml file:

dependencies:
  clipboard_image_picker: ^0.1.0

Then run:

flutter pub get

Usage

Basic Usage

import 'package:clipboard_image_picker/clipboard_image_picker.dart';
import 'dart:typed_data';

// Get image from clipboard
final imageBytes = await ClipboardImagePicker.getClipboardImage();

if (imageBytes != null) {
  // Use the image bytes
  // For example, save to file or display in Image.memory()
  print('Image found: ${imageBytes.length} bytes');
} else {
  print('No image in clipboard');
}

Check if Clipboard Has Image

final hasImage = await ClipboardImagePicker.hasClipboardImage();
if (hasImage) {
  // Handle image paste
}

Example: Paste Image in TextField

import 'package:flutter/material.dart';
import 'package:clipboard_image_picker/clipboard_image_picker.dart';
import 'dart:io';
import 'package:path_provider/path_provider.dart';

TextField(
  controller: controller,
  contextMenuBuilder: (context, editableTextState) {
    final buttonItems = <ContextMenuButtonItem>[];
    
    // Add default buttons
    buttonItems.addAll(editableTextState.contextMenuButtonItems);
    
    // Add paste button that handles images
    buttonItems.add(
      ContextMenuButtonItem(
        label: 'Paste',
        onPressed: () async {
          ContextMenuController.removeAny();
          
          final imageBytes = await ClipboardImagePicker.getClipboardImage();
          if (imageBytes != null && imageBytes.isNotEmpty) {
            // Save image to file
            final tempDir = await getTemporaryDirectory();
            final file = File('${tempDir.path}/pasted_image.jpg');
            await file.writeAsBytes(imageBytes);
            
            // Handle the image file
            onImagePasted(file);
          } else {
            // Fall back to default paste behavior
            editableTextState.pasteText(SelectionChangedCause.toolbar);
          }
        },
      ),
    );
    
    return AdaptiveTextSelectionToolbar.buttonItems(
      anchors: editableTextState.contextMenuAnchors,
      buttonItems: buttonItems,
    );
  },
)

Platform Support

  • ✅ iOS
  • ✅ Android

API Reference

ClipboardImagePicker.getClipboardImage()

Retrieves an image from the clipboard if available.

Returns: Future<Uint8List?> - The image data as bytes, or null if no image is found.

ClipboardImagePicker.hasClipboardImage()

Checks if clipboard contains an image.

Returns: Future<bool> - true if clipboard has an image, false otherwise.

Notes

  • On iOS, images are converted to JPEG format with 90% quality
  • On Android, images are converted to JPEG format with 90% quality
  • The plugin returns null if clipboard doesn't contain an image or if an error occurs

License

MIT