From 2cb408623fc29dfdb68f06443a8621660aba94e2 Mon Sep 17 00:00:00 2001 From: x544D Date: Mon, 23 Dec 2024 23:51:43 +0100 Subject: [PATCH] fix: prevent focus loop in PaymentElement widget Removed prints. --- .../lib/src/widgets/payment_element.dart | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/packages/stripe_web/lib/src/widgets/payment_element.dart b/packages/stripe_web/lib/src/widgets/payment_element.dart index 1ef4ea4b..383ef08d 100644 --- a/packages/stripe_web/lib/src/widgets/payment_element.dart +++ b/packages/stripe_web/lib/src/widgets/payment_element.dart @@ -169,15 +169,35 @@ class PaymentElementState extends State { 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(