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-03-05 22:19:50 +00:00
|
|
|
|
2021-02-08 00:17:12 +00:00
|
|
|
final GlobalKey webViewKey = GlobalKey();
|
|
|
|
|
2021-03-05 22:19:50 +00:00
|
|
|
InAppWebViewController? webViewController;
|
2022-04-19 23:31:14 +00:00
|
|
|
InAppWebViewSettings settings = InAppWebViewSettings(
|
|
|
|
useShouldOverrideUrlLoading: true,
|
|
|
|
mediaPlaybackRequiresUserGesture: false,
|
|
|
|
useHybridComposition: true,
|
2022-04-21 00:14:21 +00:00
|
|
|
allowsInlineMediaPlayback: true
|
2022-04-19 23:31:14 +00:00
|
|
|
);
|
2021-03-05 22:19:50 +00:00
|
|
|
|
|
|
|
late PullToRefreshController pullToRefreshController;
|
2021-01-28 16:10:15 +00:00
|
|
|
late ContextMenu contextMenu;
|
2019-11-18 21:21:35 +00:00
|
|
|
String url = "";
|
|
|
|
double progress = 0;
|
2021-03-05 22:19:50 +00:00
|
|
|
final urlController = TextEditingController();
|
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!");
|
2021-03-05 22:19:50 +00:00
|
|
|
print(await webViewController?.getSelectedText());
|
|
|
|
await webViewController?.clearFocus();
|
2021-03-01 19:26:57 +00:00
|
|
|
})
|
|
|
|
],
|
|
|
|
options: ContextMenuOptions(hideDefaultSystemContextMenuItems: false),
|
|
|
|
onCreateContextMenu: (hitTestResult) async {
|
|
|
|
print("onCreateContextMenu");
|
|
|
|
print(hitTestResult.extra);
|
2021-03-05 22:19:50 +00:00
|
|
|
print(await webViewController?.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);
|
|
|
|
});
|
2021-03-05 22:19:50 +00:00
|
|
|
|
|
|
|
pullToRefreshController = PullToRefreshController(
|
2022-04-20 17:20:31 +00:00
|
|
|
settings: PullToRefreshSettings(
|
2021-03-05 22:19:50 +00:00
|
|
|
color: Colors.blue,
|
|
|
|
),
|
|
|
|
onRefresh: () async {
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
webViewController?.reload();
|
|
|
|
} else if (Platform.isIOS) {
|
|
|
|
webViewController?.loadUrl(
|
|
|
|
urlRequest: URLRequest(url: await webViewController?.getUrl()));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2019-11-18 21:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-05 22:19:50 +00:00
|
|
|
TextField(
|
|
|
|
decoration: InputDecoration(
|
|
|
|
prefixIcon: Icon(Icons.search)
|
|
|
|
),
|
|
|
|
controller: urlController,
|
|
|
|
keyboardType: TextInputType.url,
|
|
|
|
onSubmitted: (value) {
|
|
|
|
var url = Uri.parse(value);
|
|
|
|
if (url.scheme.isEmpty) {
|
|
|
|
url = Uri.parse("https://www.google.com/search?q=" + value);
|
|
|
|
}
|
|
|
|
webViewController?.loadUrl(
|
|
|
|
urlRequest: URLRequest(url: url));
|
|
|
|
},
|
2021-03-01 19:26:57 +00:00
|
|
|
),
|
|
|
|
Expanded(
|
2021-03-05 22:19:50 +00:00
|
|
|
child: Stack(
|
|
|
|
children: [
|
|
|
|
InAppWebView(
|
|
|
|
key: webViewKey,
|
|
|
|
// contextMenu: contextMenu,
|
|
|
|
initialUrlRequest:
|
2022-04-21 00:14:21 +00:00
|
|
|
URLRequest(url: Uri.parse("http://github.com/flutter/")),
|
2021-03-05 22:19:50 +00:00
|
|
|
// initialFile: "assets/index.html",
|
|
|
|
initialUserScripts: UnmodifiableListView<UserScript>([]),
|
2022-04-19 23:31:14 +00:00
|
|
|
initialSettings: settings,
|
2021-03-05 22:19:50 +00:00
|
|
|
pullToRefreshController: pullToRefreshController,
|
|
|
|
onWebViewCreated: (controller) {
|
|
|
|
webViewController = controller;
|
|
|
|
},
|
2022-04-21 00:14:21 +00:00
|
|
|
onLoadStart: (controller, url) async {
|
2021-03-05 22:19:50 +00:00
|
|
|
setState(() {
|
|
|
|
this.url = url.toString();
|
|
|
|
urlController.text = this.url;
|
|
|
|
});
|
|
|
|
},
|
2022-04-21 11:33:07 +00:00
|
|
|
onPermissionRequest: (controller, request) async {
|
|
|
|
return PermissionResponse(
|
|
|
|
resources: request.resources,
|
|
|
|
action: PermissionResponseAction.GRANT);
|
2021-03-05 22:19:50 +00:00
|
|
|
},
|
|
|
|
shouldOverrideUrlLoading: (controller, navigationAction) async {
|
|
|
|
var uri = navigationAction.request.url!;
|
2020-07-02 15:30:55 +00:00
|
|
|
|
2021-03-05 22:19:50 +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-05 22:19:50 +00:00
|
|
|
return NavigationActionPolicy.ALLOW;
|
|
|
|
},
|
|
|
|
onLoadStop: (controller, url) async {
|
|
|
|
pullToRefreshController.endRefreshing();
|
|
|
|
setState(() {
|
|
|
|
this.url = url.toString();
|
|
|
|
urlController.text = this.url;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onLoadError: (controller, url, code, message) {
|
|
|
|
pullToRefreshController.endRefreshing();
|
|
|
|
},
|
|
|
|
onProgressChanged: (controller, progress) {
|
|
|
|
if (progress == 100) {
|
|
|
|
pullToRefreshController.endRefreshing();
|
|
|
|
}
|
|
|
|
setState(() {
|
|
|
|
this.progress = progress / 100;
|
|
|
|
urlController.text = this.url;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onUpdateVisitedHistory: (controller, url, androidIsReload) {
|
|
|
|
setState(() {
|
|
|
|
this.url = url.toString();
|
|
|
|
urlController.text = this.url;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onConsoleMessage: (controller, consoleMessage) {
|
|
|
|
print(consoleMessage);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
progress < 1.0
|
|
|
|
? LinearProgressIndicator(value: progress)
|
|
|
|
: Container(),
|
|
|
|
],
|
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: () {
|
2021-03-05 22:19:50 +00:00
|
|
|
webViewController?.goBack();
|
2021-03-01 19:26:57 +00:00
|
|
|
},
|
2019-11-28 01:32:03 +00:00
|
|
|
),
|
2021-03-01 19:26:57 +00:00
|
|
|
ElevatedButton(
|
|
|
|
child: Icon(Icons.arrow_forward),
|
|
|
|
onPressed: () {
|
2021-03-05 22:19:50 +00:00
|
|
|
webViewController?.goForward();
|
2021-03-01 19:26:57 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
ElevatedButton(
|
|
|
|
child: Icon(Icons.refresh),
|
|
|
|
onPressed: () {
|
2021-03-05 22:19:50 +00:00
|
|
|
webViewController?.reload();
|
2021-03-01 19:26:57 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
])));
|
2019-11-18 21:21:35 +00:00
|
|
|
}
|
2021-03-01 19:26:57 +00:00
|
|
|
}
|