iosWebViewFix/example/lib/main.dart

476 lines
16 KiB
Dart
Raw Normal View History

2018-10-15 23:27:58 +00:00
import 'dart:async';
2018-10-22 23:45:42 +00:00
import 'dart:convert';
2018-09-14 00:21:51 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_inappbrowser/flutter_inappbrowser.dart';
class MyInAppBrowser extends InAppBrowser {
2018-11-07 00:11:27 +00:00
@override
Future onBrowserCreated() async {
print("\n\nBrowser Ready!\n\n");
}
2018-09-14 00:21:51 +00:00
@override
Future onLoadStart(String url) async {
2018-10-05 11:18:28 +00:00
print("\n\nStarted $url\n\n");
2018-10-22 23:45:42 +00:00
// print("\n\n ${await this.isHidden()} \n\n");
// print(await this.webViewController.canGoBack());
// print(await this.webViewController.canGoForward());
2018-09-14 00:21:51 +00:00
}
@override
Future onLoadStop(String url) async {
2018-10-05 11:18:28 +00:00
print("\n\nStopped $url\n\n");
2018-11-07 00:11:27 +00:00
// print(base64.encode(await this.webViewController.getFavicon()));
// WebHistory history = await this.webViewController.getCopyBackForwardList();
// print(history.list.length);
// print(history.currentIndex);
// print(history.list[history.currentIndex].url);
// for(WebHistoryItem item in history.list) {
// print(item.title);
// }
2018-11-07 00:11:27 +00:00
//
// print(await this.webViewController.canGoBackOrForward(1));
// if (await this.webViewController.canGoBackOrForward(-2)) {
// this.webViewController.goTo(history.list[0]);
// }
// await this.webViewController.goBackOrForward(-1);
2018-10-21 22:41:39 +00:00
// print(await this.webViewController.canGoBack());
// print(await this.webViewController.canGoForward());
// var screenshot = await this.webViewController.takeScreenshot();
// await this.webViewController.injectScriptCode("""
2018-10-17 23:22:47 +00:00
// document.body.innerHTML = '<img style="max-width: 100%; width: 100%" src="data:image/png;base64,${base64.encode(screenshot)}" />';
// """);
// var options = await this.getOptions();
// if (options["javaScriptEnabled"]) {
// await this.setOptions({
// //"progressBar": true,
// //"useOnLoadResource": true,
// //"hidden": true,
// //"toolbarTopFixedTitle": "Fixed title A",
// //"useShouldOverrideUrlLoading": true
// //"hideUrlBar": true,
// "javaScriptEnabled": false,
// "toolbarTop": true,
// "toolbarBottom": false
// });
// }
// else {
// await this.setOptions({
// //"progressBar": false,
// //"useOnLoadResource": false,
// //"hidden": true,
// //"toolbarTopFixedTitle": "Fixed title B",
// //"useShouldOverrideUrlLoading": true
// //"hideUrlBar": false,
// "javaScriptEnabled": true,
// "toolbarTop": false,
// "toolbarBottom": true
// });
// }
// print("\n\n ${await this.isHidden()} \n\n");
//
2018-10-21 22:41:39 +00:00
// await this.webViewController.injectScriptCode("window.flutter_inappbrowser.callHandler('handlerTest', 1, 5,'string', {'key': 5}, [4,6,8]);");
// await this.webViewController.injectScriptCode("window.flutter_inappbrowser.callHandler('handlerTest2', false, null, undefined);");
// await this.webViewController.injectScriptCode("setTimeout(function(){window.flutter_inappbrowser.callHandler('handlerTest', 'anotherString');}, 1000);");
2018-10-17 23:22:47 +00:00
2018-10-21 22:41:39 +00:00
// await this.webViewController.injectScriptCode("console.log({'testObject': 5});");
// await this.webViewController.injectScriptCode("console.warn('testWarn',null);");
// await this.webViewController.injectScriptCode("console.log('testObjectStringify', JSON.stringify({'asd': 5}));");
// await this.webViewController.injectScriptCode("console.info('testInfo', 6);");
// await this.webViewController.injectScriptCode("console.error('testError', false);");
// await this.webViewController.injectScriptCode("console.debug('testDebug', true);");
//
2018-10-27 19:07:00 +00:00
// print(await this.webViewController.injectScriptCode("document.cookie"));
//
2018-10-27 23:16:29 +00:00
// print("");
// print(await CookieManager.getCookies(url));
// print("");
// print(await CookieManager.getCookie(url, "my_cookie2"));
// print("");
// await CookieManager.deleteCookie(url, "my_cookie2");
// await CookieManager.deleteCookie(url, "_gid", domain: ".googleblog.com");
// print("");
// print(await CookieManager.getCookies(url));
// print("");
// await CookieManager.deleteCookies(url);
// print("");
// print(await CookieManager.getCookies(url));
// print("");
// await CookieManager.deleteAllCookies();
// print("");
// print(await CookieManager.getCookies(url));
// print("");
2018-10-21 22:41:39 +00:00
//
// print(await this.webViewController.injectScriptCode("null"));
// print(await this.webViewController.injectScriptCode("undefined"));
// print(await this.webViewController.injectScriptCode("3"));
// print(await this.webViewController.injectScriptCode("""
// function asd (a,b) {
// return a+b;
// };
// asd(3,5);
// """));
2018-10-21 22:41:39 +00:00
// print(await this.webViewController.injectScriptCode("""
// ["3",56,"sdf"];
// """));
2018-10-21 22:41:39 +00:00
// print(await this.webViewController.injectScriptCode("""
// var x = {"as":4, "dfdfg": 6};
// x;
// """));
2018-10-15 23:27:58 +00:00
//
2018-10-21 22:41:39 +00:00
// await this.webViewController.injectScriptFile("https://code.jquery.com/jquery-3.3.1.min.js");
// this.webViewController.injectScriptCode("""
2018-09-23 23:53:22 +00:00
// \$( "body" ).html( "Next Step..." )
// """);
//
// // add custom css
2018-10-21 22:41:39 +00:00
// this.webViewController.injectStyleCode("""
2018-09-23 23:53:22 +00:00
// body {
// background-color: #3c3c3c !important;
// }
// """);
2018-10-21 22:41:39 +00:00
// this.webViewController.injectStyleFile("https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css");
2018-09-14 00:21:51 +00:00
}
2018-11-07 00:11:27 +00:00
@override
Future onScrollChanged(int x, int y) async {
// print(x.toString() + " " + y.toString());
}
2018-09-14 00:21:51 +00:00
@override
void onLoadError(String url, int code, String message) {
2018-09-14 00:21:51 +00:00
print("\n\nCan't load $url.. Error: $message\n\n");
}
2018-10-21 22:41:39 +00:00
@override
void onProgressChanged(int progress) {
2018-10-22 23:45:42 +00:00
// print("Progress: $progress");
2018-10-21 22:41:39 +00:00
}
2018-09-14 00:21:51 +00:00
@override
void onExit() {
print("\n\nBrowser closed!\n\n");
}
2018-09-23 19:38:31 +00:00
@override
void shouldOverrideUrlLoading(String url) {
print("\n\n override $url\n\n");
2018-10-21 22:41:39 +00:00
this.webViewController.loadUrl(url);
2018-11-07 00:11:27 +00:00
2018-10-22 23:45:42 +00:00
// var postData = "username=my_username&password=my_password";
// inAppBrowserFallback.webViewController.postUrl("http://localhost:8080", utf8.encode(postData));
2018-11-07 00:11:27 +00:00
2018-10-22 23:45:42 +00:00
// var htmlData = """
//<!doctype html>
//<html lang="en">
//<head>
// <meta charset="UTF-8">
// <meta name="viewport"
// content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
// <meta http-equiv="X-UA-Compatible" content="ie=edge">
// <title>Document</title>
// <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
// <link rel="stylesheet" href="http://localhost:8080/assets/css/style.css">
// <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
//</head>
//<body>
//<div class="container">
// <div class="container">
// <img src="http://localhost:8080/assets/images/dart.svg" alt="dart logo">
// <div class="row">
// <div class="col-sm">
// One of three columns
// </div>
// <div class="col-sm">
// One of three columns
// </div>
// <div class="col-sm">
// One of three columns
// </div>
// </div>
// </div>
// <script>
// console.log("hello");
// </script>
//</div>
//</body>
//</html>
// """;
// inAppBrowserFallback.webViewController.loadData(htmlData);
2018-09-23 19:38:31 +00:00
}
@override
2018-10-15 23:27:58 +00:00
void onLoadResource(
WebResourceResponse response, WebResourceRequest request) {
print("Started at: " +
response.startTime.toString() +
"ms ---> duration: " +
response.duration.toString() +
"ms " +
response.url);
// if (response.headers["content-length"] != null)
// print(response.headers["content-length"] + " length");
}
@override
void onConsoleMessage(ConsoleMessage consoleMessage) {
2018-10-17 23:22:47 +00:00
print(consoleMessage.message);
// print("""
// console output:
// sourceURL: ${consoleMessage.sourceURL}
// lineNumber: ${consoleMessage.lineNumber}
// message: ${consoleMessage.message}
// messageLevel: ${consoleMessage.messageLevel}
// """);
}
2018-09-14 00:21:51 +00:00
}
MyInAppBrowser inAppBrowserFallback = new MyInAppBrowser();
class MyChromeSafariBrowser extends ChromeSafariBrowser {
MyChromeSafariBrowser(browserFallback) : super(browserFallback);
@override
void onOpened() {
print("ChromeSafari browser opened");
}
@override
void onLoaded() {
print("ChromeSafari browser loaded");
}
@override
void onClosed() {
print("ChromeSafari browser closed");
}
}
// adding a webview fallback
2018-10-15 23:27:58 +00:00
MyChromeSafariBrowser chromeSafariBrowser =
new MyChromeSafariBrowser(inAppBrowserFallback);
2018-10-21 22:41:39 +00:00
InAppLocalhostServer localhostServer = new InAppLocalhostServer();
2018-09-14 00:21:51 +00:00
2018-10-15 23:27:58 +00:00
Future main() async {
2018-10-21 22:41:39 +00:00
// await localhostServer.start();
runApp(new MyApp());
}
2018-09-14 00:21:51 +00:00
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
2018-10-21 22:41:39 +00:00
// int indexTest = inAppBrowserFallback.webViewController.addJavaScriptHandler("handlerTest",
2018-10-15 23:27:58 +00:00
// (arguments) async {
2018-10-13 19:14:31 +00:00
// print("handlerTest arguments");
// print(arguments);
// });
2018-10-21 22:41:39 +00:00
// int indexTest2 = inAppBrowserFallback.webViewController.addJavaScriptHandler("test2", (arguments) async {
2018-10-13 19:14:31 +00:00
// print("handlerTest2 arguments");
// print(arguments);
2018-10-21 22:41:39 +00:00
// inAppBrowserFallback.webViewController.removeJavaScriptHandler("test", indexTest);
2018-10-13 19:14:31 +00:00
// });
2018-09-14 00:21:51 +00:00
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
2018-09-14 00:41:43 +00:00
title: const Text('Flutter InAppBrowser Plugin example app'),
2018-09-14 00:21:51 +00:00
),
body: new Center(
2018-10-15 23:27:58 +00:00
child: new RaisedButton(
2018-10-21 22:41:39 +00:00
onPressed: () async {
2018-10-15 23:27:58 +00:00
// await chromeSafariBrowser.open("https://flutter.io/");
2018-10-21 22:41:39 +00:00
//
2018-10-15 23:27:58 +00:00
// await InAppBrowser.openWithSystemBrowser("https://flutter.io/");
2018-10-21 22:41:39 +00:00
//
// await inAppBrowserFallback.open(url: "http://localhost:8080/assets/index.html", options: {
2018-10-15 23:27:58 +00:00
// "useOnLoadResource": true,
// //"hidden": true,
// //"toolbarTopFixedTitle": "Fixed title",
// //"useShouldOverrideUrlLoading": true
// //"hideUrlBar": true,
// //"toolbarTop": false,
2018-10-21 22:41:39 +00:00
// "toolbarBottom": false
2018-10-15 23:27:58 +00:00
// });
2018-10-21 22:41:39 +00:00
//
// await inAppBrowserFallback.openFile("assets/index.html", options: {
2018-10-17 23:22:47 +00:00
// "useOnLoadResource": true,
2018-10-15 23:27:58 +00:00
// //"hidden": true,
// //"useShouldOverrideUrlLoading": true
// //"hideUrlBar": true,
// //"toolbarTop": false,
// //"toolbarBottom": false
// });
2018-10-21 22:41:39 +00:00
//
2018-10-27 23:16:29 +00:00
// await CookieManager.setCookie("https://flutter.io/", "my_cookie2", "cookieValue2", domain: "flutter.io", expiresDate: 1540838864611);
// await CookieManager.setCookie("https://flutter.io/", "my_cookie", "cookieValue", domain: "flutter.io", expiresDate: 1540838864611);
2018-10-27 19:07:00 +00:00
2018-11-07 00:11:27 +00:00
// await inAppBrowserFallback.openData("<html><head><title>Data example</title></head><body><p>This is a \"p\" tag</p></body></html>", options: {});
2018-10-17 23:22:47 +00:00
await inAppBrowserFallback.open(url: "https://flutter.io/", options: {
2018-10-22 23:45:42 +00:00
//"useOnLoadResource": true,
2018-10-17 23:22:47 +00:00
//"hidden": true,
//"toolbarTopFixedTitle": "Fixed title",
2018-11-07 00:11:27 +00:00
"useShouldOverrideUrlLoading": true,
2018-10-17 23:22:47 +00:00
//"hideUrlBar": true,
//"toolbarTop": false,
//"toolbarBottom": false
});
2018-10-21 22:41:39 +00:00
},
child: Text("Open InAppBrowser")
),
2018-09-14 00:21:51 +00:00
),
),
);
}
}
2018-10-21 22:41:39 +00:00
// Inline WebView Example
//
//import 'dart:async';
//import 'package:flutter/material.dart';
//import 'package:flutter_inappbrowser/flutter_inappbrowser.dart';
//
//Future main() async {
// runApp(new MyApp());
//}
//
//class MyApp extends StatefulWidget {
// @override
// _MyAppState createState() => new _MyAppState();
//}
//
//class _MyAppState extends State<MyApp> {
//
// InAppWebViewController webView;
// String url = "";
// double progress = 0;
//
// @override
// void initState() {
// super.initState();
// }
//
// @override
// void dispose() {
// super.dispose();
// }
//
// @override
// Widget build(BuildContext context) {
// return MaterialApp(
// home: Scaffold(
// appBar: AppBar(
// title: const Text('Inline WebView example app'),
// ),
// body: Container(
// child: Column(
// children: <Widget>[
// Container(
// padding: EdgeInsets.all(20.0),
// child: Text("CURRENT URL\n${ (url.length > 50) ? url.substring(0, 50) + "..." : url }"),
// ),
// (progress != 1.0) ? LinearProgressIndicator(value: progress) : null,
// Expanded(
// child: Container(
// margin: const EdgeInsets.all(10.0),
// decoration: BoxDecoration(
// border: Border.all(color: Colors.blueAccent)
// ),
// child: InAppWebView(
// initialUrl: "https://flutter.io/",
2018-11-07 00:11:27 +00:00
// //initialData: InAppWebViewInitialData("<html><head><title>Data example</title></head><body><p>This is a \"p\" tag</p></body></html>"),
2018-10-21 22:41:39 +00:00
// initialHeaders: {
//
// },
// initialOptions: {
//
// },
// onWebViewCreated: (InAppWebViewController controller) {
// webView = controller;
// },
// onLoadStart: (InAppWebViewController controller, String url) {
// print("started $url");
// setState(() {
// this.url = url;
// });
// },
// onProgressChanged: (InAppWebViewController controller, int progress) {
// setState(() {
// this.progress = progress/100;
// });
// },
// ),
// ),
// ),
// ButtonBar(
// alignment: MainAxisAlignment.center,
// children: <Widget>[
// RaisedButton(
// child: Icon(Icons.arrow_back),
// onPressed: () {
// if (webView != null) {
// webView.goBack();
// }
// },
// ),
// RaisedButton(
// child: Icon(Icons.arrow_forward),
// onPressed: () {
// if (webView != null) {
// webView.goForward();
// }
// },
// ),
// RaisedButton(
// child: Icon(Icons.refresh),
// onPressed: () {
// if (webView != null) {
// webView.reload();
// }
// },
// ),
// ],
// ),
// ].where((Object o) => o != null).toList(),
// ),
// ),
// bottomNavigationBar: BottomNavigationBar(
// currentIndex: 0,
// items: [
// BottomNavigationBarItem(
// icon: Icon(Icons.home),
// title: Text('Home'),
// ),
// BottomNavigationBarItem(
// icon: Icon(Icons.mail),
// title: Text('Item 2'),
// ),
// BottomNavigationBarItem(
// icon: Icon(Icons.person),
// title: Text('Item 3')
// )
// ],
// ),
// ),
// );
// }
//}