iosWebViewFix/example/lib/in_app_webiew_example.scree...

192 lines
6.8 KiB
Dart
Raw Normal View History

import 'dart:collection';
// import 'dart:convert';
import 'dart:io';
// import 'dart:typed_data';
import 'package:flutter/material.dart';
2019-11-29 15:59:18 +00:00
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
// import 'package:path_provider/path_provider.dart';
import 'package:url_launcher/url_launcher.dart';
import 'main.dart';
class InAppWebViewExampleScreen extends StatefulWidget {
@override
_InAppWebViewExampleScreenState createState() =>
new _InAppWebViewExampleScreenState();
}
class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
final GlobalKey webViewKey = GlobalKey();
InAppWebViewController? webView;
late ContextMenu contextMenu;
String url = "";
double progress = 0;
// CookieManager _cookieManager = CookieManager.instance();
@override
void initState() {
super.initState();
contextMenu = ContextMenu(
menuItems: [
ContextMenuItem(androidId: 1, iosId: "1", title: "Special", action: () async {
2020-06-21 22:07:12 +00:00
print("Menu item Special clicked!");
print(await webView?.getSelectedText());
await webView?.clearFocus();
})
],
options: ContextMenuOptions(
hideDefaultSystemContextMenuItems: false
),
onCreateContextMenu: (hitTestResult) async {
2020-06-21 22:07:12 +00:00
print("onCreateContextMenu");
print(hitTestResult.extra);
print(await webView?.getSelectedText());
},
onHideContextMenu: () {
2020-06-21 22:07:12 +00:00
print("onHideContextMenu");
},
onContextMenuActionItemClicked: (contextMenuItemClicked) async {
var id = (Platform.isAndroid) ? contextMenuItemClicked.androidId : contextMenuItemClicked.iosId;
2020-06-21 22:07:12 +00:00
print("onContextMenuActionItemClicked: " + id.toString() + " " + contextMenuItemClicked.title);
}
);
}
@override
void dispose() {
super.dispose();
}
var options = InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
useShouldOverrideUrlLoading: false,
mediaPlaybackRequiresUserGesture: false,
),
android: AndroidInAppWebViewOptions(
useHybridComposition: true,
),
ios: IOSInAppWebViewOptions(
allowsInlineMediaPlayback: true,
)
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
2019-11-25 22:04:17 +00:00
title: Text("InAppWebView")
),
drawer: myDrawer(context: context),
body: SafeArea(
child: Column(children: <Widget>[
Container(
padding: EdgeInsets.all(20.0),
child: Text(
"CURRENT URL\n${(url.length > 50) ? url.substring(0, 50) + "..." : url}"),
),
Container(
padding: EdgeInsets.all(10.0),
child: progress < 1.0
? LinearProgressIndicator(value: progress)
: Container()),
Expanded(
child: Container(
margin: const EdgeInsets.all(10.0),
decoration:
BoxDecoration(border: Border.all(color: Colors.blueAccent)),
child: InAppWebView(
key: webViewKey,
// contextMenu: contextMenu,
initialUrlRequest: URLRequest(
url: Uri.parse("https://flutter.dev")
),
2020-06-21 22:07:12 +00:00
// initialFile: "assets/index.html",
initialUserScripts: UnmodifiableListView<UserScript>([
]),
initialOptions: options,
onWebViewCreated: (controller) {
webView = controller;
print("onWebViewCreated");
},
onLoadStart: (controller, url) {
print("onLoadStart $url");
setState(() {
this.url = url.toString();
});
},
androidOnPermissionRequest: (InAppWebViewController controller, String origin, List<String> resources) async {
return PermissionRequestResponse(resources: resources, action: PermissionRequestResponseAction.GRANT);
},
shouldOverrideUrlLoading: (controller, navigationAction) async {
var uri = navigationAction.request.url!;
if (!["http", "https", "file",
"chrome", "data", "javascript",
"about"].contains(uri.scheme)) {
if (await canLaunch(url)) {
// Launch the App
await launch(
url,
);
// and cancel the request
return NavigationActionPolicy.CANCEL;
}
}
return NavigationActionPolicy.ALLOW;
},
onLoadStop: (controller, url) async {
print("onLoadStop $url");
setState(() {
this.url = url.toString();
});
webView = controller;
},
onProgressChanged: (controller, progress) {
setState(() {
this.progress = progress / 100;
});
},
onUpdateVisitedHistory: (controller, url, androidIsReload) {
print("onUpdateVisitedHistory $url");
setState(() {
this.url = url.toString();
});
},
onConsoleMessage: (controller, consoleMessage) {
print(consoleMessage);
},
),
),
),
ButtonBar(
alignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
child: Icon(Icons.arrow_back),
onPressed: () {
webView?.goBack();
},
),
ElevatedButton(
child: Icon(Icons.arrow_forward),
onPressed: () {
webView?.goForward();
},
),
ElevatedButton(
child: Icon(Icons.refresh),
onPressed: () {
webView?.reload();
},
),
],
),
]))
2019-11-25 22:04:17 +00:00
);
}
}