copy method
Copies text to the system clipboard with optional auto-clear.
Returns a ClipboardCopyResult with PII detection info and timing.
final result = await ClipboardShield().copy(
'myP@ssw0rd',
expireAfter: Duration(seconds: 15),
);
Implementation
Future<ClipboardCopyResult> copy(
String text, {
Duration? expireAfter,
}) async {
try {
await Clipboard.setData(ClipboardData(text: text));
// Detect PII.
final piiType = PIIDetector().getPIIType(text);
final bool piiDetected = _config.detectPIIOnCopy && piiType != null;
// Start auto-clear timer.
final expiry = expireAfter ?? _config.defaultExpiry;
DateTime? expiresAt;
Duration? expiresIn;
// Cancel any existing timer.
cancelAutoClear();
if (expiry > Duration.zero) {
expiresAt = DateTime.now().add(expiry);
expiresIn = expiry;
_clearAt = expiresAt;
_autoClearTimer = Timer(expiry, () {
clearNow();
_clearedController.add(null);
_config.onClearCallback?.call();
});
}
final result = ClipboardCopyResult(
success: true,
piiDetected: piiDetected,
piiType: piiType,
expiresAt: expiresAt,
expiresIn: expiresIn,
);
_copiedController.add(result);
_config.onCopyCallback?.call(result);
if (_config.logCopyEvents) {
shieldLog(
'Clipboard: copied text (containsPII: $piiDetected)',
level: 'DEBUG',
);
}
return result;
} catch (e) {
return const ClipboardCopyResult(
success: false,
piiDetected: false,
);
}
}