build method

  1. @override
Widget build(
  1. BuildContext context
)
override

Describes the part of the user interface represented by this widget.

The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.

The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.

Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.

The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.

The implementation of this method must only depend on:

If a widget's build method is to depend on anything else, use a StatefulWidget instead.

See also:

  • StatelessWidget, which contains the discussion on performance considerations.

Implementation

@override
Widget build(BuildContext context) {
  final style = buttonStyle ??
      ElevatedButton.styleFrom(
        padding: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 8.0),
        shape: const StadiumBorder(),
        textStyle: const TextStyle(fontSize: 14.0),
        backgroundColor: Colors.white,
        foregroundColor: Colors.black,
        elevation: 3,
      );
  return SizedBox(
    width: lpeButtonWidth,
    child: ConstrainedBox(
      constraints: const BoxConstraints(maxHeight: 40),
      child: ElevatedButton(
        onPressed: () async {
          // On web, avoid invoking the Google Pay / PaymentRequest flow
          // when Safari / ApplePaySession is present but the Google Pay
          // JS SDK is not available. Safari will attempt to start an
          // Apple Pay session which requires merchant validation and
          // a secure context; to prevent the "insecure button" error
          // we show a friendly message instead of starting the flow.
          if (kIsWeb) {
            try {
              final hasApple =
                  js_util.hasProperty(html.window, 'ApplePaySession');
              final hasGoogle = js_util.hasProperty(html.window, 'google') &&
                  js_util.hasProperty(
                      js_util.getProperty(html.window, 'google'), 'payments');
              if (hasApple && !hasGoogle) {
                // Inform the user/developer that Google Pay isn't available
                // on this browser and Apple Pay requires merchant validation.
                await showDialog<void>(
                  context: context,
                  builder: (ctx) => AlertDialog(
                    title: const Text('Payment Unavailable'),
                    content: const Text(
                        'This browser supports Apple Pay but Google Pay is not available. Apple Pay requires merchant validation and a secure (HTTPS) origin. Use Chrome/Edge for Google Pay, or configure Apple Pay merchant validation for this domain.'),
                    actions: [
                      TextButton(
                          onPressed: () => Navigator.of(ctx).pop(),
                          child: const Text('OK'))
                    ],
                  ),
                );
                return;
              }
            } catch (_) {}
          }
          final margs = buildMerchantArgs(
            gatewayMerchantId: googleMerchantId,
            merchantName: merchantName,
            merchantInfo: merchantInfo,
            summaryItems: summaryItems,
            builder: merchantArgs,
          );
          final double amt = double.tryParse(amount) ?? 0.0;
          final int amountCents = (amt * 100).round();
          final args = <String, dynamic>{
            'method': 'google_pay',
            'apiKey': apiKey ?? '',
            'merchantArgs': margs ?? <String, dynamic>{},
            'amountCents': amountCents,
            'amount': amount,
            'currency': currency,
            'googleMerchantId': googleMerchantId,
          };
          final result = await LearmondNativePay.showNativePay(args);
          _handlePaymentResult(context, result, onResult);
        },
        style: style,
        child: const Center(
          child: Image(
            image: AssetImage('static/assets/GPay_Acceptance_Mark_800.png',
                package: 'lpe_sdk'),
            height: 20,
            fit: BoxFit.contain,
          ),
        ),
      ),
    ),
  );
}