1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
| import 'dart:async'; import 'package:flutter/services.dart'; import 'package:flutter/material.dart'; import 'package:clipboard_watcher/clipboard_watcher.dart'; import 'package:super_clipboard/super_clipboard.dart';
class Paster extends StatefulWidget { const Paster({Key? key}) : super(key: key);
@override PasterState createState() => PasterState(); }
class PasterState extends State<Paster> with ClipboardListener { List<ClipboardHistoryItem> historyList = [];
bool tempDisableListener = false;
@override void initState() { clipboardWatcher.addListener(this); clipboardWatcher.start(); super.initState(); }
@override void dispose() { clipboardWatcher.removeListener(this); clipboardWatcher.stop(); super.dispose(); }
@override void onClipboardChanged() async { if (tempDisableListener) { return; }
final item = await ClipboardHistoryItem.readFromClipboard(); if (item.contentType == ClipboardContentType.empty) { return; } setState(() { historyList.insert(0, item); }); }
@override Widget build(BuildContext context) { return Column( children: [ const Padding(padding: EdgeInsets.all(8), child: Text('粘贴板历史')), const Divider(), Flexible( child: Align( alignment: Alignment.topLeft, child: _buildClipboardHistoryList(context))), ], ); }
Widget _buildClipboardHistoryList(BuildContext context) { return ConstrainedBox( constraints: const BoxConstraints(maxHeight: 300), child: ListView.separated( padding: const EdgeInsets.all(8), scrollDirection: Axis.horizontal, itemCount: historyList.length, itemBuilder: (BuildContext context, int index) { final historyItem = historyList[index];
late Widget title; switch (historyItem.contentType) { case ClipboardContentType.text: title = Text(historyItem.text!); break; case ClipboardContentType.image: title = Image.memory(historyItem.imageBytes!); break; default: title = const Text('未知类型'); }
return Container( width: 280, padding: const EdgeInsets.all(8), decoration: BoxDecoration( border: Border.all(color: Colors.grey), borderRadius: BorderRadius.circular(8)), child: Column( children: [ Row( children: [ Expanded( child: Text( '${historyItem.timestamp!.hour}:${historyItem.timestamp!.minute}:${historyItem.timestamp!.second}')), IconButton( onPressed: () { setState(() { historyList.removeAt(index); }); }, icon: const Icon(Icons.delete)) ], ), const Divider(), Expanded( child: ListTile( title: title, onTap: () async { tempDisableListener = true; await historyItem.writeToClipboard(); Future.delayed(const Duration(milliseconds: 100), () { tempDisableListener = false; setState(() { historyList.removeAt(index); historyList.insert(0, historyItem); }); });
const snackBar = SnackBar( content: Text('已复制到粘贴板~'), duration: Duration(seconds: 2), showCloseIcon: true, ); if (!context.mounted) return; ScaffoldMessenger.of(context) .showSnackBar(snackBar); })) ], )); }, separatorBuilder: (BuildContext context, int index) => const VerticalDivider(width: 10, color: Colors.transparent), )); } }
enum ClipboardContentType { empty, text, image }
class ClipboardHistoryItem { String? text; Uint8List? imageBytes; DateTime? timestamp;
late ClipboardContentType contentType;
ClipboardHistoryItem({this.text, this.imageBytes}) { if (text != null) { contentType = ClipboardContentType.text; } else if (imageBytes != null) { contentType = ClipboardContentType.image; } else { contentType = ClipboardContentType.empty; } timestamp = DateTime.now(); }
Future<void> writeToClipboard() async { final item = DataWriterItem(); if (imageBytes != null) { item.add(Formats.png(imageBytes!)); } if (text != null) { item.add(Formats.plainText(text!)); } await ClipboardWriter.instance.write([item]); }
static Future<ClipboardHistoryItem> readFromClipboard() async { final reader = await ClipboardReader.readClipboard();
if (reader.canProvide(Formats.plainText)) { final text = await reader.readValue(Formats.plainText); return ClipboardHistoryItem(text: text); } else if (reader.canProvide(Formats.png)) { final imageBytes = await ClipboardHistoryItem.readFile(reader, Formats.png); return ClipboardHistoryItem(imageBytes: imageBytes); } else { return ClipboardHistoryItem(); } }
static Future<Uint8List?>? readFile( ClipboardReader reader, FileFormat format) { final c = Completer<Uint8List?>(); final progress = reader.getFile(format, (file) async { try { final all = await file.readAll(); c.complete(all); } catch (e) { c.completeError(e); } }, onError: (e) { c.completeError(e); }); if (progress == null) { c.complete(null); } return c.future; } }
|