init method

void init()

Implementation

void init() {
  widget.firstPageLoad = false;
  widget.paymentResult = BootpayPaymentResult.NONE;

  if (WebViewPlatform.instance is BTWebKitWebViewPlatform) {
    params = WebKitWebViewControllerCreationParams(
      allowsInlineMediaPlayback: true,
      mediaTypesRequiringUserAction: const <PlaybackMediaTypes>{},
    );
  } else {
    params = PlatformWebViewControllerCreationParams(
    );
  }

  // if (WebViewPlatform.instance is BTWebKitWebViewPlatform) {
  //   params = WebKitWebViewControllerCreationParams(
  //     allowsInlineMediaPlayback: true,
  //     mediaTypesRequiringUserAction: const <PlaybackMediaTypes>{},
  //   );
  // } else {
  //   params = PlatformWebViewControllerCreationParams(
  //   );
  // }

  final WebViewController controller = WebViewController.fromPlatformCreationParams(params);

  if(widget.userAgent != null) {
    controller.setUserAgent(widget.userAgent!);
  }

  print("user Agent: ${widget.userAgent}");

  String loadUrl = (widget.isWidget ?? false) ? widget.WIDGET_URL : widget.INAPP_URL;
  // print(loadUrl);

  controller
    ..setJavaScriptMode(JavaScriptMode.unrestricted)
    ..setBackgroundColor(const Color(0x00000000))
    ..setNavigationDelegate(
      NavigationDelegate(
        onProgress: (int progress) {
          debugPrint('WebView is loading (progress : $progress%)');
        },
        onPageStarted: (String url) {
          debugPrint('Page started loading: $url');
        },
        onPageFinished: (String url) async {
          debugPrint('Page finished loading: $url');

          //300ms 뒤에 실행
          // Future.delayed(Duration(milliseconds: 700), () {
          //   if(mounted && widget.firstPageLoad == false)
          //     setState(() {
          //       widget.firstPageLoad = true;
          //     });
          // });

          // if(mounted && widget.firstPageLoad == false)
          //   setState(() {
          //     widget.firstPageLoad = true;
          //   });

          (widget.isWidget ?? false) ? widget.loadWidgetScript(url) : loadPaymentScript(url);

        },
        // onNavi
        onWebResourceError: (WebResourceError error) {
          debugPrint('''
          Page resource error:
          code: ${error.errorCode}
          description: ${error.description}
          errorType: ${error.errorType}
          isForMainFrame: ${error.isForMainFrame}
                  ''');
          if(error.errorCode == 3) { // SSL 인증서 에러, update 유도
            if(error.description.contains("sslerror:")) {
              if (this.widget.onError != null) {
                this.widget.onError!(error.description);
              }
              debounceClose();
            }
          }
        },
        onNavigationRequest: (NavigationRequest request) {
          debugPrint('Page navigation request: ${request.url}');
          if(request.url.contains("https://nid.naver.com")) {
            widget._controller.runJavaScript("document.getElementById('back').remove()");
          }
          return NavigationDecision.navigate;
        },
        // onHttpError: (HttpResponseError error) {
        //   debugPrint('Error occurred on page: ${error.response?.statusCode}');
        // },
        onUrlChange: (UrlChange change) {
          debugPrint('url change to ${change.url}');
        },
        // onHttpAuthRequest: (HttpAuthRequest request) {
        //   debugPrint('auth request: ${request.host}');
        // },
        // Navigation

      ),
    )
    // ..setOnJavaScriptAlertDialog((JavaScriptAlertDialogRequest request) async {
    //   await _showAlert(context, request.message);
    // })
    // ..setOnJavaScriptConfirmDialog(
    //         (JavaScriptConfirmDialogRequest request) async {
    //       final bool result = await _showConfirm(context, request.message);
    //       return result;
    //     })
    // ..setOnJavaScriptTextInputDialog(
    //         (JavaScriptTextInputDialogRequest request) async {
    //       final String result =
    //       await _showTextInput(context, request.message, request.defaultText);
    //       return result;
    //     })
    ..addJavaScriptChannel(
      'Toaster',
      onMessageReceived: (JavaScriptMessage message) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text(message.message)),
        );
      },
    )
    ..addJavaScriptChannel(
      'BootpayCancel',
      onMessageReceived: onCancelJS,
    )
    ..addJavaScriptChannel(
      'BootpayError',
      onMessageReceived: onErrorJS,
    )
    ..addJavaScriptChannel(
      'BootpayClose',
      onMessageReceived: onCloseJS,
    )
    ..addJavaScriptChannel(
      'BootpayIssued',
      onMessageReceived: onIssuedJS,
    )
    ..addJavaScriptChannel(
      'BootpayConfirm',
      onMessageReceived: onConfirmJS,
    )
    ..addJavaScriptChannel(
      'BootpayDone',
      onMessageReceived: onDoneJS,
    )
    ..addJavaScriptChannel(
      'WidgetReady',
      onMessageReceived: onWidgetReadyJS,
    )
    ..addJavaScriptChannel(
      'WidgetResize',
      onMessageReceived: onWidgetResizeJS,
    )
    ..addJavaScriptChannel(
      'WidgetChangePayment',
      onMessageReceived: onWidgetChangePaymentJS,
    )
    ..addJavaScriptChannel(
      'WidgetChangeTerms',
      onMessageReceived: onWidgetChangeAgreeTermJS,
    )
    ..addJavaScriptChannel(
      'BootpayFlutterWebView',
      onMessageReceived: onRedirectJS,
    )
    ..loadRequest(Uri.parse(loadUrl));

  if (controller.platform is AndroidWebViewController) {
    AndroidWebViewController.enableDebugging(true);
    (controller.platform as AndroidWebViewController)
        .setMediaPlaybackRequiresUserGesture(false);
  }

  widget._controller = controller;
}