initState method

  1. @override
void initState()
override

Called when this object is inserted into the tree.

The framework will call this method exactly once for each State object it creates.

Override this method to perform initialization that depends on the location at which this object was inserted into the tree (i.e., context) or on the widget used to configure this object (i.e., widget).

If a State's build method depends on an object that can itself change state, for example a ChangeNotifier or Stream, or some other object to which one can subscribe to receive notifications, then be sure to subscribe and unsubscribe properly in initState, didUpdateWidget, and dispose:

  • In initState, subscribe to the object.
  • In didUpdateWidget unsubscribe from the old object and subscribe to the new one if the updated widget configuration requires replacing the object.
  • In dispose, unsubscribe from the object.

You should not use BuildContext.dependOnInheritedWidgetOfExactType from this method. However, didChangeDependencies will be called immediately following this method, and BuildContext.dependOnInheritedWidgetOfExactType can be used there.

Implementations of this method should start with a call to the inherited method, as in super.initState().

Implementation

@override
void initState() {
  // TODO: implement initState
  super.initState();

  if (Platform.isAndroid){
    Helper().shouldHybridComposition().then((value) => shouldHybrid);
  }

  setState(() {

  });


  if (widget.isWallet != null && widget.isWallet!) {
    // Helper().pushSellonAnalytic(context, TypeClass.Wallet, type: "Main");
  }

  PlatformWebViewControllerCreationParams params;
  params = const PlatformWebViewControllerCreationParams();
  final WebViewController controller = WebViewController.fromPlatformCreationParams(params);
  controller
    ..setJavaScriptMode(JavaScriptMode.unrestricted)
    ..setBackgroundColor(const Color(0x00000000))
    ..enableZoom(!widget.isWallet!)
    ..setNavigationDelegate(
      NavigationDelegate(
        onProgress: (int progress) {
          debugPrint('WebView is loading (progress : $progress%)');
          if (widget.isWallet!) {
            if(mounted){
              setState(() {
                loadingPercentage = progress;
              });
            }
          }
        },
        onPageStarted: (String url) {
          debugPrint('Page started loading: $url');
        },
        onPageFinished: widget.onPageFinish != null ? widget.onPageFinish : (String s) {
          if (widget.isCheckList != null && widget.isCheckList!) {
            if (s.contains("successredirect") ||
                s.contains("Berhasil") ||
                s.contains("success")) {
              SellonRouter.pop(context);
            }
          }else{
            if (widget.isWallet != null && widget.isWallet!) {
              if (!iscreatedUrl) {
                loadingPercentage = 100;
                iscreatedUrl = true;
                if (mounted){
                  setState(() {});
                }
              }
            }
            if (Config.openApi) {
              print("s url ${s}");
            }
            if (s.contains("successredirect") ||
                s.contains("Berhasil") ||
                s.contains("success") ||
                s.contains("closeUrl")) {
              if (s.contains("closeUrl/500")) {
                Helper().modalSellOnKebalik(context,
                    message:
                    "Connection is not good temporarily due to lots of traffic. Please try again.",
                    alone: true, onOke: () {
                      SellonRouter.pop(context);
                      SellonRouter.pop(context);
                    });
              } else {
                SellonRouter.pop(context);
              }
            }
            // if (s.contains("status=openaddress")) {
            //   url = s;
            //   SellonRouter.push(
            //       context,
            //       AddressListScreen(
            //         isPicker: true,
            //       )).then((value) {
            //     if (Device.get().isIos) {
            //       controllerGlobal.goBack();
            //     }
            //     controllerGlobal.loadRequest(Uri.parse(url.replaceAll("status=openaddress", "status=selectedaddress")));
            //   });
            // }
            if (s.contains("walletinfo")) {
              walletinfo = true;
              setState(() {});
            } else {
              if (walletinfo) {
                walletinfo = false;
                setState(() {});
              }
            }

            if (s.contains("reload")) {
              Navigator.pop(context, "reload");
            }
            if (widget.isWallet!) {
              if (s.contains("openlink")) {
                String a = s
                    .substring(s.indexOf('openlink/'))
                    .replaceAll('openlink/', '');
                convert.Codec<String, String> stringToBase64 =
                convert.utf8.fuse(convert.base64);
                String decoded = stringToBase64.decode(a);
                String b = Uri.decodeFull(decoded);
                Helper().launcherUrl(
                    url: b, openSafari: true, openbrowser: true);
              } else if (s.contains("referersln")) {
                Helper().shareEveryThing(
                    "${Config().baseUrlShare}share${Config.nationCode}");
              } else if (s.contains("today_mission")) {
                if (widget.isActivity!) {
                  SellonRouter.pop(context);
                } else {
                  // SellonRouter.push(
                  //     context, ActivityDashboardScreen());
                }
              } else if (s.contains("gotError")) {
                List a = s.split("/");
                String error = a.last.toString();
                String decode = Uri.decodeFull(error);

                Helper().modalSellOnKebalik(
                  context,
                  message: decode,
                  onOke: () {
                    SellonRouter.pop(context);
                    SellonRouter.pop(context);
                  },
                  alone: true,
                );
              } else if (s.contains("main_banner")) {
                // SellonRouter.makeFirst(context, HomeScreen());
              }
            }
          }
        },
        onWebResourceError: (WebResourceError error) {
          SellonRouter.pop(context);
//             debugPrint('''
// Page resource error:
//   code: ${error.errorCode}
//   description: ${error.description}
//   errorType: ${error.errorType}
//   isForMainFrame: ${error.isForMainFrame}
//           ''');
        },
        onNavigationRequest: (NavigationRequest request) {
          if (request.url.startsWith('https://www.youtube.com/')) {
            debugPrint('blocking navigation to ${request.url}');
            return NavigationDecision.prevent;
          }
          debugPrint('allowing navigation to ${request.url}');
          return NavigationDecision.navigate;
        },
        onUrlChange: (UrlChange change) {
          debugPrint('url change to ${change.url}');
        },
      ),
    )
    ..addJavaScriptChannel(
      'Toaster',
      onMessageReceived: (JavaScriptMessage message) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text(message.message)),
        );
      },
    )
    ..loadRequest(Uri.parse(widget.url!));

  controllerGlobal = controller;
}