-
-
Save giordanna/938ee0c301a19a0068d03ca29ace2300 to your computer and use it in GitHub Desktop.
vimeo-player-ios-issue-flutter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Add the following dependencies on pubspec.yaml: | |
- url_launcher: ^6.0.20 | |
- flutter_inappwebview: ^6.0.0-beta.24+1 | |
*/ | |
import 'dart:async'; | |
import 'dart:io'; | |
import 'package:flutter/foundation.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_inappwebview/flutter_inappwebview.dart'; | |
import 'package:url_launcher/url_launcher.dart'; | |
const defaultUrl = "https://giordanna.github.io/vimeo-player-ios-issue-svelte/"; | |
Future main() async { | |
WidgetsFlutterBinding.ensureInitialized(); | |
FlutterError.onError = (FlutterErrorDetails details) { | |
FlutterError.presentError(details); | |
if (kReleaseMode) { | |
exit(1); | |
} | |
}; | |
runApp( | |
MaterialApp(home: WebApp(), debugShowCheckedModeBanner: false), | |
); | |
} | |
class WebApp extends StatefulWidget { | |
@override | |
_WebAppState createState() => new _WebAppState(); | |
} | |
class _WebAppState extends State<WebApp> { | |
InAppWebViewController? webViewController; | |
InAppWebViewSettings options = InAppWebViewSettings( | |
userAgent: | |
"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Mobile Safari/537.36", | |
useShouldOverrideUrlLoading: true, | |
supportZoom: true, | |
horizontalScrollBarEnabled: true, | |
cacheEnabled: true, | |
clearCache: false, | |
useOnDownloadStart: false, | |
useOnLoadResource: true, | |
useHybridComposition: true, | |
disableLongPressContextMenuOnLinks: true, | |
limitsNavigationsToAppBoundDomains: true, | |
supportMultipleWindows: true, | |
allowUniversalAccessFromFileURLs: true, | |
allowFileAccessFromFileURLs: true, | |
loadWithOverviewMode: true, | |
disableDefaultErrorPage: true, | |
isFraudulentWebsiteWarningEnabled: false, | |
isPagingEnabled: true, | |
disallowOverScroll: true, | |
isFindInteractionEnabled: false, | |
disableVerticalScroll: Platform.isIOS, | |
disableHorizontalScroll: Platform.isIOS, | |
javaScriptEnabled: true, | |
allowsInlineMediaPlayback: true, | |
allowsAirPlayForMediaPlayback: true, | |
allowsPictureInPictureMediaPlayback: true, | |
allowsBackForwardNavigationGestures: true, | |
allowsLinkPreview: true); | |
@override | |
void initState() { | |
super.initState(); | |
} | |
@override | |
void dispose() { | |
webViewController = null; | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return WillPopScope( | |
onWillPop: () async { | |
// detect Android back button click | |
final controller = webViewController; | |
if (controller != null) { | |
if (await controller.canGoBack()) { | |
controller.goBack(); | |
return false; | |
} | |
} | |
return true; | |
}, | |
child: Scaffold( | |
resizeToAvoidBottomInset: true, | |
appBar: AppBar( | |
elevation: 0, | |
toolbarHeight: 0, | |
), | |
body: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Expanded( | |
child: InAppWebView( | |
initialUrlRequest: URLRequest(url: WebUri(defaultUrl)), | |
initialSettings: options, | |
onWebViewCreated: (controller) { | |
webViewController = controller; | |
}, | |
shouldOverrideUrlLoading: shouldOverrideUrlLoadingMethod, | |
onCreateWindow: shouldOverrideCreateWindowMethod, | |
onConsoleMessage: (controller, consoleMessage) { | |
print(consoleMessage); | |
}, | |
)) | |
]))); | |
} | |
} | |
Future<NavigationActionPolicy?> shouldOverrideUrlLoadingMethod( | |
InAppWebViewController controller, | |
NavigationAction navigationAction) async { | |
var uri = navigationAction.request.url!; | |
final requestUrl = navigationAction.request.url.toString(); | |
if (requestUrl.contains(defaultUrl) || | |
requestUrl.contains("player.vimeo.com/video/")) { | |
return NavigationActionPolicy.ALLOW; | |
} | |
if (!["http", "https", "file", "chrome", "data", "javascript", "about"] | |
.contains(uri.scheme) || | |
["http", "https"].contains(uri.scheme)) { | |
if (await canLaunchUrl(uri)) { | |
// Launch the App | |
await launchUrl(uri, mode: LaunchMode.externalApplication); | |
return NavigationActionPolicy.CANCEL; | |
} | |
} | |
if (requestUrl.contains("wa.me")) { | |
await launchUrl(uri, mode: LaunchMode.externalApplication); | |
return NavigationActionPolicy.CANCEL; | |
} | |
return NavigationActionPolicy.ALLOW; | |
} | |
Future<bool?> shouldOverrideCreateWindowMethod( | |
InAppWebViewController controller, CreateWindowAction windowAction) async { | |
if (windowAction.request.url == null) { | |
return false; | |
} | |
var uri = windowAction.request.url!; | |
final requestUrl = windowAction.request.url.toString(); | |
if (requestUrl.contains(defaultUrl) || | |
requestUrl.contains("player.vimeo.com/video/")) { | |
return true; | |
} | |
if (!["http", "https", "file", "chrome", "data", "javascript", "about"] | |
.contains(uri.scheme) || | |
["http", "https"].contains(uri.scheme)) { | |
if (await canLaunchUrl(uri)) { | |
// Launch the App | |
await launchUrl(uri, mode: LaunchMode.externalApplication); | |
return false; | |
} | |
} | |
if (requestUrl.contains("wa.me")) { | |
await launchUrl(uri, mode: LaunchMode.externalApplication); | |
return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment