Skip to content

Commit

Permalink
clean up payment element, use flutter best practices, enable scrolling (
Browse files Browse the repository at this point in the history
#1620)

* clean up payment element, use flutter best practices, enable scrolling

* disable horizontal scroll

* readd comment with typo fix

---------

Co-authored-by: jonasbark <[email protected]>
Co-authored-by: Rémon <[email protected]>
  • Loading branch information
3 people authored Mar 2, 2024
1 parent 8420abb commit 9667858
Showing 1 changed file with 52 additions and 49 deletions.
101 changes: 52 additions & 49 deletions packages/stripe_web/lib/src/widgets/payment_element.dart
Original file line number Diff line number Diff line change
@@ -1,64 +1,55 @@
import 'dart:html';

import 'dart:ui' as ui;

import 'dart:ui_web' as ui;
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

import '../../flutter_stripe_web.dart';
import 'package:stripe_js/stripe_js.dart' as js;
import 'package:stripe_js/stripe_api.dart' as js;
import 'package:stripe_js/stripe_js.dart' as js;

export 'package:stripe_js/stripe_api.dart' show PaymentElementLayout;

typedef PaymentElementTheme = js.ElementTheme;

class PaymentElement extends StatefulWidget {
PaymentElement({
required this.onCardChanged,
Key? key,
this.onFocus,
this.style,
this.placeholder,
this.enablePostalCode = false,
double? width,
double? height = kCardFieldDefaultHeight,
BoxConstraints? constraints,
this.focusNode,
required this.clientSecret,
this.autofocus = false,
this.layout = PaymentElementLayout.accordion,
this.appearance,
}) : assert(constraints == null || constraints.debugAssertIsValid()),
constraints = (width != null || height != null)
? constraints?.tighten(width: width, height: height) ??
BoxConstraints.tightFor(width: width, height: height)
: constraints,
super(key: key);

final BoxConstraints? constraints;
final CardFocusCallback? onFocus;
final String clientSecret;
final CardChangedCallback onCardChanged;
final double? width;
final double? height;
final CardStyle? style;
final CardPlaceholder? placeholder;
final bool enablePostalCode;
final FocusNode? focusNode;
final bool autofocus;
final FocusNode? focusNode;
final CardFocusCallback? onFocus;
final CardChangedCallback onCardChanged;
final PaymentElementLayout layout;
final js.ElementAppearance? appearance;

PaymentElement({
super.key,
required this.clientSecret,
this.width,
this.height,
this.style,
this.placeholder,
this.enablePostalCode = false,
this.autofocus = false,
this.focusNode,
this.onFocus,
required this.onCardChanged,
this.layout = PaymentElementLayout.accordion,
this.appearance,
});

@override
PaymentElementState createState() => PaymentElementState();
State<PaymentElement> createState() => PaymentElementState();
}

class PaymentElementState extends State<PaymentElement> {
// 2 is the first size generated by the iframe. O will not worrk
double height = 2;
late final DivElement _divElement = DivElement()
..id = 'payment-element'
..style.border = 'none'
..style.width = '100%'
..style.height = '$height';
DivElement _divElement = DivElement();
// 2 is the first size generated by the iframe, O will not work.
double height = 2.0;

late MutationObserver? mutationObserver =
MutationObserver((entries, observer) {
if (document.getElementById('payment-element') != null) {
Expand All @@ -82,23 +73,33 @@ class PaymentElementState extends State<PaymentElement> {
});

late final resizeObserver = ResizeObserver((entries, observer) {
for (final entry in entries) {
if (entry.contentRect is DomRectReadOnly) {
final cr = entry.contentRect;
final height = cr.height;
setState(() {
this.height = height;
_divElement.style.height = '${height}px';
});
if (widget.height == null) {
for (final entry in entries) {
if (entry.contentRect is DomRectReadOnly) {
final cr = entry.contentRect;
setState(() {
height = cr.height;
_divElement.style.height = '${height}px';
});
}
}
}
});

@override
void initState() {
height = widget.height ?? height;

_divElement = DivElement()
..id = 'payment-element'
..style.border = 'none'
..style.width = '100%'
..style.height = '${height}'
..style.overflow = 'scroll'
..style.overflowX = 'hidden';

elements = WebStripe.js.elements(createOptions());
mutationObserver!.observe(document, childList: true, subtree: true);
// ignore: undefined_prefixed_name
ui.platformViewRegistry.registerViewFactory(
'stripe_payment_element',
(int viewId) => _divElement,
Expand Down Expand Up @@ -144,8 +145,10 @@ class PaymentElementState extends State<PaymentElement> {
element?.blur(); */
},
child: ConstrainedBox(
constraints:
BoxConstraints(maxWidth: double.infinity, maxHeight: height),
constraints: BoxConstraints(
maxWidth: double.infinity,
maxHeight: height,
),
child: const HtmlElementView(viewType: 'stripe_payment_element'),
),
);
Expand Down

0 comments on commit 9667858

Please sign in to comment.