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(
useShouldOverrideUrlLoading: true,
mediaPlaybackRequiresUserGesture: false,
useHybridComposition: true,
2022-04-22 00:51:56 +00:00
allowsInlineMediaPlayback: true,
iframeAllow: "camera; microphone"
2022-04-19 23:31:14 +00:00
);
2022-04-21 21:14:51 +00:00
PullToRefreshController? pullToRefreshController;
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
})
],
options: ContextMenuOptions(hideDefaultSystemContextMenuItems: false),
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 {
print("onContextMenuActionItemClicked: " +
2022-04-23 15:49:29 +00:00
contextMenuItemClicked.id.toString() +
2021-03-01 19:26:57 +00:00
" " +
contextMenuItemClicked.title);
});
2022-04-21 21:14:51 +00:00
pullToRefreshController = !kIsWeb ? PullToRefreshController(
2022-04-20 17:20:31 +00:00
settings: PullToRefreshSettings(
color: Colors.blue,
),
onRefresh: () async {
if (defaultTargetPlatform == TargetPlatform.android) {
webViewController?.reload();
2022-04-23 15:49:29 +00:00
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
webViewController?.loadUrl(
urlRequest: URLRequest(url: await webViewController?.getUrl()));
}
},
2022-04-21 21:14:51 +00:00
) : null;
}
@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(
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(
child: Stack(
children: [
InAppWebView(
key: webViewKey,
// contextMenu: contextMenu,
2022-04-22 08:38:19 +00:00
// initialUrlRequest:
2022-04-22 11:39:21 +00:00
// URLRequest(url: Uri.parse("https://www.pubnub.com/developers/demos/webrtc/launch/")),
initialUrlRequest:
URLRequest(url: Uri.parse(Uri.base.toString().replaceFirst("/#/", "/") + 'page.html')),
// initialFile: "assets/index.html",
initialUserScripts: UnmodifiableListView<UserScript>([]),
2022-04-19 23:31:14 +00:00
initialSettings: settings,
pullToRefreshController: pullToRefreshController,
onWebViewCreated: (controller) {
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!;
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 {
2022-04-21 21:14:51 +00:00
pullToRefreshController?.endRefreshing();
setState(() {
this.url = url.toString();
urlController.text = this.url;
});
},
onLoadError: (controller, url, code, message) {
2022-04-21 21:14:51 +00:00
pullToRefreshController?.endRefreshing();
},
onProgressChanged: (controller, progress) {
if (progress == 100) {
2022-04-21 21:14:51 +00:00
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);
},
),
2022-04-21 21:14:51 +00:00
!kIsWeb && 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: () {
webViewController?.reload();
2021-03-01 19:26:57 +00:00
},
),
2022-04-22 11:39:21 +00:00
ElevatedButton(
child: Icon(Icons.cancel),
onPressed: () {
webViewController?.stopLoading();
},
),
2021-03-01 19:26:57 +00:00
],
),
])));
}
2021-03-01 19:26:57 +00:00
}