Home > database >  How to make dynamic initial url on webview
How to make dynamic initial url on webview

Time:10-28

Here is my code... I need to make initial URL dynamic so that I can use it for many times. how to make classes parameterized? so that I can call it like a function? I'm trying to make it reusable.Is there any way how to make it optional or parameterized?

  class defaultWebpage extends StatefulWidget {
  const defaultWebpage({Key? key}) : super(key: key);

  @override
  State<defaultWebpage> createState() => _defaultWebpageState();
}

class _defaultWebpageState extends State<defaultWebpage> {
  int position = 1;
  @override
  WebViewController? controller;
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        if (await controller!.canGoBack()) {
          controller?.goBack();
          return false;
        } else {
          return true;
        }
      },
      child: IndexedStack(
        index: position,
        children: [
          WebView(
              initialUrl: "https://stackoverflow.com/",
              javascriptMode: JavascriptMode.unrestricted,
              onPageStarted: (value) {
                setState(() {
                  position = 1;
                });
              },
              onPageFinished: (value) {
                setState(() {
                  position = 0;

                  controller?.evaluateJavascript(
                      "document.getElementsByClassName('navbar-wrapper')[0].style.display='none'");

                  controller?.evaluateJavascript(
                      "document.getElementsByClassName('footer')[0].style.display='none'");
                });
              },
              onWebViewCreated: (controller) {
                this.controller = controller;
              }),
          Container(
            child: Center(
              child: shimmer_effect(),
            ),
          )
        ],
      ),
    );
  }
}

CodePudding user response:

defaultWebpage(url:yourDynamicUrl)
class defaultWebpage extends StatefulWidget {
  defaultWebpage({Key? key,required this.url}) : super(key: key);

  String url;
  @override
  State<defaultWebpage> createState() => _defaultWebpageState();
}

class _defaultWebpageState extends State<defaultWebpage> {
  int position = 1;
  @override
  WebViewController? controller;
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        if (await controller!.canGoBack()) {
          controller?.goBack();
          return false;
        } else {
          return true;
        }
      },
      child: IndexedStack(
        index: position,
        children: [
          WebView(
              initialUrl: widget.url,
              javascriptMode: JavascriptMode.unrestricted,
              onPageStarted: (value) {
                setState(() {
                  position = 1;
                });
              },
              onPageFinished: (value) {
                setState(() {
                  position = 0;

                  controller?.evaluateJavascript(
                      "document.getElementsByClassName('navbar-wrapper')[0].style.display='none'");

                  controller?.evaluateJavascript(
                      "document.getElementsByClassName('footer')[0].style.display='none'");
                });
              },
              onWebViewCreated: (controller) {
                this.controller = controller;
              }),
          Container(
            child: Center(
              child: shimmer_effect(),
            ),
          )
        ],
      ),
    );
  }
}
  • Related