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

226 lines
8.1 KiB
Dart
Raw Normal View History

import 'dart:collection';
2022-04-21 21:14:51 +00:00
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
2019-11-29 15:59:18 +00:00
import 'package:flutter_inappwebview/flutter_inappwebview.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? webViewController;
2022-04-19 23:31:14 +00:00
InAppWebViewSettings settings = InAppWebViewSettings(
2022-04-25 21:21:26 +00:00
useShouldOverrideUrlLoading: true,
mediaPlaybackRequiresUserGesture: false,
allowsInlineMediaPlayback: true,
iframeAllow: "camera; microphone",
2022-05-01 02:03:17 +00:00
iframeAllowFullscreen: true
2022-04-19 23:31:14 +00:00
);
2022-04-21 21:14:51 +00:00
PullToRefreshController? pullToRefreshController;
2022-10-08 12:19:35 +00:00
late ContextMenu contextMenu;
String url = "";
double progress = 0;
final urlController = TextEditingController();
@override
void initState() {
super.initState();
contextMenu = ContextMenu(
2021-03-01 19:26:57 +00:00
menuItems: [
ContextMenuItem(
id: 1,
2021-03-01 19:26:57 +00:00
title: "Special",
action: () async {
print("Menu item Special clicked!");
print(await webViewController?.getSelectedText());
await webViewController?.clearFocus();
2021-03-01 19:26:57 +00:00
})
],
2022-04-23 20:10:02 +00:00
settings: ContextMenuSettings(hideDefaultSystemContextMenuItems: false),
2021-03-01 19:26:57 +00:00
onCreateContextMenu: (hitTestResult) async {
print("onCreateContextMenu");
print(hitTestResult.extra);
print(await webViewController?.getSelectedText());
2021-03-01 19:26:57 +00:00
},
onHideContextMenu: () {
print("onHideContextMenu");
},
onContextMenuActionItemClicked: (contextMenuItemClicked) async {
2022-04-25 21:21:26 +00:00
var id = contextMenuItemClicked.id;
2021-03-01 19:26:57 +00:00
print("onContextMenuActionItemClicked: " +
2022-04-25 21:21:26 +00:00
id.toString() +
2021-03-01 19:26:57 +00:00
" " +
contextMenuItemClicked.title);
});
2022-10-17 00:23:05 +00:00
pullToRefreshController = kIsWeb || ![TargetPlatform.iOS, TargetPlatform.android].contains(defaultTargetPlatform)
2022-04-25 21:21:26 +00:00
? null
: PullToRefreshController(
settings: PullToRefreshSettings(
color: Colors.blue,
),
onRefresh: () async {
if (defaultTargetPlatform == TargetPlatform.android) {
webViewController?.reload();
} else if (defaultTargetPlatform == TargetPlatform.iOS ||
defaultTargetPlatform == TargetPlatform.macOS) {
webViewController?.loadUrl(
urlRequest:
URLRequest(url: await webViewController?.getUrl()));
}
},
);
}
@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")),
drawer: myDrawer(context: context),
body: SafeArea(
child: Column(children: <Widget>[
TextField(
2022-04-25 21:21:26 +00:00
decoration: InputDecoration(prefixIcon: Icon(Icons.search)),
controller: urlController,
keyboardType: TextInputType.text,
onSubmitted: (value) {
var url = Uri.parse(value);
if (url.scheme.isEmpty) {
2022-04-25 21:21:26 +00:00
url = Uri.parse((!kIsWeb
? "https://www.google.com/search?q="
: "https://www.bing.com/search?q=") +
value);
}
2022-04-25 21:21:26 +00:00
webViewController?.loadUrl(urlRequest: URLRequest(url: url));
},
2021-03-01 19:26:57 +00:00
),
Expanded(
2022-04-25 21:21:26 +00:00
child: Stack(
children: [
InAppWebView(
key: webViewKey,
2022-04-30 19:22:31 +00:00
initialUrlRequest:
2022-10-11 08:10:13 +00:00
URLRequest(url: Uri.parse('https://flutter.dev')),
2022-04-30 19:23:16 +00:00
// initialUrlRequest:
// URLRequest(url: Uri.parse(Uri.base.toString().replaceFirst("/#/", "/") + 'page.html')),
2022-04-25 21:21:26 +00:00
// initialFile: "assets/index.html",
initialUserScripts: UnmodifiableListView<UserScript>([]),
initialSettings: settings,
// contextMenu: contextMenu,
pullToRefreshController: pullToRefreshController,
2022-04-30 19:22:31 +00:00
onWebViewCreated: (controller) async {
2022-04-25 21:21:26 +00:00
webViewController = controller;
},
onLoadStart: (controller, url) async {
setState(() {
this.url = url.toString();
urlController.text = this.url;
});
},
onPermissionRequest: (controller, request) async {
return PermissionResponse(
resources: request.resources,
action: PermissionResponseAction.GRANT);
},
shouldOverrideUrlLoading:
(controller, navigationAction) async {
var uri = navigationAction.request.url!;
2022-04-25 21:21:26 +00:00
if (![
"http",
"https",
"file",
"chrome",
"data",
"javascript",
"about"
].contains(uri.scheme)) {
2022-04-27 14:59:49 +00:00
if (await canLaunchUrl(uri)) {
2022-04-25 21:21:26 +00:00
// Launch the App
2022-04-27 14:59:49 +00:00
await launchUrl(
uri,
2022-04-25 21:21:26 +00:00
);
// and cancel the request
return NavigationActionPolicy.CANCEL;
}
2022-04-25 21:21:26 +00:00
}
2022-04-25 21:21:26 +00:00
return NavigationActionPolicy.ALLOW;
},
onLoadStop: (controller, url) async {
pullToRefreshController?.endRefreshing();
setState(() {
this.url = url.toString();
urlController.text = this.url;
});
},
onReceivedError: (controller, request, error) {
2022-04-25 21:21:26 +00:00
pullToRefreshController?.endRefreshing();
},
onProgressChanged: (controller, progress) {
if (progress == 100) {
2022-04-21 21:14:51 +00:00
pullToRefreshController?.endRefreshing();
2022-04-25 21:21:26 +00:00
}
setState(() {
this.progress = progress / 100;
urlController.text = this.url;
});
},
2022-10-11 08:10:13 +00:00
onUpdateVisitedHistory: (controller, url, isReload) {
2022-04-25 21:21:26 +00:00
setState(() {
this.url = url.toString();
urlController.text = this.url;
});
},
onConsoleMessage: (controller, consoleMessage) {
print(consoleMessage);
},
),
progress < 1.0
? LinearProgressIndicator(value: progress)
: Container(),
],
),
2021-03-01 19:26:57 +00:00
),
ButtonBar(
alignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
child: Icon(Icons.arrow_back),
onPressed: () {
webViewController?.goBack();
2021-03-01 19:26:57 +00:00
},
),
2021-03-01 19:26:57 +00:00
ElevatedButton(
child: Icon(Icons.arrow_forward),
onPressed: () {
webViewController?.goForward();
2021-03-01 19:26:57 +00:00
},
),
ElevatedButton(
child: Icon(Icons.refresh),
onPressed: () {
2022-04-29 19:48:17 +00:00
webViewController?.reload();
},
),
2021-03-01 19:26:57 +00:00
],
),
])));
}
2021-03-01 19:26:57 +00:00
}