iosWebViewFix/example/lib/in_app_browser_example.scre...

144 lines
4.0 KiB
Dart
Executable File

import 'dart:async';
import 'dart:collection';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'main.dart';
class MyInAppBrowser extends InAppBrowser {
MyInAppBrowser(
{int? windowId, UnmodifiableListView<UserScript>? initialUserScripts})
: super(windowId: windowId, initialUserScripts: initialUserScripts);
@override
Future onBrowserCreated() async {
print("\n\nBrowser Created!\n\n");
}
@override
Future onLoadStart(url) async {
print("\n\nStarted $url\n\n");
}
@override
Future onLoadStop(url) async {
pullToRefreshController?.endRefreshing();
print("\n\nStopped $url\n\n");
}
@override
void onLoadError(url, code, message) {
pullToRefreshController?.endRefreshing();
print("Can't load $url.. Error: $message");
}
@override
void onProgressChanged(progress) {
if (progress == 100) {
pullToRefreshController?.endRefreshing();
}
print("Progress: $progress");
}
@override
void onExit() {
print("\n\nBrowser closed!\n\n");
}
@override
Future<NavigationActionPolicy> shouldOverrideUrlLoading(
navigationAction) async {
print("\n\nOverride ${navigationAction.request.url}\n\n");
return NavigationActionPolicy.ALLOW;
}
@override
void onLoadResource(response) {
print("Started at: " +
response.startTime.toString() +
"ms ---> duration: " +
response.duration.toString() +
"ms " +
(response.url ?? '').toString());
}
@override
void onConsoleMessage(consoleMessage) {
print("""
console output:
message: ${consoleMessage.message}
messageLevel: ${consoleMessage.messageLevel.toValue()}
""");
}
}
class InAppBrowserExampleScreen extends StatefulWidget {
final MyInAppBrowser browser = new MyInAppBrowser();
@override
_InAppBrowserExampleScreenState createState() =>
new _InAppBrowserExampleScreenState();
}
class _InAppBrowserExampleScreenState extends State<InAppBrowserExampleScreen> {
late PullToRefreshController pullToRefreshController;
@override
void initState() {
super.initState();
pullToRefreshController = PullToRefreshController(
options: PullToRefreshOptions(
color: Colors.black,
),
onRefresh: () async {
if (Platform.isAndroid) {
widget.browser.webViewController.reload();
} else if (Platform.isIOS) {
widget.browser.webViewController.loadUrl(
urlRequest: URLRequest(url: await widget.browser.webViewController.getUrl()));
}
},
);
widget.browser.pullToRefreshController = pullToRefreshController;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"InAppBrowser",
)),
drawer: myDrawer(context: context),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: () async {
await widget.browser.openUrlRequest(
urlRequest:
URLRequest(url: Uri.parse("https://flutter.dev")),
options: InAppBrowserClassOptions(
inAppWebViewGroupOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
useShouldOverrideUrlLoading: true,
useOnLoadResource: true,
))));
},
child: Text("Open In-App Browser")),
Container(height: 40),
ElevatedButton(
onPressed: () async {
await InAppBrowser.openWithSystemBrowser(
url: Uri.parse("https://flutter.dev/"));
},
child: Text("Open System Browser")),
])));
}
}