From 4bdfe413eea89175e3f8cb36fe40b5042b4946bf Mon Sep 17 00:00:00 2001 From: guoshuyu <359369982@qq.com> Date: Wed, 12 Jun 2024 18:01:42 +0800 Subject: [PATCH] fix #884 android login block --- android/app/build.gradle | 2 +- lib/page/login/login_webview.dart | 106 ++++++++++++------------------ pubspec.lock | 32 +++++---- pubspec.yaml | 4 +- 4 files changed, 67 insertions(+), 77 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index 5f9d7ce3..43b9110c 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -15,7 +15,7 @@ if (localPropertiesFile.exists()) { apply from: "exported.gradle" android { - compileSdkVersion 33 + compileSdkVersion 34 sourceSets { main.java.srcDirs += 'src/main/kotlin' diff --git a/lib/page/login/login_webview.dart b/lib/page/login/login_webview.dart index e4296871..7ebfa56c 100644 --- a/lib/page/login/login_webview.dart +++ b/lib/page/login/login_webview.dart @@ -1,17 +1,12 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import 'package:flutter_spinkit/flutter_spinkit.dart'; import 'package:gsy_github_app_flutter/common/localization/default_localizations.dart'; import 'package:gsy_github_app_flutter/common/style/gsy_style.dart'; import 'package:gsy_github_app_flutter/widget/gsy_common_option_widget.dart'; import 'package:webview_flutter/webview_flutter.dart'; -// Import for Android features. -import 'package:webview_flutter_android/webview_flutter_android.dart'; - -// Import for iOS features. -import 'package:webview_flutter_wkwebview/webview_flutter_wkwebview.dart'; - class LoginWebView extends StatefulWidget { final String url; final String title; @@ -25,57 +20,14 @@ class LoginWebView extends StatefulWidget { class _LoginWebViewState extends State { late final WebViewController controller; + final GlobalKey webViewKey = GlobalKey(); + + InAppWebViewController? webViewController; + late final PlatformWebViewControllerCreationParams params; @override void initState() { - if (WebViewPlatform.instance is WebKitWebViewPlatform) { - params = WebKitWebViewControllerCreationParams( - allowsInlineMediaPlayback: true, - mediaTypesRequiringUserAction: const {}, - ); - } else { - params = const PlatformWebViewControllerCreationParams(); - } - - controller = WebViewController.fromPlatformCreationParams(params); - if (controller.platform is AndroidWebViewController) { - //AndroidWebViewController.enableDebugging(true); - (controller.platform as AndroidWebViewController) - .setMediaPlaybackRequiresUserGesture(true); - } - controller - ..setJavaScriptMode(JavaScriptMode.unrestricted) - ..setBackgroundColor(const Color(0x00000000)) - ..setNavigationDelegate( - NavigationDelegate( - onProgress: (int progress) { - // Update loading bar. - }, - onPageStarted: (String url) {}, - onPageFinished: (String url) { - setState(() { - isLoading = false; - }); - }, - onWebResourceError: (WebResourceError error) {}, - onNavigationRequest: (NavigationRequest request) { - if (request.url.startsWith("gsygithubapp://authed")) { - var code = Uri.parse(request.url).queryParameters["code"]; - if (kDebugMode) { - print("code $code"); - } - Navigator.of(context).pop(code); - return NavigationDecision.prevent; - } - return NavigationDecision.navigate; - }, - ), - ) - ..loadRequest(Uri.parse( - widget.url, - )); - super.initState(); } @@ -86,10 +38,10 @@ class _LoginWebViewState extends State { return Row(children: [ Expanded( child: Text( - widget.title, - maxLines: 1, - overflow: TextOverflow.ellipsis, - )), + widget.title, + maxLines: 1, + overflow: TextOverflow.ellipsis, + )), GSYCommonOptionWidget(url: widget.url), ]); } @@ -109,8 +61,38 @@ class _LoginWebViewState extends State { TextField( focusNode: focusNode, ), - WebViewWidget( - controller: controller, + InAppWebView( + key: webViewKey, + initialUrlRequest: URLRequest(url: Uri.parse(widget.url)), + onWebViewCreated: (controller) { + webViewController = controller; + webViewController?.loadUrl( + urlRequest: URLRequest(url: Uri.parse(widget.url))); + }, + onLoadStart: (controller, url) { + setState(() { + isLoading = true; + }); + }, + onLoadStop: (controller, url) async { + setState(() { + isLoading = false; + }); + if (url.toString().startsWith("gsygithubapp://authed")) { + var code = Uri.parse(url.toString()).queryParameters["code"]; + if (kDebugMode) { + print("code $code"); + } + Navigator.of(context).pop(code); + } + }, + onProgressChanged: (controller, progress) { + if (progress == 100) { + setState(() { + isLoading = false; + }); + } + }, ), if (isLoading) Center( @@ -121,11 +103,9 @@ class _LoginWebViewState extends State { child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ - SpinKitDoubleBounce( - color: Theme.of(context).primaryColor), + SpinKitDoubleBounce(color: Theme.of(context).primaryColor), Container(width: 10.0), - Text( - GSYLocalizations.i18n(context)!.loading_text, + Text(GSYLocalizations.i18n(context)!.loading_text, style: GSYConstant.middleText), ], ), diff --git a/pubspec.lock b/pubspec.lock index 53144765..8535747d 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -303,6 +303,14 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_inappwebview: + dependency: "direct main" + description: + name: flutter_inappwebview + sha256: d198297060d116b94048301ee6749cd2e7d03c1f2689783f52d210a6b7aba350 + url: "https://pub.flutter-io.cn" + source: hosted + version: "5.8.0" flutter_lints: dependency: "direct dev" description: @@ -773,10 +781,10 @@ packages: dependency: transitive description: name: plugin_platform_interface - sha256: da3fdfeccc4d4ff2da8f8c556704c08f912542c5fb3cf2233ed75372384a034d + sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02" url: "https://pub.flutter-io.cn" source: hosted - version: "2.1.6" + version: "2.1.8" pool: dependency: transitive description: @@ -1194,34 +1202,34 @@ packages: dependency: "direct main" description: name: webview_flutter - sha256: "04a0782fb058b7c71f2048935583488f4d32e9147ca403abc4e58f1de9964629" + sha256: "6869c8786d179f929144b4a1f86e09ac0eddfe475984951ea6c634774c16b522" url: "https://pub.flutter-io.cn" source: hosted - version: "4.2.3" + version: "4.8.0" webview_flutter_android: dependency: "direct main" description: name: webview_flutter_android - sha256: "9427774649fd3c8b7ff53523051395d13aed2ca355822b822e6493d79f5fc05a" + sha256: f42447ca49523f11d8f70abea55ea211b3cafe172dd7a0e7ac007bb35dd356dc url: "https://pub.flutter-io.cn" source: hosted - version: "3.10.0" + version: "3.16.4" webview_flutter_platform_interface: dependency: transitive description: name: webview_flutter_platform_interface - sha256: "6d9213c65f1060116757a7c473247c60f3f7f332cac33dc417c9e362a9a13e4f" + sha256: d937581d6e558908d7ae3dc1989c4f87b786891ab47bb9df7de548a151779d8d url: "https://pub.flutter-io.cn" source: hosted - version: "2.6.0" + version: "2.10.0" webview_flutter_wkwebview: dependency: "direct main" description: name: webview_flutter_wkwebview - sha256: ed749f94ac9e814d04a258a9255cf69cfa4cc6006ff59542aea7fb4590144972 + sha256: "7affdf9d680c015b11587181171d3cad8093e449db1f7d9f0f08f4f33d24f9a0" url: "https://pub.flutter-io.cn" source: hosted - version: "3.7.3" + version: "3.13.1" win32: dependency: transitive description: @@ -1263,5 +1271,5 @@ packages: source: hosted version: "3.1.0" sdks: - dart: ">=3.3.0-0 <4.0.0" - flutter: ">=3.13.0" + dart: ">=3.4.0 <4.0.0" + flutter: ">=3.22.0" diff --git a/pubspec.yaml b/pubspec.yaml index 31f22634..5336e843 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -37,7 +37,9 @@ dependencies: dio: 5.3.1 #fconsole: 2.2.1 path_provider: 2.1.0 - webview_flutter: 4.2.3 + webview_flutter: 4.8.0 + # 高版本目前和👆兼容有问题 + flutter_inappwebview: 5.8.0 #bezier: ^1.1.5 flare_flutter: git: