VolkanUstekidag
VolkanUstekidag

Reputation: 375

Flutter detects touch as touch while scrolling in webview

I have a webview widget that displays a YouTube video in Flutter. The video opens without any problems, but when the page is scrolled by touching this widget, the video starts at the first touch. How can I solve this? What exactly is the problem with this? How can I solve this? What exactly is the problem with this? How can I solve this? What exactly is the problem with this?

-----------
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:webview_flutter/webview_flutter.dart';

class StoryVideoWebView extends StatefulWidget {
  final String? videoUrl;

  const StoryVideoWebView({super.key, this.videoUrl});

  @override
  State<StoryVideoWebView> createState() => _StoryVideoWebViewState();
}

class _StoryVideoWebViewState extends State<StoryVideoWebView> {
  WebViewController webViewController = WebViewController();

  initVideo() {
    if (widget.videoUrl != null && isYouTubeOrVimeoUrl(widget.videoUrl!)) {
      webViewController
        ..setBackgroundColor(Colors.transparent)
        ..setJavaScriptMode(JavaScriptMode.unrestricted)
        ..setNavigationDelegate(NavigationDelegate(
          onNavigationRequest: (request) {
            if (request.url == extractVideoIdFromLink(widget.videoUrl!)) {
              return NavigationDecision.navigate;
            }
            return NavigationDecision.prevent;
          },
        ))
        ..loadRequest(
          Uri.parse(extractVideoIdFromLink(widget.videoUrl!)),
        );
    }
  }

  bool isYouTubeOrVimeoUrl(String url) {
    return url.contains("youtube.com") ||
        url.contains("youtu.be") ||
        url.contains("vimeo.com");
  }

  bool isYouTube(String url) {
    return url.contains("vimeo") ? false : true;
  }

  String extractVimeoVideoId(String vimeoLink) {
    RegExp? regExp = RegExp(r'vimeo\.com/(\d+)');
    Match? match = regExp.firstMatch(vimeoLink);
    String videoId = "";

    if (match != null && match.groupCount >= 1) {
      videoId = match.group(1) ?? "";
    }
    return "https://player.vimeo.com/video/$videoId";
  }

  String extractYoutubeVideoId(String youtubeLink) {
    RegExp? regExp1 = RegExp(r'[?&]v=([^&]+)');
    RegExp? regExp2 = RegExp(r'youtu\.be/([^?]+)');
    RegExp? regExp3 = RegExp(r'/embed/([^?]+)');
    RegExp regExp4 = RegExp(
        r"^https:\/\/www\.youtube\.com\/watch\?v=([a-zA-Z0-9_-]+)&list=[a-zA-Z0-9_-]+");

    String videoId = "";

    if (regExp1.hasMatch(youtubeLink)) {
      Match? match = regExp1.firstMatch(youtubeLink);
      if (match != null && match.groupCount >= 1) {
        videoId = match.group(1) ?? "";
      }
    } else if (regExp2.hasMatch(youtubeLink)) {
      Match? match = regExp2.firstMatch(youtubeLink);
      if (match != null && match.groupCount >= 1) {
        videoId = match.group(1) ?? "";
      }
    } else if (regExp3.hasMatch(youtubeLink)) {
      Match? match = regExp3.firstMatch(youtubeLink);
      if (match != null && match.groupCount >= 1) {
        videoId = match.group(1) ?? "";
      }
    } else if (regExp4.hasMatch(youtubeLink)) {
      Match? match = regExp4.firstMatch(youtubeLink);
      if (match != null && match.groupCount >= 1) {
        videoId = match.group(1) ?? "";
      }
    }
    return "https://www.youtube.com/embed/$videoId";
  }

  String extractVideoIdFromLink(String videoLink) {
    if (videoLink.contains("vimeo")) {
      return extractVimeoVideoId(videoLink);
    } else if (videoLink.contains("youtube.com") ||
        videoLink.contains("youtu.be")) {
      return extractYoutubeVideoId(videoLink);
    }
    return "";
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    initVideo();
  }

  @override
  Widget build(BuildContext context) {
    if (widget.videoUrl != null && isYouTubeOrVimeoUrl(widget.videoUrl!)) {
      return SizedBox(
        height: Get.height * 0.30,
        width: double.infinity,
        child: ClipRRect(
          borderRadius: BorderRadius.circular(6),
          child: WebViewWidget(
            controller: webViewController,
          ),
        ),
      );
    } else {
      return Container();
    }
  }
}

Upvotes: 0

Views: 39

Answers (0)

Related Questions