2021-02-01 14:55:27 +00:00
|
|
|
import 'dart:collection';
|
2021-02-24 23:00:46 +00:00
|
|
|
// import 'dart:convert';
|
2020-05-21 01:34:39 +00:00
|
|
|
import 'dart:io';
|
2021-02-24 23:00:46 +00:00
|
|
|
// import 'dart:typed_data';
|
2020-05-21 01:34:39 +00:00
|
|
|
|
2019-11-18 21:21:35 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2019-11-29 15:59:18 +00:00
|
|
|
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
|
2021-02-09 00:39:35 +00:00
|
|
|
// import 'package:path_provider/path_provider.dart';
|
2020-07-02 15:30:55 +00:00
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2019-11-25 11:51:10 +00:00
|
|
|
|
|
|
|
import 'main.dart';
|
2019-11-18 21:21:35 +00:00
|
|
|
|
|
|
|
class InAppWebViewExampleScreen extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_InAppWebViewExampleScreenState createState() =>
|
|
|
|
new _InAppWebViewExampleScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
|
2021-02-08 00:17:12 +00:00
|
|
|
final GlobalKey webViewKey = GlobalKey();
|
|
|
|
|
2021-01-28 16:10:15 +00:00
|
|
|
InAppWebViewController? webView;
|
|
|
|
late ContextMenu contextMenu;
|
2019-11-18 21:21:35 +00:00
|
|
|
String url = "";
|
|
|
|
double progress = 0;
|
2021-02-24 23:00:46 +00:00
|
|
|
// CookieManager _cookieManager = CookieManager.instance();
|
2019-11-18 21:21:35 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2020-05-21 01:34:39 +00:00
|
|
|
|
|
|
|
contextMenu = ContextMenu(
|
2021-03-01 19:26:57 +00:00
|
|
|
menuItems: [
|
|
|
|
ContextMenuItem(
|
|
|
|
androidId: 1,
|
|
|
|
iosId: "1",
|
|
|
|
title: "Special",
|
|
|
|
action: () async {
|
|
|
|
print("Menu item Special clicked!");
|
|
|
|
print(await webView?.getSelectedText());
|
|
|
|
await webView?.clearFocus();
|
|
|
|
})
|
|
|
|
],
|
|
|
|
options: ContextMenuOptions(hideDefaultSystemContextMenuItems: false),
|
|
|
|
onCreateContextMenu: (hitTestResult) async {
|
|
|
|
print("onCreateContextMenu");
|
|
|
|
print(hitTestResult.extra);
|
2021-01-28 16:10:15 +00:00
|
|
|
print(await webView?.getSelectedText());
|
2021-03-01 19:26:57 +00:00
|
|
|
},
|
|
|
|
onHideContextMenu: () {
|
|
|
|
print("onHideContextMenu");
|
|
|
|
},
|
|
|
|
onContextMenuActionItemClicked: (contextMenuItemClicked) async {
|
|
|
|
var id = (Platform.isAndroid)
|
|
|
|
? contextMenuItemClicked.androidId
|
|
|
|
: contextMenuItemClicked.iosId;
|
|
|
|
print("onContextMenuActionItemClicked: " +
|
|
|
|
id.toString() +
|
|
|
|
" " +
|
|
|
|
contextMenuItemClicked.title);
|
|
|
|
});
|
2019-11-18 21:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2021-02-22 11:16:23 +00:00
|
|
|
var options = InAppWebViewGroupOptions(
|
|
|
|
crossPlatform: InAppWebViewOptions(
|
|
|
|
useShouldOverrideUrlLoading: false,
|
|
|
|
mediaPlaybackRequiresUserGesture: false,
|
|
|
|
),
|
|
|
|
android: AndroidInAppWebViewOptions(
|
|
|
|
useHybridComposition: true,
|
|
|
|
),
|
|
|
|
ios: IOSInAppWebViewOptions(
|
|
|
|
allowsInlineMediaPlayback: true,
|
2021-03-01 19:26:57 +00:00
|
|
|
));
|
2021-02-22 11:16:23 +00:00
|
|
|
|
2019-11-18 21:21:35 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2021-03-01 19:26:57 +00:00
|
|
|
appBar: AppBar(title: Text("InAppWebView")),
|
2019-11-25 11:51:10 +00:00
|
|
|
drawer: myDrawer(context: context),
|
2019-12-09 23:29:09 +00:00
|
|
|
body: SafeArea(
|
2019-11-28 01:32:03 +00:00
|
|
|
child: Column(children: <Widget>[
|
2021-03-01 19:26:57 +00:00
|
|
|
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:
|
2019-11-28 01:32:03 +00:00
|
|
|
BoxDecoration(border: Border.all(color: Colors.blueAccent)),
|
2021-03-01 19:26:57 +00:00
|
|
|
child: InAppWebView(
|
|
|
|
key: webViewKey,
|
|
|
|
// contextMenu: contextMenu,
|
|
|
|
initialUrlRequest:
|
|
|
|
URLRequest(url: Uri.parse("https://flutter.dev")),
|
|
|
|
// 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!;
|
2020-07-02 15:30:55 +00:00
|
|
|
|
2021-03-01 19:26:57 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2020-07-02 15:30:55 +00:00
|
|
|
|
2021-03-01 19:26:57 +00:00
|
|
|
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);
|
|
|
|
},
|
2019-11-18 21:21:35 +00:00
|
|
|
),
|
2021-03-01 19:26:57 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
ButtonBar(
|
|
|
|
alignment: MainAxisAlignment.center,
|
|
|
|
children: <Widget>[
|
|
|
|
ElevatedButton(
|
|
|
|
child: Icon(Icons.arrow_back),
|
|
|
|
onPressed: () {
|
|
|
|
webView?.goBack();
|
|
|
|
},
|
2019-11-28 01:32:03 +00:00
|
|
|
),
|
2021-03-01 19:26:57 +00:00
|
|
|
ElevatedButton(
|
|
|
|
child: Icon(Icons.arrow_forward),
|
|
|
|
onPressed: () {
|
|
|
|
webView?.goForward();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
ElevatedButton(
|
|
|
|
child: Icon(Icons.refresh),
|
|
|
|
onPressed: () {
|
|
|
|
webView?.reload();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
])));
|
2019-11-18 21:21:35 +00:00
|
|
|
}
|
2021-03-01 19:26:57 +00:00
|
|
|
}
|