BrowserController constructor

BrowserController({
  1. String uriString = '',
  2. String? userAgent,
  3. bool isZoomEnabled = true,
  4. WebViewController? webViewController,
  5. NavigationDelegate? webViewNavigationDelegate,
})

Constructs a new browser controller.

Optional parameter uriString is the initial URI.

Optional parameter userAgent is value for the HTTP header "User-Agent". The default is null, which means that no user agent should be sent.

Optional parameter isZoomEnabled specifies whether zooming into the content should allowed.

Optional parameter webViewController is the underlying WebViewController.

Implementation

BrowserController({
  String uriString = '',
  String? userAgent,
  bool isZoomEnabled = true,
  WebViewController? webViewController,
  this.webViewNavigationDelegate,
})  : _uriString = uriString,
      _userAgent = userAgent,
      _webViewController = webViewController ?? WebViewController() {
  _maybeClearState();
  try {
    _webViewController.setNavigationDelegate(NavigationDelegate(
      onNavigationRequest: (request) async {
        await _maybeClearState();
        final result = await webViewNavigationDelegate?.onNavigationRequest
            ?.call(request);
        if (result != null && result == NavigationDecision.prevent) {
          return result;
        }
        final policy = this.policy;
        if (policy != null && request.isMainFrame) {
          final parsedUri = Uri.tryParse(request.url);
          if (parsedUri == null) {
            return NavigationDecision.navigate;
          }
          if (!policy.isUriAllowed(parsedUri)) {
            return NavigationDecision.prevent;
          }
        }
        return NavigationDecision.navigate;
      },
      onPageStarted: (uri) {
        _uriString = uri;
        _isLoading = true;

        // By default, assume this was a navigation to a new page.
        _canGoBack = true;
        _canGoForward = false;

        if (!kIsWeb) {
          try {
            if (_isAndroid) {
              _webViewController.canGoBack().then((value) {
                if (value != _canGoBack) {
                  _canGoBack = value;
                  notifyListeners();
                }
              }, onError: (error) {});
            }
            _webViewController.canGoForward().then((value) {
              if (value != _canGoForward) {
                _canGoForward = value;
                notifyListeners();
              }
            }, onError: (error) {});
          } catch (error) {
            // Ignore error
          }
        }

        notifyListeners();

        webViewNavigationDelegate?.onPageStarted?.call(uri);
      },
      onProgress: (progress) {
        notifyListeners();
        webViewNavigationDelegate?.onProgress?.call(progress);
      },
      onPageFinished: (uri) {
        _uriString = uri;
        _isLoading = false;
        notifyListeners();
        webViewNavigationDelegate?.onPageFinished?.call(uri);
      },
      onWebResourceError: (error) {
        if (error.isForMainFrame ?? true) {
          _error = error;
          _isLoading = false;
          notifyListeners();
        }
        webViewNavigationDelegate?.onWebResourceError?.call(error);
      },
    ));
  } catch (error) {
    // Ignore
  }
  try {
    _webViewController.setJavaScriptMode(
      JavaScriptMode.unrestricted,
    );
  } on UnimplementedError {
    // Ignore errors in browser
  }
  try {
    _isZoomEnabled = isZoomEnabled;
    _webViewController.enableZoom(isZoomEnabled);
  } catch (error) {
    // Ignore error
  }
}