Skip to content

Commit

Permalink
fix #884 android login block
Browse files Browse the repository at this point in the history
  • Loading branch information
CarGuo committed Jun 12, 2024
1 parent 567fd62 commit 4bdfe41
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 77 deletions.
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if (localPropertiesFile.exists()) {
apply from: "exported.gradle"

android {
compileSdkVersion 33
compileSdkVersion 34

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
Expand Down
106 changes: 43 additions & 63 deletions lib/page/login/login_webview.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,57 +20,14 @@ class LoginWebView extends StatefulWidget {
class _LoginWebViewState extends State<LoginWebView> {
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 <PlaybackMediaTypes>{},
);
} 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();
}

Expand All @@ -86,10 +38,10 @@ class _LoginWebViewState extends State<LoginWebView> {
return Row(children: [
Expanded(
child: Text(
widget.title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
)),
widget.title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
)),
GSYCommonOptionWidget(url: widget.url),
]);
}
Expand All @@ -109,8 +61,38 @@ class _LoginWebViewState extends State<LoginWebView> {
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(
Expand All @@ -121,11 +103,9 @@ class _LoginWebViewState extends State<LoginWebView> {
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
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),
],
),
Expand Down
32 changes: 20 additions & 12 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"
4 changes: 3 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 4bdfe41

Please sign in to comment.