Skip to content

Commit 35c81b5

Browse files
committed
chore: Optimize routing interceptor (PageWebview)
1 parent 00c6698 commit 35c81b5

4 files changed

Lines changed: 68 additions & 36 deletions

File tree

lib/pages/webview/_scaffold.dart

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ class PageWebviewScaffoldState extends State<PageWebviewScaffold> {
2626
Widget build(BuildContext context) {
2727
return Obx(() {
2828
final child = webview(context);
29-
final allow = controller.canPop.value;
30-
return PopScope(canPop: allow, child: child);
29+
final isAllow = controller.canPop.value;
30+
return PopScope(canPop: isAllow, child: child);
3131
});
3232
}
3333

3434
Widget webview(BuildContext context) {
3535
final webViewKey = controller.webViewKey;
3636
final webviewMeta = controller.webviewMeta;
37+
final focusWebview = controller.focusWebview;
3738
final writeScripts = controller.writeScripts;
3839
final popuping = controller.popuping;
3940
final titling = controller.titling;
@@ -120,20 +121,18 @@ class PageWebviewScaffoldState extends State<PageWebviewScaffold> {
120121
initialUrlRequest: initialUrl,
121122
initialSettings: initialSettings,
122123
initialUserScripts: initialScripts,
123-
onLoadStop: (webviewController, url) async {
124-
writeScripts().whenComplete(() => loading(false));
124+
onLoadStop: (webController, url) async {
125+
await writeScripts().catchError((_) => null);
126+
await focusWebview().catchError((_) => null);
127+
await loading(false).catchError((_) => null);
125128
},
126-
onTitleChanged: (webviewController, title) async {
129+
onTitleChanged: (webController, title) async {
127130
titling(title);
128131
},
129-
onReceivedError: (webviewController, request, error) async {
132+
onReceivedError: (webController, request, error) async {
130133
loading(false);
131134
},
132-
onUpdateVisitedHistory: (controller, url, isReload) async {
133-
await Future.delayed(Duration(milliseconds: 200));
134-
canPop.value = !await controller.canGoBack();
135-
},
136-
shouldOverrideUrlLoading: (webviewController, action) async {
135+
shouldOverrideUrlLoading: (webController, action) async {
137136
final url = action.request.url;
138137

139138
if (!url.bv) {
@@ -147,19 +146,24 @@ class PageWebviewScaffoldState extends State<PageWebviewScaffold> {
147146

148147
return NavigationActionPolicy.ALLOW;
149148
},
150-
onPermissionRequest: (webviewController, request) async {
149+
onUpdateVisitedHistory: (webController, url, _) async {
150+
await Future.delayed(Duration(milliseconds: 100));
151+
canPop.value = !await webController.canGoBack();
152+
},
153+
onPermissionRequest: (webController, request) async {
151154
return PermissionResponse(
152155
resources: request.resources,
153156
action: PermissionResponseAction.GRANT,
154157
);
155158
},
156-
onProgressChanged: (webviewController, progress) async {
159+
onProgressChanged: (webController, progress) async {
157160
if (progress == 100) {
158-
loading(false);
161+
await Future.delayed(Duration(milliseconds: 100));
162+
canPop.value = !await webController.canGoBack();
159163
}
160164
},
161-
onWebViewCreated: (webviewController) async {
162-
controller.webviewController = webviewController;
165+
onWebViewCreated: (webController) async {
166+
controller.webviewController = webController;
163167
controller.webChannelController.createScriptHandlers(
164168
controller,
165169
);

lib/pages/webview/controller.dart

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,14 @@ class PageWebviewController extends GetxController {
2929
late final webviewMeta = RxWebviewMeta(Get.arguments);
3030
late final languageCode = localeController.languageCode;
3131
late final inAppWebViewSettings = InAppWebViewSettings(
32-
limitsNavigationsToAppBoundDomains: true,
32+
regexToAllowSyncUrlLoading: r'^(http://|https://).*',
33+
allowsBackForwardNavigationGestures: true,
34+
limitsNavigationsToAppBoundDomains: false,
3335
mediaPlaybackRequiresUserGesture: false,
36+
allowUniversalAccessFromFileURLs: false,
37+
useShouldInterceptFetchRequest: false,
38+
useShouldInterceptAjaxRequest: false,
39+
allowFileAccessFromFileURLs: false,
3440
useShouldOverrideUrlLoading: true,
3541
javaScriptEnabled: true,
3642
isInspectable: debug,
@@ -53,7 +59,7 @@ class PageWebviewController extends GetxController {
5359
webviewMeta.popup.value = false;
5460
webviewMeta.loading.value = false;
5561

56-
HardwareKeyboard.instance.addHandler(keyboarding);
62+
HardwareKeyboard.instance.addHandler(keyboard);
5763

5864
if (Platform.isAndroid) {
5965
InAppWebViewController.setWebContentsDebuggingEnabled(debug);
@@ -70,7 +76,7 @@ class PageWebviewController extends GetxController {
7076
webChannelController.removeScriptHandlers(this);
7177
}
7278

73-
HardwareKeyboard.instance.removeHandler(keyboarding);
79+
HardwareKeyboard.instance.removeHandler(keyboard);
7480

7581
webviewMeta.loading.value = false;
7682
webviewController = null;
@@ -198,6 +204,11 @@ class PageWebviewController extends GetxController {
198204
}
199205
}
200206

207+
// focusWebview
208+
Future<void> focusWebview() async {
209+
await webviewController?.requestFocus();
210+
}
211+
201212
// loading
202213
Future<void> loading([bool? loading = false]) async {
203214
webviewMeta.loading.value = loading == true;
@@ -235,8 +246,8 @@ class PageWebviewController extends GetxController {
235246
}
236247
}
237248

238-
// keyboarding
239-
bool keyboarding(KeyEvent event) {
249+
// keyboard
250+
bool keyboard(KeyEvent event) {
240251
if (event.logicalKey != LogicalKeyboardKey.goBack) {
241252
return false;
242253
}

pubspec.lock

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -386,18 +386,18 @@ packages:
386386
dependency: "direct main"
387387
description:
388388
name: flutter_inappwebview
389-
sha256: "80092d13d3e29b6227e25b67973c67c7210bd5e35c4b747ca908e31eb71a46d5"
389+
sha256: a8f5c9dd300a8cc7fde7bb902ae57febe95e9269424e4d08d5a1a56214e1e6ff
390390
url: "https://pub.flutter-io.cn"
391391
source: hosted
392-
version: "6.1.5"
392+
version: "6.2.0-beta.2"
393393
flutter_inappwebview_android:
394394
dependency: transitive
395395
description:
396396
name: flutter_inappwebview_android
397-
sha256: "62557c15a5c2db5d195cb3892aab74fcaec266d7b86d59a6f0027abd672cddba"
397+
sha256: "2427e89d9c7b00cc756f800932d7ab8f3272d3fbc71544e1aedb3dbc17dae074"
398398
url: "https://pub.flutter-io.cn"
399399
source: hosted
400-
version: "1.1.3"
400+
version: "1.2.0-beta.2"
401401
flutter_inappwebview_internal_annotations:
402402
dependency: transitive
403403
description:
@@ -410,42 +410,42 @@ packages:
410410
dependency: transitive
411411
description:
412412
name: flutter_inappwebview_ios
413-
sha256: "5818cf9b26cf0cbb0f62ff50772217d41ea8d3d9cc00279c45f8aabaa1b4025d"
413+
sha256: "7ff65d7408e453f9a4ff38f74673aeec8cae824cba8276b4b77350262bfe356a"
414414
url: "https://pub.flutter-io.cn"
415415
source: hosted
416-
version: "1.1.2"
416+
version: "1.2.0-beta.2"
417417
flutter_inappwebview_macos:
418418
dependency: transitive
419419
description:
420420
name: flutter_inappwebview_macos
421-
sha256: c1fbb86af1a3738e3541364d7d1866315ffb0468a1a77e34198c9be571287da1
421+
sha256: be8b8ab0100c94ec9fc079a4d48b2bc8dd1a8b4c2647da34f1d3dae93cd5f88a
422422
url: "https://pub.flutter-io.cn"
423423
source: hosted
424-
version: "1.1.2"
424+
version: "1.2.0-beta.2"
425425
flutter_inappwebview_platform_interface:
426426
dependency: transitive
427427
description:
428428
name: flutter_inappwebview_platform_interface
429-
sha256: cf5323e194096b6ede7a1ca808c3e0a078e4b33cc3f6338977d75b4024ba2500
429+
sha256: "2c99bf767900ba029d825bc6f494d30169ee83cdaa038d86e85fe70571d0a655"
430430
url: "https://pub.flutter-io.cn"
431431
source: hosted
432-
version: "1.3.0+1"
432+
version: "1.4.0-beta.2"
433433
flutter_inappwebview_web:
434434
dependency: transitive
435435
description:
436436
name: flutter_inappwebview_web
437-
sha256: "55f89c83b0a0d3b7893306b3bb545ba4770a4df018204917148ebb42dc14a598"
437+
sha256: "6c4bb61ea9d52e51d79ea23da27c928d0430873c04ad380df39c1ef442b11f4e"
438438
url: "https://pub.flutter-io.cn"
439439
source: hosted
440-
version: "1.1.2"
440+
version: "1.2.0-beta.2"
441441
flutter_inappwebview_windows:
442442
dependency: transitive
443443
description:
444444
name: flutter_inappwebview_windows
445-
sha256: "8b4d3a46078a2cdc636c4a3d10d10f2a16882f6be607962dbfff8874d1642055"
445+
sha256: "0ff241f814b7caff63b9632cf858b6d3d9c35758040620a9745e5f6e9dd94d74"
446446
url: "https://pub.flutter-io.cn"
447447
source: hosted
448-
version: "0.6.0"
448+
version: "0.7.0-beta.2"
449449
flutter_launcher_icons:
450450
dependency: "direct main"
451451
description:
@@ -1154,6 +1154,22 @@ packages:
11541154
url: "https://pub.flutter-io.cn"
11551155
source: hosted
11561156
version: "1.1.1"
1157+
web_socket:
1158+
dependency: transitive
1159+
description:
1160+
name: web_socket
1161+
sha256: "34d64019aa8e36bf9842ac014bb5d2f5586ca73df5e4d9bf5c936975cae6982c"
1162+
url: "https://pub.flutter-io.cn"
1163+
source: hosted
1164+
version: "1.0.1"
1165+
web_socket_channel:
1166+
dependency: "direct main"
1167+
description:
1168+
name: web_socket_channel
1169+
sha256: d645757fb0f4773d602444000a8131ff5d48c9e47adfe9772652dd1a4f2d45c8
1170+
url: "https://pub.flutter-io.cn"
1171+
source: hosted
1172+
version: "3.0.3"
11571173
win32:
11581174
dependency: transitive
11591175
description:

pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ dependencies:
1616
sdk: flutter
1717

1818
get: ^5.0.0-release-candidate-9.3.2
19+
flutter_inappwebview: ^6.2.0-beta.2
1920

2021
internet_connection_checker: ^3.0.1
2122
flutter_ringtone_player: ^5.0.0-dev.1
2223
flutter_launcher_icons: ^0.14.4
23-
flutter_inappwebview: ^6.1.5
2424
responsive_framework: ^1.5.1
2525
permission_handler: ^12.0.1
26+
web_socket_channel: ^3.0.3
2627
connectivity_plus: ^6.1.4
2728
cupertino_icons: ^1.0.8
2829
mobile_scanner: ^7.0.1

0 commit comments

Comments
 (0)