Skip to content

Commit 3da3f4b

Browse files
x544Dremonh87
andauthored
fix: prevent focus loop in PaymentElement widget (#2022)
Removed prints. Co-authored-by: Rémon <[email protected]>
1 parent d8f8e3c commit 3da3f4b

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

packages/stripe_web/lib/src/widgets/payment_element.dart

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,35 @@ class PaymentElementState extends State<PaymentElement> {
169169
final FocusNode _focusNode = FocusNode(debugLabel: 'CardField');
170170
FocusNode get _effectiveNode => widget.focusNode ?? _focusNode;
171171

172+
bool _isManuallyFocusing = false; // Track manual focus/blur actions
173+
bool _isCurrentlyFocused = false; // Track current focus state
174+
172175
@override
173176
Widget build(BuildContext context) {
174177
return Focus(
175178
focusNode: _effectiveNode,
176-
onFocusChange: (focus) {
177-
/* if (focus)
178-
element?.focus();
179-
else
180-
element?.blur(); */
179+
onFocusChange: (focus) {
180+
// Prevent feedback loop from manual focus/blur actions
181+
if (_isManuallyFocusing) {
182+
_isManuallyFocusing = false;
183+
return;
184+
}
185+
186+
// Check if the focus state has actually changed
187+
if (_isCurrentlyFocused == focus) {
188+
return; // No state change, do nothing
189+
}
190+
191+
// Update the current focus state
192+
_isCurrentlyFocused = focus;
193+
194+
if (focus) {
195+
_isManuallyFocusing = true;
196+
element?.focus();
197+
} else {
198+
_isManuallyFocusing = true;
199+
element?.blur();
200+
}
181201
},
182202
child: ConstrainedBox(
183203
constraints: BoxConstraints(

0 commit comments

Comments
 (0)