Skip to content

Commit

Permalink
fix: prevent focus loop in PaymentElement widget (#2022)
Browse files Browse the repository at this point in the history
Removed prints.

Co-authored-by: Rémon <[email protected]>
  • Loading branch information
x544D and remonh87 authored Jan 18, 2025
1 parent d8f8e3c commit 3da3f4b
Showing 1 changed file with 25 additions and 5 deletions.
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;
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

0 comments on commit 3da3f4b

Please sign in to comment.