如何在Flutter的TextField中接受图像

huangapple go评论72阅读模式
英文:

How to accept images in TextField Flutter

问题

在Android中,当我们截取屏幕时,截图会存储在剪贴板中。现在,如果我打开WhatsApp并点击消息字段,我的键盘会给我建议从剪贴板粘贴图像,如下所示。

我猜这是因为TextField在某种程度上告诉操作系统它可以接受图像。如何在Flutter的TextField中实现相同的行为?如果不是TextField,是否有一种方法可以实现从剪贴板粘贴图像到我们的应用程序中?

我在其他平台上找到了一个pub包 - https://pub.dev/packages/pasteboard 但似乎不支持Android。

英文:

In android, when we take a screenshot, it gets stored in the clipboard. Now if I open WhatsApp, and tap on the message field. My keyboard gives me a suggestion to paste the image from the clipboard, as shown below.

如何在Flutter的TextField中接受图像

I am guessing this is because the TextField somehow tells the OS that it can accept images. How can I achieve the same behavior in a TextField of flutter? If not a TextField, is there any way to implement a button that can paste image from clipboard into our app?

I have found a pub package for other platforms - https://pub.dev/packages/pasteboard but it doesn't seem to support android.

答案1

得分: 1

非常好的问题,位于Flutter的前沿。

要获取此功能,请切换到Flutter的主分支(命令 flutter channel master),这将包括 PR 110052,该请求添加了此功能。

要使用它,只需将以下内容添加到您的TextField:

//...
  contentInsertionConfiguration: ContentInsertionConfiguration(
    onContentInserted : (_){/*在此处添加您的回调函数*/},
    allowedMimeTypes: ["image/png"/*以及其他的类型*/],
  }),
//...

然后,它将使用特定于Android的 内容提交 API 告诉操作系统,您的TextField接受给定的 MIME 类型;Gboard 将识别此内容,并建议具有匹配 MIME 类型的内容存储在剪贴板中。

英文:

very good question, on the bleeding edge of flutter

to get this functionality switch to the flutter master channel (command flutter channel master), this will now include PR 110052 which adds this functionality.

to use it simply add this to your TextField:

//...
  contentInsertionConfiguration: ContentInsertionConfiguration(
    onContentInserted : (_){/*your cb here*/}
    allowedMimeTypes: ["image/png",/*...*/],
  }),
//...

this will then use the android specific content commit api to (as you guessed) tell the OS that your TextField accepts the given mimetypes; gboard will recognize this and suggest the content with fitting mimetypes in your clipboard

答案2

得分: 0

你可以使用这个包,它支持所有平台 super_clipboard,然后通过添加一个图标按钮来自定义处理过程,例如。

请按照安装步骤进行操作,因为该插件在内部使用 Rust 来实现低级平台特定功能。

英文:

You can use this package it supports all platforms super_clipboard
then customize the process by adding an icon button for example.

follow the installation steps because the plugin uses Rust internally to implement low-level platform-specific functionality

答案3

得分: 0

在Flutter中,您可以使用FlutterClipboard包与系统剪贴板进行交互。

import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_clipboard_manager/flutter_clipboard_manager.dart';

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(
        title: Text("My Widget"),
      ),
      body: Column(
        children: [
          TextField(
            decoration: InputDecoration(
              hintText: "Paste the screenshot here",
            ),
            keyboardType: TextInputType.multiline,
            maxLines: null,
            onTap: () async {
              var clipboardData = await Clipboard.getData(Clipboard.kTextPlain);
              if (clipboardData != null && clipboardData.text != null) {
                setState(() {
                  _textEditingController.text = clipboardData.text;
                });
              }
            },
          ),
        ],
      ),
    );
  }
}

如果您需要更多帮助或有其他问题,请随时提问。

英文:

In Flutter, you can use the FlutterClipboard package to interact with the system clipboard.

import &#39;dart:typed_data&#39;;
import &#39;package:flutter/material.dart&#39;;
import &#39;package:flutter/services.dart&#39;;
import &#39;package:flutter_clipboard_manager/flutter_clipboard_manager.dart&#39;;

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() =&gt; _MyWidgetState();
}

class _MyWidgetState extends State&lt;MyWidget&gt; {
  final GlobalKey&lt;ScaffoldState&gt; _scaffoldKey = GlobalKey&lt;ScaffoldState&gt;();
  

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(
        title: Text(&quot;My Widget&quot;),
      ),
      body: Column(
        children: [
          TextField(
            decoration: InputDecoration(
              hintText: &quot;Paste the screenshot here&quot;,
            ),
            keyboardType: TextInputType.multiline,
            maxLines: null,
            onTap: () async {
              var clipboardData = await Clipboard.getData(Clipboard.kTextPlain);
              if (clipboardData != null &amp;&amp; clipboardData.text != null) {
                setState(() {
                  _textEditingController.text = clipboardData.text;
                });
              }
            },
          ),
        ],
      ),
    );
  }
}

答案4

得分: 0

Copying files to the clipboard is not widely supported; however, as a general standard, some applications follow the rule of copying the path of the file to the clipboard in order to access it.

你可以像这样做:

Future<String> get _localPath async {
  final directory = await getApplicationDocumentsDirectory();
  return directory.path;
}

Future<void> _copyFile() async {
   final path = await _localPath;
   Clipboard.setData(new ClipboardData(text: "content://${_localPath}/image.png"));
   return;
}

And these are sources I used when I making this answer. It will usefull for you.

https://stackoverflow.com/questions/65432586/clipboard-an-image-in-flutter/68882967#68882967

https://stackoverflow.com/questions/30660771/how-to-copy-images-files-to-clipboard-in-android-any-alternative-methods-steps/30661133#30661133

And If you really intersting solve this problem you can write in native functionally and used in flutter with platform channel

英文:

Copying files to the clipboard is not widely supported; however, as a general standard, some applications follow the rule of copying the path of the file to the clipboard in order to access it.

you can doing something like this.

Future&lt;String&gt; get _localPath async {
  final directory = await getApplicationDocumentsDirectory();
  return directory.path;
}

Future&lt;void&gt; _copyFile() async {
   final path = await _localPath;
   Clipboard.setData(new ClipboardData(text: &quot;content://${_localPath}/image.png&quot;));
   return;
}

And these are sources I used when I making this answer. It will usefull for you.

https://stackoverflow.com/questions/65432586/clipboard-an-image-in-flutter/68882967#68882967

https://stackoverflow.com/questions/30660771/how-to-copy-images-files-to-clipboard-in-android-any-alternative-methods-steps/30661133#30661133

And If you really intersting solve this problem you can write in native functionally and used in flutter with platform channel

huangapple
  • 本文由 发表于 2023年3月7日 13:34:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658357.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定