flutter_clipboard 1.0.2 flutter_clipboard: ^1.0.2 copied to clipboard
A Flutter plugin to retrieve text from clipboard and save text explicitly to clipboard in Android and iOS.
import 'package:flutter/material.dart';
import 'package:flutter_clipboard/flutter_clipboard.dart';
enum Action { SEND, RETRIEVE }
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var _textController = TextEditingController();
var _scaffoldKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.amber,
),
home: Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: Text("Flutter Clipboard Example"),
),
body: Padding(
padding: EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
TextFormField(
controller: _textController,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: "Enter text",
labelText: "Enter text",
suffixIcon: IconButton(
icon: Icon(Icons.clear),
onPressed: () => _textController.clear(),
),
),
),
SizedBox(
height: 16.0,
),
_button("COPY TO CLIPBOARD", Action.SEND),
SizedBox(
height: 16.0,
),
_button("RETRIEVE CLIPBOARD", Action.RETRIEVE),
],
),
),
),
);
}
Widget _button(String text, Action action) {
return InkWell(
child: Container(
color: Colors.amber,
child: Padding(
padding: EdgeInsets.symmetric(vertical: 16.0),
child: Text(
text,
style: TextStyle(
fontSize: 18.0,
color: Colors.white,
),
textAlign: TextAlign.center,
),
),
),
onTap: () {
if (action == Action.RETRIEVE) {
FlutterClipboard.retrieve.then(
(clipboardText) {
if (clipboardText != null) {
_textController.text = clipboardText;
_scaffoldKey.currentState.showSnackBar(
SnackBar(
content: Text("Retrived Clipboard Text"),
duration: Duration(seconds: 1),
backgroundColor: Colors.green,
),
);
}
},
);
} else {
FlutterClipboard.send(_textController.text).then(
(success) {
_textController.clear();
_scaffoldKey.currentState.showSnackBar(
SnackBar(
content: Text(
success ? "Copied to Clipboard." : "Failed to copy!"),
duration: Duration(seconds: 1),
backgroundColor: success ? Colors.green : Colors.red,
),
);
},
);
}
},
);
}
}