2020-05-21 01:34:39 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
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';
|
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-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;
|
2020-06-13 01:50:19 +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(
|
2020-05-30 18:23:33 +00:00
|
|
|
menuItems: [
|
|
|
|
ContextMenuItem(androidId: 1, iosId: "1", title: "Special", action: () async {
|
2020-06-21 22:07:12 +00:00
|
|
|
print("Menu item Special clicked!");
|
2021-01-28 16:10:15 +00:00
|
|
|
print(await webView?.getSelectedText());
|
|
|
|
await webView?.clearFocus();
|
2020-05-30 18:23:33 +00:00
|
|
|
})
|
|
|
|
],
|
|
|
|
options: ContextMenuOptions(
|
2021-01-31 21:08:20 +00:00
|
|
|
hideDefaultSystemContextMenuItems: false
|
2020-05-30 18:23:33 +00:00
|
|
|
),
|
2020-05-21 01:34:39 +00:00
|
|
|
onCreateContextMenu: (hitTestResult) async {
|
2020-06-21 22:07:12 +00:00
|
|
|
print("onCreateContextMenu");
|
2020-05-21 01:34:39 +00:00
|
|
|
print(hitTestResult.extra);
|
2021-01-28 16:10:15 +00:00
|
|
|
print(await webView?.getSelectedText());
|
2020-05-21 01:34:39 +00:00
|
|
|
},
|
|
|
|
onHideContextMenu: () {
|
2020-06-21 22:07:12 +00:00
|
|
|
print("onHideContextMenu");
|
2020-05-21 01:34:39 +00:00
|
|
|
},
|
2020-05-30 18:23:33 +00:00
|
|
|
onContextMenuActionItemClicked: (contextMenuItemClicked) async {
|
2020-05-21 01:34:39 +00:00
|
|
|
var id = (Platform.isAndroid) ? contextMenuItemClicked.androidId : contextMenuItemClicked.iosId;
|
2020-06-21 22:07:12 +00:00
|
|
|
print("onContextMenuActionItemClicked: " + id.toString() + " " + contextMenuItemClicked.title);
|
2020-05-21 01:34:39 +00:00
|
|
|
}
|
|
|
|
);
|
2019-11-18 21:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2019-11-25 22:04:17 +00:00
|
|
|
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>[
|
|
|
|
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(
|
2020-08-27 14:41:40 +00:00
|
|
|
// contextMenu: contextMenu,
|
2021-01-28 16:10:15 +00:00
|
|
|
initialUrl: "https://flutter.dev/",
|
2020-06-21 22:07:12 +00:00
|
|
|
// initialFile: "assets/index.html",
|
2019-11-28 01:32:03 +00:00
|
|
|
initialHeaders: {},
|
2020-05-11 00:48:41 +00:00
|
|
|
initialOptions: InAppWebViewGroupOptions(
|
2020-05-29 08:49:08 +00:00
|
|
|
crossPlatform: InAppWebViewOptions(
|
2021-01-28 17:56:04 +00:00
|
|
|
useShouldOverrideUrlLoading: false,
|
2020-05-29 08:49:08 +00:00
|
|
|
),
|
2020-08-27 14:41:40 +00:00
|
|
|
android: AndroidInAppWebViewOptions(
|
|
|
|
useHybridComposition: true
|
|
|
|
)
|
2019-11-28 01:32:03 +00:00
|
|
|
),
|
2021-01-28 16:10:15 +00:00
|
|
|
onWebViewCreated: (controller) {
|
2019-11-28 01:32:03 +00:00
|
|
|
webView = controller;
|
2019-12-18 20:34:40 +00:00
|
|
|
print("onWebViewCreated");
|
2019-11-28 01:32:03 +00:00
|
|
|
},
|
2021-01-28 16:10:15 +00:00
|
|
|
onLoadStart: (controller, url) {
|
2019-12-16 22:58:10 +00:00
|
|
|
print("onLoadStart $url");
|
2019-11-28 01:32:03 +00:00
|
|
|
setState(() {
|
2021-01-28 16:10:15 +00:00
|
|
|
this.url = url ?? '';
|
2019-11-28 01:32:03 +00:00
|
|
|
});
|
|
|
|
},
|
2020-05-29 08:49:08 +00:00
|
|
|
shouldOverrideUrlLoading: (controller, shouldOverrideUrlLoadingRequest) async {
|
2020-07-02 15:30:55 +00:00
|
|
|
var url = shouldOverrideUrlLoadingRequest.url;
|
|
|
|
var uri = Uri.parse(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 ShouldOverrideUrlLoadingAction.CANCEL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 08:49:08 +00:00
|
|
|
return ShouldOverrideUrlLoadingAction.ALLOW;
|
|
|
|
},
|
2021-01-28 16:10:15 +00:00
|
|
|
onLoadStop: (controller, url) async {
|
2019-12-16 22:58:10 +00:00
|
|
|
print("onLoadStop $url");
|
2019-11-28 01:32:03 +00:00
|
|
|
setState(() {
|
2021-01-28 16:10:15 +00:00
|
|
|
this.url = url ?? '';
|
2019-11-28 01:32:03 +00:00
|
|
|
});
|
|
|
|
},
|
2021-01-28 16:10:15 +00:00
|
|
|
onProgressChanged: (controller, progress) {
|
2019-12-09 23:29:09 +00:00
|
|
|
setState(() {
|
2019-12-16 22:58:10 +00:00
|
|
|
this.progress = progress / 100;
|
2019-12-09 23:29:09 +00:00
|
|
|
});
|
|
|
|
},
|
2021-01-28 16:10:15 +00:00
|
|
|
onUpdateVisitedHistory: (controller, url, androidIsReload) {
|
2019-12-16 22:58:10 +00:00
|
|
|
print("onUpdateVisitedHistory $url");
|
2019-11-28 01:32:03 +00:00
|
|
|
setState(() {
|
2021-01-28 16:10:15 +00:00
|
|
|
this.url = url ?? '';
|
2019-11-28 01:32:03 +00:00
|
|
|
});
|
2020-08-27 14:41:40 +00:00
|
|
|
},
|
|
|
|
onConsoleMessage: (controller, consoleMessage) {
|
|
|
|
print(consoleMessage);
|
|
|
|
},
|
2019-11-25 11:12:10 +00:00
|
|
|
),
|
|
|
|
),
|
2019-11-18 21:21:35 +00:00
|
|
|
),
|
2019-11-28 01:32:03 +00:00
|
|
|
ButtonBar(
|
|
|
|
alignment: MainAxisAlignment.center,
|
|
|
|
children: <Widget>[
|
|
|
|
RaisedButton(
|
|
|
|
child: Icon(Icons.arrow_back),
|
|
|
|
onPressed: () {
|
2021-01-28 16:10:15 +00:00
|
|
|
webView?.goBack();
|
2019-11-28 01:32:03 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
RaisedButton(
|
|
|
|
child: Icon(Icons.arrow_forward),
|
|
|
|
onPressed: () {
|
2021-01-28 16:10:15 +00:00
|
|
|
webView?.goForward();
|
2019-11-28 01:32:03 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
RaisedButton(
|
|
|
|
child: Icon(Icons.refresh),
|
|
|
|
onPressed: () {
|
2021-01-28 16:10:15 +00:00
|
|
|
webView?.reload();
|
2019-11-28 01:32:03 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
]))
|
2019-11-25 22:04:17 +00:00
|
|
|
);
|
2019-11-18 21:21:35 +00:00
|
|
|
}
|
2019-11-28 01:32:03 +00:00
|
|
|
}
|