android – Flutter WebView needing double click on to reinitialize

android – Flutter WebView needing double click on to reinitialize


On my flutter app I’ve a web page that hundreds an internet site utilizing WebView. The Url for the WebView is being acquired by an API which is saved inside a variable referred to as pageUrl. When a person clicks on the BottomNavBar to go to the mentioned display, the WebView will reload (or reinitialize, unsure about the correct terminology right here). It’s working nice.

Nonetheless, the problem I’m having is that, when the person goes to that web page, they should click on the button once more for the web page to reinitialize. I would like it to be such that every time the person clicks on the button for the web page, it simply reinitializes the web page. I’ve hooked up my code under.

navbar_widget.dart

class BottomNavbar extends StatelessWidget {
  const BottomNavbar({tremendous.key});

  @override
  Widget construct(BuildContext context) {
    return GetBuilder<BottomNavbarController>(
      builder: (controller) => WillPopScope(
        onWillPop: () async {
          if (controller.tabIndex != 0) {
            controller.updateIndex(0);
            return false;
          } else {
            bool closeApp = await showDialog(
              context: context,
              builder: (context) => AlertDialog(
                 // Alert Dialog
                ],
              ),
            );
            return closeApp;
          }
        },
        youngster: Scaffold(
          extendBody: true,
          appBar: AppbarWidget(tabIndex: controller.tabIndex),
          bottomNavigationBar: CurvedNavigationBar(

            index: controller.tabIndex,
            onTap: (val) {
              controller.updateIndex(val);
            },
            gadgets: const [
              Icon(Icons.home_outlined, color: Colors.white),
              Icon(Icons.maps_home_work_outlined, color: Colors.white),
              Icon(Icons.access_time, color: Colors.white),
            ],
          ),
          physique: Padding(
            padding: EdgeInsets.solely(
                high: MediaQuery.of(context).measurement.peak * 0.018),
            youngster: SafeArea(
              backside: false,
              youngster: IndexedStack(
                index: controller.tabIndex,
                kids: const [
                  PageOne(),
                  PageTwo(),
                  PageThree(),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

page2_screen.dart

class PageTwo extends StatefulWidget {
  const PageTwo({tremendous.key});

  @override
  State<PageTwo> createState() => _PageTwoState();
}

class _PageTwoState extends State<PageTwo> {
  late WebViewController webViewController;
  String pageUrl="";

  @override
  void initState() {
    tremendous.initState();
    initializeWebView();
    fetchData();
  }

  void initializeWebView() {
    webViewController = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setBackgroundColor(const Coloration(0x00000000))
      ..setNavigationDelegate(
        NavigationDelegate(
          onPageFinished: (url) => _injectJavaScript(),
        ),
      );
  }

  Future<void> fetchData() async {
    // Getting URL for the webview
  }

  void _injectJavaScript() {
    String jsCode = """
    // Injecting JS
  """;
    webViewController.runJavaScript(jsCode);
  }

  @override
  Widget construct(BuildContext context) {
    return GetBuilder<BottomNavbarController>(
      builder: (bottomNavbarController) {
        if (bottomNavbarController.refreshMenuScreen) {
          initializeWebView();
          fetchData();
          bottomNavbarController.refreshMenuScreen = false;
        }

        return Scaffold(
          backgroundColor: const Coloration(0xFF000000),
          physique: Padding(
            padding: EdgeInsets.solely(
                backside: MediaQuery.of(context).measurement.peak * 0.01),
            youngster: SafeArea(
              youngster: WebViewWidget(
                controller: webViewController,
              ),
            ),
          ),
        );
      },
    );
  }
}

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 
rooshohttps://www.roosho.com
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Latest Articles

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog.