How to get Current url in webview Futter Web

Viewed 48

How do i get current url in in Flutter web, navigationDelegate, onPageFinished, onPageStarted not seem to be working in flutter Web. In android and Ios its working fine but in Web i am unable to do it. Or if there are any way i can set a listener in controller of Webview.

In pubsec.yaml

webview_flutter: ^3.0.0
webview_flutter_web: ^0.1.0+4

and in my

void main() async {
 WidgetsFlutterBinding.ensureInitialized();
 await initGlobalControllers();
 setPathUrlStrategy();
 WebView.platform = WebWebViewPlatform();
 runApp(MyApp());
}

And in my WebScreen

import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter_web/webview_flutter_web.dart';
import 'package:webview_flutter/webview_flutter.dart';

 class WebScreen extends StatefulWidget {
  final String webUrl, pageTitle;
  WebScreen(this.webUrl, this.pageTitle);

  @override
   WebScreenState createState() => WebScreenState();
  }

class WebScreenState extends State<WebScreen> {
 final Completer<WebViewController> _controller =
 Completer<WebViewController>();

 @override
 Widget build(BuildContext context) {
 return Scaffold(
  appBar: AppBar(
    automaticallyImplyLeading: false,
    backgroundColor: primaryRed,
    leading: IconButton(
        onPressed: () {
          Navigator.pop(context);
        },
        icon: const ImageIcon(
          AssetImage(
            'assets/icons/icon_back.png',
          ),
          color: Colors.white,
          size: 20,
        )),
    title: Text(widget.pageTitle,
        style: GoogleFonts.roboto(
            textStyle: TextStyle(
                fontSize: getDeviceType(context) == deviceMobile
                    ? 15
                    : 19,
                color: Colors.white))),
  ),
  body: bodyWidget(),
 );
}

Widget bodyWidget() {
 return WebView(
      initialUrl: widget.webUrl,
      onWebViewCreated: (WebViewController webController) {
        printLog('pageStatus 01}');
        _controller.complete(webController);
      },
      navigationDelegate: (request) {
        printLog('pageStatus ${request.url}');
        return NavigationDecision.navigate;
      },
      onPageFinished: (url) {
        printLog('pageStatus finish, $url');
      },
      onPageStarted: (url){
        printLog('pageStatus start, $url');
      },
      onProgress: (progress){
        printLog('pageStatus $progress');
      },
    );
 }
1 Answers

I have the same issue with this. The URL's variable I define getting proper URL string but when it's pass to the initialUrl :, it doesn't work and once you pass the same URL's string which is stored in the variable it's working fine. Like This : "www.google.com"

Related