How to catch redirect in WebView and redirect to native browser?

Viewed 97

Now I'm opening the Privacy Policy page using a WebView. It has links and when you click in the same tab in the application, a browser page opens. I need a browser page to open when clicking on a link. And the user was informed that a redirect was taking place. How can I do that?

class PrivacyPolicyPage extends StatefulWidget {
  const PrivacyPolicyPage({super.key});
  static const routeName = '/privacy_policy';
  @override
  State<PrivacyPolicyPage> createState() => _PrivacyPolicyPageState();
}
class _PrivacyPolicyPageState extends State<PrivacyPolicyPage> {
  final _filePath = Assets.assetsPrivacyPolicy;
  late WebViewController _webViewController;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Privacy Policy'),
        centerTitle: true,
      ),
      body: WebView(
        initialUrl: '',
        javascriptMode: JavascriptMode.unrestricted,
        onWebViewCreated: _onWebViewCreated,
      ),
    );
  }
  void _onWebViewCreated(WebViewController webViewController) {
    _webViewController = webViewController;
    _loadHtmlFromAssets();
  }
  Future<void> _loadHtmlFromAssets() async {
    final fileHtmlContents = await rootBundle.loadString(_filePath);
    await _webViewController.loadUrl(Uri.dataFromString(
      fileHtmlContents,
      mimeType: 'text/html',
      encoding: Encoding.getByName('utf-8'),
    ).toString());
  }
}

0 Answers
Related