📜  Flutter – 从剪贴板复制和粘贴

📅  最后修改于: 2022-05-13 01:55:07.494000             🧑  作者: Mango

Flutter – 从剪贴板复制和粘贴

应用程序通常涉及从用户那里获取输入,并且用户希望从剪贴板粘贴信息,但应用程序不支持它。这可能很烦人。为了增强用户体验, Flutter开发人员提出了一个包剪贴板,允许用户复制和粘贴文本。它还允许用户从剪贴板粘贴信息。在本文中,我们将研究它的简单实现。

步骤:添加依赖

在项目的pubspec.yaml文件中,在依赖项部分添加剪贴板包。然后运行 pub get 进行配置。

第二步:导入依赖

现在,在安装包之后,是时候将它导入 main.js 了。dart如下:

Dart
import 'package:clipboard/clipboard.dart';


Dart
TextEditingController message = TextEditingController();


Dart
FlutterClipboard.copy(message.text).then((value) => print('copied text'));


Dart
FlutterClipboard.paste().then((value) {
             setState(() {
                  message.text = value;
              });
  });


Dart
import 'package:clipboard/clipboard.dart';
import 'package:flutter/material.dart';
  
void main() {
  runApp(MyApp());
}
  
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Clipboard Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      home: HomePage(),
    );
  }
}
  
class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}
  
class _HomePageState extends State {
  TextEditingController message = TextEditingController();
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("GeeksForGeeks"),
        centerTitle: true,
      ),
      body: Container(
        child: Padding(
          padding: const EdgeInsets.all(20.0),
          child: SingleChildScrollView(
            child: Column(
              children: [
                SizedBox(
                  height: 100,
                ),
                TextFormField(
                  controller: message,
                  decoration: InputDecoration(hintText: 'Enter text'),
                ),
                SizedBox(
                  height: 40,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    ElevatedButton(
                        onPressed: () {
                          if (message.text.trim() == "") {
                              
                            // do nothing
                          } else {
                            FlutterClipboard.copy(message.text)
                                .then((value) => print('copied text'));
                          }
                        },
                        child: Text(
                          'COPY',
                          style: TextStyle(color: Colors.white),
                        )),
                    ElevatedButton(
                      onPressed: () {
                        FlutterClipboard.paste().then((value) {
                          setState(() {
                            message.text = value;
                          });
                        });
                      },
                      child: Text(
                        'PASTE',
                        style: TextStyle(color: Colors.white),
                      ),
                    )
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}


第 3 步:构建应用程序

  • 初始化TextEditingController()消息,它将接受用户的输入。

Dart

TextEditingController message = TextEditingController();
  • 创建两个按钮——一个用于复制文本,另一个用于将复制的文本粘贴到文本字段中。

要复制文本,我们将使用FlutterClipboard.copy()函数。

Dart

FlutterClipboard.copy(message.text).then((value) => print('copied text'));

要粘贴文本,我们使用 FlutterClipboard.paste()函数并添加到文本字段,我们将文本字段的值设置为复制文本。

Dart

FlutterClipboard.paste().then((value) {
             setState(() {
                  message.text = value;
              });
  });

完整的源代码:

Dart

import 'package:clipboard/clipboard.dart';
import 'package:flutter/material.dart';
  
void main() {
  runApp(MyApp());
}
  
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Clipboard Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      home: HomePage(),
    );
  }
}
  
class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}
  
class _HomePageState extends State {
  TextEditingController message = TextEditingController();
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("GeeksForGeeks"),
        centerTitle: true,
      ),
      body: Container(
        child: Padding(
          padding: const EdgeInsets.all(20.0),
          child: SingleChildScrollView(
            child: Column(
              children: [
                SizedBox(
                  height: 100,
                ),
                TextFormField(
                  controller: message,
                  decoration: InputDecoration(hintText: 'Enter text'),
                ),
                SizedBox(
                  height: 40,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    ElevatedButton(
                        onPressed: () {
                          if (message.text.trim() == "") {
                              
                            // do nothing
                          } else {
                            FlutterClipboard.copy(message.text)
                                .then((value) => print('copied text'));
                          }
                        },
                        child: Text(
                          'COPY',
                          style: TextStyle(color: Colors.white),
                        )),
                    ElevatedButton(
                      onPressed: () {
                        FlutterClipboard.paste().then((value) {
                          setState(() {
                            message.text = value;
                          });
                        });
                      },
                      child: Text(
                        'PASTE',
                        style: TextStyle(color: Colors.white),
                      ),
                    )
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

输出: