Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent focus loop in PaymentElement widget #2022

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions packages/stripe_web/lib/src/widgets/payment_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,35 @@ class PaymentElementState extends State<PaymentElement> {
final FocusNode _focusNode = FocusNode(debugLabel: 'CardField');
FocusNode get _effectiveNode => widget.focusNode ?? _focusNode;

bool _isManuallyFocusing = false; // Track manual focus/blur actions
bool _isCurrentlyFocused = false; // Track current focus state

@override
Widget build(BuildContext context) {
return Focus(
focusNode: _effectiveNode,
onFocusChange: (focus) {
/* if (focus)
element?.focus();
else
element?.blur(); */
onFocusChange: (focus) {
// Prevent feedback loop from manual focus/blur actions
if (_isManuallyFocusing) {
_isManuallyFocusing = false;
x544D marked this conversation as resolved.
Show resolved Hide resolved
return;
}

// Check if the focus state has actually changed
if (_isCurrentlyFocused == focus) {
return; // No state change, do nothing
}

// Update the current focus state
_isCurrentlyFocused = focus;

if (focus) {
_isManuallyFocusing = true;
element?.focus();
} else {
_isManuallyFocusing = true;
element?.blur();
}
},
child: ConstrainedBox(
constraints: BoxConstraints(
Expand Down