Skip to content
Related Articles

Related Articles

Flutter – Copy & Paste From Clipboard

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 08 Feb, 2022

The applications generally involve taking input from users, and users want to paste information from the clipboard, but the application doesn’t support it. This can be annoying. To enhance user experience, Flutter developer came up with a package clipboard that allows users to copy text and paste it. It also allows users to paste information from the clipboard. In this article, we will be looking at its simple implementation.

Step: Add the dependency

In pubspec.yaml file of the project, add the clipboard package in the dependencies section. Then run pub get to configure it.

Step 2: Import the dependency

Now, after installing the package, it’s time to import it in main.dart as follows:

Dart




import 'package:clipboard/clipboard.dart';


Step 3: Structuring the Application

  • Initialize the TextEditingController() message, that will take input from the user.

Dart




TextEditingController message = TextEditingController();


  • Create two buttons – One for copying the text and the other for pasting the copied text in the text field.

To copy text, we will use FlutterClipboard.copy() function.

Dart




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


To paste the text we use FlutterClipboard.paste() function and to add to the text field, we set the value of the text field as copied text.

Dart




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


Complete Source Code:

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<HomePage> {
  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: <Widget>[
                SizedBox(
                  height: 100,
                ),
                TextFormField(
                  controller: message,
                  decoration: InputDecoration(hintText: 'Enter text'),
                ),
                SizedBox(
                  height: 40,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    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),
                      ),
                    )
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}


Output:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!