flutter icon indicating copy to clipboard operation
flutter copied to clipboard

Flutter clipboard should support images

Open robsonmeemo opened this issue 6 years ago • 17 comments

Use case

It would be nice to also support videos and binary data in general. But having images would already be a big help.

Proposal

I can write a plugin but such basic behavior should be part of the Flutter framework. Having it in the framework also allows integrating it with Flutter widgets like TextField.

Related issues

https://github.com/flutter/flutter/issues/30719

robsonmeemo avatar May 03 '19 18:05 robsonmeemo

Hi, Any update on this feature? Are there any alternatives at the moment, for importing/exporting an image copied to the clipboard?

Thanks

yanivshaked avatar Dec 21 '20 06:12 yanivshaked

same problem/question. Any update?

fabioselau077 avatar Dec 23 '20 23:12 fabioselau077

Yes, please, very much needed! In my use-case mostly text/html. But image/... as well.

tiloc avatar Apr 03 '21 11:04 tiloc

For those interested: I got it to work on iOS through Flutter Channels:

// main.dart

class MyTextSelectionControls extends CupertinoTextSelectionControls {
// https://api.flutter.dev/flutter/widgets/TextSelectionControls/handlePaste.html
// https://flutter.dev/docs/development/platform-integration/platform-channels#step-4-add-an-ios-platform-specific-implementation
  static final channelName = 'clipboard/image';
  final methodChannel = MethodChannel(channelName);

  Function(ImageProvider) callback;
  MyTextSelectionControls(this.callback);

  @override
  Future<void> handlePaste(TextSelectionDelegate delegate) async {
    await getClipboardImage();

    final TextEditingValue value =
        delegate.textEditingValue; // Snapshot the input before using `await`.
    final ClipboardData data = await Clipboard.getData(Clipboard.kTextPlain);

    if (data != null) {
      delegate.textEditingValue = TextEditingValue(
        text: value.selection.textBefore(value.text) +
            data.text +
            value.selection.textAfter(value.text),
        selection: TextSelection.collapsed(
            offset: value.selection.start + data.text.length),
      );
    }
    delegate.bringIntoView(delegate.textEditingValue.selection.extent);
    delegate.hideToolbar();
  }

  getClipboardImage() async {
    try {
      final result = await methodChannel.invokeMethod('getClipboardImage');
      ImageProvider prov = Image.memory(result).image;
      callback(prov);
    } on PlatformException catch (e) {
      print("error in getting clipboard image");
      print(e);
    }
  }
}

// AppDelegate.swift

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    
    let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
        let batteryChannel = FlutterMethodChannel(name: "clipboard/image",
                                                  binaryMessenger: controller.binaryMessenger)
        batteryChannel.setMethodCallHandler({
          (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
          // Note: this method is invoked on the UI thread.
            guard call.method == "getClipboardImage" else {
              result(FlutterMethodNotImplemented)
              return
            }
            self.getClipboardImage(result: result)
        })
    
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

    
    // https://flutter.dev/docs/development/platform-integration/platform-channels#step-4-add-an-ios-platform-specific-implementation
    // https://www.raywenderlich.com/2882495-your-own-image-picker-with-flutter-channels#toc-anchor-008
    private func getClipboardImage(result: FlutterResult) {
      
        let image = UIPasteboard.general.image;
        
        if (image == nil) {
            print("no image in clipboard")
            return
        }
        
        let data = image!.jpegData(compressionQuality: 0.9)
        result(data)
        
    }
    
}

Full Gist: https://gist.github.com/Krecharles/4338276a4b6763378544d276d5998408

Krecharles avatar May 05 '21 09:05 Krecharles

same problem/question. Any update?

3215466800 avatar Dec 04 '21 02:12 3215466800

same problem/question. Any update?

3215466800 avatar Dec 06 '21 03:12 3215466800

Also facing same issue. Any updates?

JaimonRamanan avatar Jan 28 '22 11:01 JaimonRamanan

this feature request opened at almost 2 years ago, is there any plan for it ?

taojoe avatar Feb 23 '22 09:02 taojoe

It's a requirement for my current project. Hoping to see it soon.

Eerey avatar Apr 18 '22 13:04 Eerey

https://pub.dev/packages/pasteboard is helpful.

sensuikan1973 avatar Jul 27 '22 16:07 sensuikan1973

issue opened 3 years go, any suggestions here ? thanks

sherry0429 avatar Aug 17 '22 08:08 sherry0429

is there any plan for it ?

longgong avatar Sep 07 '22 09:09 longgong

need support in this issue... but no any update

UmairSaqibBhutta avatar Oct 03 '22 05:10 UmairSaqibBhutta

need support in this issue... but no any update

Use this: sensuikan1973 commented [on 27 Jul]

https://pub.dev/packages/pasteboard is helpful.

Eerey avatar Oct 03 '22 15:10 Eerey

Seems like a reasonable expectation of a modern UX development framework designed to work on modern devices that you can copy/paste to/from the OS clipboard buffer with both text AND image. How many years have we had graphical operating systems with the ability to copy and paste images? Probably close to 30 years !!

I love Flutter, but sometimes the depth on features vs the breadth of features across the supported platforms seems to be out of whack. Not sure where the focus and energy is being invested, but doesn't seem to be with much direction or planning.... anyway, I'm sure eventually it will be looked into... though after 3 years we seem to be no further advanced :-(

gslender avatar Dec 01 '22 23:12 gslender

@gslender Flutter is also a framework that is highly driven by the community. Did you try pasteboard ? In my case, it works flawlessly.

Eerey avatar Dec 02 '22 08:12 Eerey

@Eerey yep, but the support of all platforms and all features is inconsistent (eg paste of image is poorly supported).

If the excuse for poorly supporting basic platform functionality is always the communities fault then Flutter is doomed. The project team need to enroll more people and delegate more control - it's going to quickly suffocate under its own weight if they don't close these types of gaps soon.

gslender avatar Dec 02 '22 11:12 gslender

Seems like a duplicate of https://github.com/flutter/flutter/issues/23603? Can this be closed?

knopp avatar Feb 15 '23 15:02 knopp

any updates?

MohammedSalehelShazly avatar Jul 29 '23 16:07 MohammedSalehelShazly

Please remember https://github.com/flutter/flutter/wiki/Issue-hygiene#do-not-add-me-too-or-same-or-is-there-an-update-comments-to-bugs

guidezpl avatar Jul 30 '23 19:07 guidezpl

https://pub.dev/packages/pasteboard is helpful.

It is helpful but it's better if Flutter supports it officially. Currently we have to:

  1. Override the Ctrl/Command+V event by using Shortcuts and Actions widget
  2. Override the context menu of TextField by using contextMenuBuilder
  3. Disable the default context menu on web by using BrowserContextMenu.disableContextMenu() (which also disables the context menu in other places 🤯).

I only want to override the paste action in the Textfield without affecting the context menu in other widgets, and it seems impossible at this point.

crizant avatar Feb 14 '24 07:02 crizant

+1, this is crucial functionality that has been lacking for years. Platform channels seem to be the only current solution, which contradicts the multi-platform nature of Flutter.

ds797 avatar Apr 14 '24 05:04 ds797