Files
hash-wallet/lib/new-ui/widgets/copy_wrapper.dart
T
Seth For PrivacyandGitHub 930397cbe6 a11y: receive flow semantics (copy control, pickers, selection state) (#3467)
* Add strings for receive-flow accessibility labels

New English keys: info, long_press_to_copy, standard, token. They replace
hard-coded English in the receive amount modal, the payjoin copy modal and
the addresses long-press menu, and name the copy hint.

* Collapse copy-to-clipboard controls to one accessible button

CopyWrapper gains an optional controlBuilder that hands the copy callback to
the control it builds instead of stacking its own gesture layer on top, and it
now announces the copy through SemanticsService so screen-reader users get the
same feedback the copied state gives sighted users. When there is nothing to
copy it no longer leaves a gesture detector behind that reads as actionable
but does nothing. Its own tap/long-press wrapper is exposed as one merged
button node with a copy hint and a Copy custom action, so tap-to-copy stays
reachable on rows that also navigate.

The receive copy button used an outer gesture detector for the real work while
the visible ModernButton had a no-op onPressed behind two IgnorePointers.
It is now a single ModernButton whose onPressed copies, or opens the payjoin
copy modal when there is no direct URI to copy; the AnimatedSwitcher copied
state is unchanged. The payjoin modal announces the copy and no longer
hard-codes "Standard".

* Add screen-reader semantics to receive flow and address pickers

Every control touched here becomes exactly one merged semantics node with a
localized name, the right role and, where it exists, its selection state:

- receive page: the label chip is a button hinted with set_label and is kept
  out of the semantics tree while it is collapsed to zero height.
- amount modal: token picker and currency picker are buttons named by
  select_token / select_fiat_currency_title, the amount field carries the
  amount label instead of relying on its placeholder, and the visible captions
  are excluded so they are not announced twice.
- amount display: amount, symbol and fiat equivalent are announced together.
- info box: Dismiss is a real button, the info icon and the illustrative
  currency stack are decorative (the message text carries their meaning).
- address type: the row and its chevron triggered the same picker; only the row
  is exposed now, named by address_type.
- address type selector: More Options exposes its expanded state, each option
  row reports selected + inMutuallyExclusiveGroup (its checkbox is excluded so
  the state is announced once), and the collapsed options are unreachable.
- label modal quick-label chips are buttons.
- addresses page: rows are buttons reporting selection, with the long-press
  actions (set label, hide/unhide, info) also exposed as custom semantics
  actions; the checkmark is decorative; the search field keeps a name once it
  has text; show-hidden-addresses is a button.
- currency picker: search field, clear button, currency rows, chain chips and
  network rows get names, roles and selection state; icons and chevrons are
  decorative.

No visual or pointer behaviour changes.

* Drop single-use locals to keep the diff minimal

* Address review: replace deprecated copy announcements with semantic state
2026-08-05 09:16:58 +02:00

116 lines
3.9 KiB
Dart

import 'dart:io';
import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/utils/clipboard_util.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/semantics.dart';
import 'package:flutter/services.dart';
class CopyWrapper extends StatefulWidget {
const CopyWrapper(
{super.key,
this.data,
this.requireLongPress = false,
this.isSensitive = false,
this.builder,
this.controlBuilder,
this.duration = const Duration(milliseconds: 1200)})
: assert((builder == null) != (controlBuilder == null),
"Provide exactly one of builder or controlBuilder");
final ClipboardData? data;
final bool isSensitive;
final bool requireLongPress;
/// Builds content that this widget makes copyable with its own gesture
/// detector. Receives the transient "copied" state.
final Widget Function(BuildContext, bool)? builder;
/// Builds a control that performs the copy itself, so no extra gesture
/// detector or actionable semantics node is stacked on top of it. [onCopy] is
/// null when there is nothing to copy.
final Widget Function(BuildContext context, bool copied, VoidCallback? onCopy)? controlBuilder;
final Duration duration;
@override
State<CopyWrapper> createState() => _CopyWrapperState();
}
class _CopyWrapperState extends State<CopyWrapper> {
bool copied = false;
void handleCopy() async {
if (widget.data == null) return;
ClipboardUtil.setSensitiveDataToClipboard(widget.data!, isSensitive: widget.isSensitive);
HapticFeedback.mediumImpact();
// Screen readers learn about the copy from the transient "copied" state
// below, which build() marks as a live region, instead of from a push
// announcement: those are deprecated on Android and make TalkBack drop
// whatever it was saying. Where we don't render that state, android 13+
// already shows its own clipboard confirmation.
if (await shouldShowCopied()) {
setState(() => copied = true);
Future.delayed(widget.duration, () {
if (mounted) setState(() => copied = false);
});
}
}
@override
Widget build(BuildContext context) {
final VoidCallback? onCopy = widget.data == null ? null : handleCopy;
// The control's own label carries the copied state (Copy -> Copied), so
// merging it into one node and marking that node a live region while copied
// is enough for a screen reader to pick the change up.
if (widget.controlBuilder != null) {
return MergeSemantics(
child: Semantics(
liveRegion: copied,
child: widget.controlBuilder!(context, copied, onCopy),
),
);
}
final content = widget.builder!(context, copied);
// Nothing to copy: don't leave behind a gesture detector that a screen
// reader would present as an actionable target that does nothing.
if (onCopy == null) return content;
return MergeSemantics(
child: Semantics(
button: !widget.requireLongPress,
liveRegion: copied,
hint: widget.requireLongPress ? S.of(context).long_press_to_copy : S.of(context).copy,
customSemanticsActions: <CustomSemanticsAction, VoidCallback>{
CustomSemanticsAction(label: S.of(context).copy): onCopy,
},
child: GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: widget.requireLongPress ? null : onCopy,
onLongPress: !widget.requireLongPress ? null : onCopy,
child: content,
),
),
);
}
// android 13 (sdk 33) added a built-in "text was copied to clipboard" ui element
Future<bool> shouldShowCopied() async {
if (!Platform.isAndroid) return true;
try {
final deviceInfo = DeviceInfoPlugin();
final androidInfo = await deviceInfo.androidInfo;
final sdk = androidInfo.version.sdkInt;
return sdk < 33;
} catch (_) {
return true;
}
}
}