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: wait html element load #1730

Merged
merged 1 commit into from
May 13, 2024
Merged
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
37 changes: 22 additions & 15 deletions packages/stripe_web/lib/src/widgets/card_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class WebCardField extends StatefulWidget {
class WebStripeCardState extends State<WebCardField> with CardFieldContext {
CardEditController get controller => widget.controller;

late MutationObserver mutationObserver;

@override
void initState() {
// ignore: undefined_prefixed_name
Expand All @@ -62,6 +64,24 @@ class WebStripeCardState extends State<WebCardField> with CardFieldContext {
..id = 'card-element'
..style.border = 'none',
);
mutationObserver = MutationObserver((entries, observer) {
if (document.getElementById('card-element') != null) {
mutationObserver.disconnect();

updateCardDetails(
const CardFieldInputDetails(complete: false),
controller,
);
element = WebStripe.js
.elements(createElementOptions())
.createCard(createOptions())
..mount('#card-element')
..onBlur(requestBlur)
..onFocus(requestFocus)
..onChange(onCardChanged);
}
});
mutationObserver.observe(document, childList: true, subtree: true);
initStripe();
super.initState();
}
Expand All @@ -80,19 +100,6 @@ class WebStripeCardState extends State<WebCardField> with CardFieldContext {
dev.log('WARNING! Initial card data value has been ignored. \n'
'$kDebugPCIMessage');
}
ambiguate(WidgetsBinding.instance)?.addPostFrameCallback((timeStamp) {
updateCardDetails(
const CardFieldInputDetails(complete: false),
controller,
);
element = WebStripe.js
.elements(createElementOptions())
.createCard(createOptions())
..mount('#card-element')
..onBlur(requestBlur)
..onFocus(requestFocus)
..onChange(onCardChanged);
});
}
});
}
Expand Down Expand Up @@ -162,8 +169,7 @@ class WebStripeCardState extends State<WebCardField> with CardFieldContext {
return js.CardElementOptions(
style: {
'base': {
if (textColor != null)
'color': '${colorToCssString(textColor)}'
if (textColor != null) 'color': '${colorToCssString(textColor)}'
}
},
hidePostalCode: !widget.enablePostalCode,
Expand Down Expand Up @@ -191,6 +197,7 @@ class WebStripeCardState extends State<WebCardField> with CardFieldContext {
void dispose() {
detachController(controller);
element?.unmount();
mutationObserver.disconnect();
super.dispose();
}

Expand Down
Loading