Copy to Clipboard Text in Flutter | How to Copy text in Clipboard using Flutter ?

flutter researchthinker

To execute below code first add this package in your Flutter project 
flutter pub add clipboard

import 'package:clipboard/clipboard.dart';
import 'package:flutter/material.dart';
import 'package:flutter/material.dart';
import 'package:rttutorials/clip_board.dart';
import 'package:rttutorials/shimmer_flutter.dart';
import 'package:rttutorials/shimmer_in_flutter.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ResearchThinker',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: ResearchThinkerExample(),
    );
  }
}

class ResearchThinkerExample extends StatefulWidget {
  const ResearchThinkerExample({Key? key}) : super(key: key);

  @override
  State<ResearchThinkerExample> createState() => _ResearchThinkerExampleState();
}

class _ResearchThinkerExampleState extends State<ResearchThinkerExample> {
  TextEditingController controller = TextEditingController();
  var newValue = '';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('ResearchThinker Flutter Examples'),
        ),
        body: SingleChildScrollView(
            child: Container(
          height: 700,
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: CopyTextOption(),
          ),
        )),
      ),
    );
  }
}



class CopyTextOption extends StatefulWidget {
  @override
  _CopyTextOptionState createState() => _CopyTextOptionState();
}

class _CopyTextOptionState extends State<CopyTextOption> {

  TextEditingController field = TextEditingController();
  String pasteValue='';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Padding(
          padding: const EdgeInsets.all(20.0),
          child: SingleChildScrollView(
            child: Column(
              children: <Widget>[
                SizedBox(
                  height: 100,
                ),
                TextFormField(
                  controller: field,
                  decoration: InputDecoration(
                      hintText: 'Enter text'
                  ),
                ),
                SizedBox(
                  height: 20,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    InkWell(
                      onTap: (){
                        if(field.text.trim() == ""){
                          print('enter text');
                        } else {
                          print(field.text);
                          FlutterClipboard.copy(field.text).then(( value ) =>
                              print('copied'));
                        }
                      },
                      child: Container(
                        width: 100,
                        height: 50,
                        decoration: BoxDecoration(
                            color: Colors.blue,
                            borderRadius: BorderRadius.circular(15)
                        ),
                        child: Center(child: Text('COPY')),
                      ),
                    ),
                    InkWell(
                      onTap: (){
                        FlutterClipboard.paste().then((value) {
                          setState(() {
                            field.text = value;
                            pasteValue = value;
                          });
                        });
                      },
                      child: Container(
                        width: 100,
                        height: 50,
                        decoration: BoxDecoration(
                            color: Colors.blue,
                            borderRadius: BorderRadius.circular(15)
                        ),
                        child: Center(child: Text('PASTE')),
                      ),
                    )
                  ],
                ),
                SizedBox(
                  height: 20,
                ),
                Text('Clipboard Text: $pasteValue',style: TextStyle(fontSize: 20),)
              ],
            ),
          ),
        ),
      ),
    );
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

web_horizontal
About Us ♢ Disclaimer ♢ Privacy Policy ♢ Terms & Conditions ♢ Contact Us

Copyright © 2023 ResearchThinker.com. All rights reserved.