how to navigate to another page when a facebook video is clicked in flutter inappwebview

59 views Asked by At

I have been trying to navigate to a material widget when a facebook video is clicked in my inappwebview flutter this is my code -

import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

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

class _MyHomePageState extends State<MyHomePage> {
  InAppWebViewController? webViewController;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('InAppWebView Button Click Example'),
      ),
      body: InAppWebView(
        initialUrlRequest:
            URLRequest(url: Uri.parse('https://www.facebook.com')),
        onWebViewCreated: (controller) {
          webViewController = controller;
        },
        onLoadStop: (controller, url) {
          // Inject JavaScript to listen for button clicks
          controller.evaluateJavascript(source: """
            document.querySelector('button').addEventListener('click', function() {
              window.flutter_inappwebview.callHandler('buttonClicked');
            });
          """);
        },
        onConsoleMessage: (controller, consoleMessage) {
          print("Console Message: ${consoleMessage.message}");
        },
        initialOptions: InAppWebViewGroupOptions(
          crossPlatform: InAppWebViewOptions(
            useShouldOverrideUrlLoading: true,
          ),
        ),
        shouldOverrideUrlLoading: (controller, navigationAction) async {
          final url = navigationAction.request.url.toString();
          if (url.startsWith("https://m.facebook.com/watch/")) {
            Navigator.of(context).push(MaterialPageRoute(builder: (context) {
              return const Material();
            }));
            return NavigationActionPolicy.CANCEL;
          }
          print("URL: $url");
          return NavigationActionPolicy.ALLOW;
        },
      ),
    );
  }
}

I have been trying to navigate to a material widget when a facebook video is clicked in my inappwebview flutter this is my code, but what i got instead is just the normal facebook navigation

0

There are 0 answers