【问题标题】:Flutter, how to copy text after pressing the button?Flutter,按下按钮后如何复制文本?
【发布时间】:2020-04-21 07:34:10
【问题描述】:

对于我不完美的英语,我真的很抱歉;)

我想创建一个按钮,当按下此按钮时将复制文本值。我正在寻找它,但我没有找到任何信息。如何在颤振框架中创建自动复制?

提前感谢您的帮助。

【问题讨论】:

标签: user-interface button flutter dart copy


【解决方案1】:

首先,为您的String 指定一个名称:

String quote = "This is a very awesome quote";

其次,将String复制到剪贴板?:

onPressed: (){
    Clipboard.setData(ClipboardData(text: quote));
},

要通知用户它已完成,您可以使用SnackBar

onPressed: () =>
  Clipboard.setData(ClipboardData(text: inviteLink))
    .then((value) { //only if ->
       ScaffoldMessenger.of(context).showSnackBar(snackBar)); // -> show a notification
},

【讨论】:

  • 不需要第三方库,只需导入import 'package:flutter/services.dart';并使用Clipboard
【解决方案2】:

您可以使用这个库clipboard_manager 来执行将文本存储在剪贴板中的实际操作。然后只需通过TextEditingController 实例访问您要复制的文本。

RaisedButton(
  child: Text('Copy'),
  onPressed: () {
    ClipboardManager.copyToClipBoard(
            _textEditingController.value.text)
        .then((result) {
      final snackBar = SnackBar(
        content: Text('Copied to Clipboard'),
        action: SnackBarAction(
          label: 'Undo',
          onPressed: () {},
        ),
      );
      Scaffold.of(context).showSnackBar(snackBar);
    });
  },
),

或通过变量访问String

RaisedButton(
  child: Text('Copy'),
  onPressed: () {
    ClipboardManager.copyToClipBoard(
            _variableContainingString)
        .then((result) {
      final snackBar = SnackBar(
        content: Text('Copied to Clipboard'),
        action: SnackBarAction(
          label: 'Undo',
          onPressed: () {},
        ),
      );
      Scaffold.of(context).showSnackBar(snackBar);
    });
  },
),

【讨论】:

    【解决方案3】:

    你可以使用这个插件clipboard

    FlutterClipboard.copy('hello flutter friends').then(( value ) => print('copied'));
    

    【讨论】:

      猜你喜欢
      • 2021-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多