flutter:复制粘贴功能
一,代码
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:flutter/services.dart';
class CopyPage extends StatefulWidget {
final Map arguments;
// 为title设置一个默认参数,这样的跳转该界面时可以不传值。
CopyPage({super.key, required this.arguments});
@override
State<CopyPage> createState() => _CopyPageState();
}
class _CopyPageState extends State<CopyPage> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.primaryContainer,
title: Text(widget.arguments["title"]),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
String content = "空山新雨后,天气晚来秋";
Clipboard.setData(ClipboardData(text: content));
},
child: Row(
mainAxisSize: MainAxisSize.min, // 根据内容调整大小
children: <Widget>[
Icon(Icons.add), // 图标在左侧
SizedBox(width: 10), // 可选:添加一些间隔
Text("复制文本到剪贴板"), // 文本在右侧
],
),
),
SizedBox(height: 16),
ElevatedButton(
onPressed: () async {
var content = await Clipboard.getData(Clipboard.kTextPlain);
Fluttertoast.showToast(
msg: "从剪贴板中读取到的内容:"+(content?.text ?? ""),
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0,
//duration
);
},
child: Row(
mainAxisSize: MainAxisSize.min, // 根据内容调整大小
children: <Widget>[
Icon(Icons.add), // 图标在左侧
SizedBox(width: 10), // 可选:添加一些间隔
Text("从剪贴板读取文本"), // 文本在右侧
],
),
),
],
),
),
);
}
}