Force in-app webview instead of browsing external app for youtube video with url_launcher

86 views Asked by At

In my app , I want to open in-app web view instead of browsing external youtube link using url launcher. What should I do? Or Should I use in-app web view plugin instead?

This is my code.

if (await canLaunchUrl(uri)) {
      try {
        await launchUrl(uri, mode: LaunchMode.inAppWebView);
        return true;
      } on PlatformException catch (e) {
        // cannot launch url
        return false;
      }
    } else {
      return false;
    }

1

There are 1 answers

0
Enilson Filho On

you can open web views within your application using webview_flutter. Check it out on pub.dev -> https://pub.dev/packages/webview_flutter.

Here's an example:

class WebViewScreen extends StatelessWidget {
  final String url;

  WebViewScreen({required this.url});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("WebView"),
      ),
      body: WebView(
        initialUrl: url,
        javascriptMode: JavascriptMode.unrestricted,
      ),
    );
  }
}

To implement this in your code:

if (await canLaunchUrl(uri)) {
  try {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => WebViewScreen(url: uri),
      ),
    );
    return true;
  } on PlatformException catch (e) {
    // cannot launch url
    return false;
  }
} else {
  return false;
}

Supporting documentation at https://codelabs.developers.google.com/codelabs/flutter-webview

Note: If your main focus is only video playback, I recommend using the "youtube_player_flutter" library, pub.dev -> https://pub.dev/packages/youtube_player_flutter

I hope this can help you.