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

145 lines
4.2 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:collection';
import 'dart:io';
2022-10-17 00:23:05 +00:00
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
2019-11-29 15:59:18 +00:00
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'main.dart';
class MyInAppBrowser extends InAppBrowser {
2021-03-01 19:26:57 +00:00
MyInAppBrowser(
2023-11-17 22:28:11 +00:00
{int? windowId,
UnmodifiableListView<UserScript>? initialUserScripts,
PullToRefreshController? pullToRefreshController})
: super(
windowId: windowId,
initialUserScripts: initialUserScripts,
pullToRefreshController: pullToRefreshController);
@override
Future onBrowserCreated() async {
print("\n\nBrowser Created!\n\n");
}
@override
2022-10-17 00:23:05 +00:00
Future onLoadStart(url) async {}
@override
Future onLoadStop(url) async {
pullToRefreshController?.endRefreshing();
}
@override
Future<PermissionResponse> onPermissionRequest(request) async {
return PermissionResponse(
resources: request.resources, action: PermissionResponseAction.GRANT);
}
@override
void onLoadError(url, code, message) {
pullToRefreshController?.endRefreshing();
}
@override
void onProgressChanged(progress) {
if (progress == 100) {
pullToRefreshController?.endRefreshing();
}
}
@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;
}
void onMainWindowWillClose() {
close();
}
}
class InAppBrowserExampleScreen extends StatefulWidget {
@override
_InAppBrowserExampleScreenState createState() =>
_InAppBrowserExampleScreenState();
}
class _InAppBrowserExampleScreenState extends State<InAppBrowserExampleScreen> {
2023-11-17 22:28:11 +00:00
late final MyInAppBrowser browser;
@override
void initState() {
super.initState();
2023-11-17 22:28:11 +00:00
PullToRefreshController? pullToRefreshController = kIsWeb ||
2022-10-17 00:23:05 +00:00
![TargetPlatform.iOS, TargetPlatform.android]
.contains(defaultTargetPlatform)
? null
: PullToRefreshController(
settings: PullToRefreshSettings(
color: Colors.black,
),
onRefresh: () async {
if (Platform.isAndroid) {
2023-11-17 22:28:11 +00:00
browser.webViewController?.reload();
2022-10-17 00:23:05 +00:00
} else if (Platform.isIOS) {
2023-11-17 22:28:11 +00:00
browser.webViewController?.loadUrl(
2022-10-17 00:23:05 +00:00
urlRequest: URLRequest(
2023-11-17 22:28:11 +00:00
url: await browser.webViewController?.getUrl()));
2022-10-17 00:23:05 +00:00
}
},
);
2023-11-17 22:28:11 +00:00
browser = MyInAppBrowser(pullToRefreshController: pullToRefreshController);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"InAppBrowser",
)),
drawer: myDrawer(context: context),
body: Center(
2020-05-23 10:32:27 +00:00
child: Column(
2021-03-01 19:26:57 +00:00
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: () async {
2023-11-17 22:28:11 +00:00
await browser.openUrlRequest(
urlRequest:
URLRequest(url: WebUri("https://flutter.dev")),
settings: InAppBrowserClassSettings(
2022-10-11 08:10:13 +00:00
browserSettings: InAppBrowserSettings(
toolbarTopBackgroundColor: Colors.blue,
presentationStyle: ModalPresentationStyle.POPOVER),
webViewSettings: InAppWebViewSettings(
isInspectable: kDebugMode,
useShouldOverrideUrlLoading: true,
useOnLoadResource: true,
2022-04-19 23:31:14 +00:00
),
),
2022-04-19 23:31:14 +00:00
);
2021-03-01 19:26:57 +00:00
},
child: Text("Open In-App Browser")),
Container(height: 40),
ElevatedButton(
onPressed: () async {
await InAppBrowser.openWithSystemBrowser(
url: WebUri("https://flutter.dev/"));
2021-03-01 19:26:57 +00:00
},
child: Text("Open System Browser")),
2020-05-23 10:32:27 +00:00
])));
}
}