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

Initialize platform web pay button when added to DOM #1978

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
63 changes: 37 additions & 26 deletions packages/stripe_web/lib/src/widgets/platform_pay_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,36 @@ class WebPlatformPayButton extends StatefulWidget {
}

class _WebPlatformPayButtonState extends State<WebPlatformPayButton> {
final web.HTMLDivElement _divElement = web.HTMLDivElement()
..id = 'platform-pay-button';

late final web.MutationObserver mutationObserver = web.MutationObserver(
((JSArray<web.MutationRecord> entries, web.MutationObserver observer) {
if (web.document.getElementById('platform-pay-button') != null) {
mutationObserver.disconnect();

PaymentRequest paymentRequest = WebStripe.js
.paymentRequest((widget.paymentRequestCreateOptions).toJS());

paymentRequest.canMakePayment().then((value) {
WebStripe.js.elements().createPaymentRequestButton(
JsPaymentRequestButtonElementCreateOptions(
paymentRequest: paymentRequest.js,
style: JsPaymentRequestButtonElementStyle(
paymentRequestButton: PaymentRequestButtonStyleOptions(
theme: theme(Theme.of(context).brightness),
type: type,
height: '${constraints.maxHeight}px',
))))
..on('click', (event) {
event.toDart['preventDefault']();
widget.onPressed();
})
..mount('#platform-pay-button'.toJS);
});
}
}.toJS));

BoxConstraints get constraints =>
widget.constraints ??
const BoxConstraints.expand(height: kPlatformPayButtonDefaultHeight);
Expand All @@ -40,8 +70,13 @@ class _WebPlatformPayButtonState extends State<WebPlatformPayButton> {
void initState() {
// ignore: undefined_prefixed_name
ui.platformViewRegistry.registerViewFactory('stripe_platform_pay_button',
(int viewId) => web.HTMLDivElement()..id = 'platform-pay-button');
_initButton();
(int viewId) => _divElement);

mutationObserver.observe(
web.document,
web.MutationObserverInit(childList: true, subtree: true),
);

super.initState();
}

Expand All @@ -58,30 +93,6 @@ class _WebPlatformPayButtonState extends State<WebPlatformPayButton> {
);
}

_initButton() {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
PaymentRequest paymentRequest = WebStripe.js
.paymentRequest((widget.paymentRequestCreateOptions).toJS());

paymentRequest.canMakePayment().then((value) {
WebStripe.js.elements().createPaymentRequestButton(
JsPaymentRequestButtonElementCreateOptions(
paymentRequest: paymentRequest.js,
style: JsPaymentRequestButtonElementStyle(
paymentRequestButton: PaymentRequestButtonStyleOptions(
theme: theme(Theme.of(context).brightness),
type: type,
height: '${constraints.maxHeight}px',
))))
..on('click', (event) {
event.toDart['preventDefault']();
widget.onPressed();
})
..mount('#platform-pay-button'.toJS);
});
});
}

PaymentRequestButtonType get type {
switch (widget.type) {
case PlatformButtonType.buy:
Expand Down
Loading