Skip to content

Commit

Permalink
feat: update macOS and windows webview implement to `flutter_inappweb…
Browse files Browse the repository at this point in the history
…view`
  • Loading branch information
abc873693 committed Dec 19, 2024
1 parent 146f894 commit 637136c
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 72 deletions.
64 changes: 5 additions & 59 deletions lib/pages/guide/admission_guide_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import 'dart:io';
import 'package:ap_common/ap_common.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:nsysu_ap/config/constants.dart';
import 'package:webview_windows/webview_windows.dart';

class AdmissionGuidePage extends StatefulWidget {
@override
Expand All @@ -17,15 +15,11 @@ class _AdmissionGuidePageState extends State<AdmissionGuidePage> {
late ApLocalizations ap;

InAppWebViewController? webViewController;
final WebviewController _windowsController = WebviewController();

@override
void initState() {
AnalyticsUtil.instance
.setCurrentScreen('AdmissionGuidePage', 'admission_guide_page.dart');
if (!kIsWeb && Platform.isWindows) {
initWindowsPlatformState();
}
super.initState();
}

Expand All @@ -44,8 +38,6 @@ class _AdmissionGuidePageState extends State<AdmissionGuidePage> {
if (await webViewController?.canGoBack() ?? false) {
webViewController?.goBack();
}
} else if (Platform.isWindows) {
_windowsController.goBack();
}
},
),
Expand All @@ -56,16 +48,18 @@ class _AdmissionGuidePageState extends State<AdmissionGuidePage> {
if (await webViewController?.canGoForward() ?? false) {
webViewController?.goForward();
}
} else if (Platform.isWindows) {
_windowsController.goForward();
}
},
),
],
),
body: Builder(
builder: (BuildContext context) {
if (!kIsWeb && (Platform.isAndroid || Platform.isIOS)) {
if (!kIsWeb &&
(Platform.isAndroid ||
Platform.isIOS ||
Platform.isMacOS ||
Platform.isWindows)) {
return InAppWebView(
initialUrlRequest: URLRequest(
url: WebUri(Constants.admissionGuideUrl),
Expand All @@ -75,10 +69,6 @@ class _AdmissionGuidePageState extends State<AdmissionGuidePage> {
//_windowsController.complete(webViewController);
},
);
} else if (Platform.isWindows) {
return Webview(
_windowsController,
);
} else {
return HintContent(
icon: ApIcon.apps,
Expand All @@ -89,48 +79,4 @@ class _AdmissionGuidePageState extends State<AdmissionGuidePage> {
),
);
}

Future<void> initWindowsPlatformState() async {
try {
await _windowsController.initialize();
_windowsController.url.listen((String url) {
if (kDebugMode) {
print(url);
}
});

await _windowsController.setBackgroundColor(Colors.transparent);
await _windowsController
.setPopupWindowPolicy(WebviewPopupWindowPolicy.deny);
await _windowsController.loadUrl(Constants.admissionGuideUrl);

if (!mounted) return;
setState(() {});
} on PlatformException catch (e) {
WidgetsBinding.instance.addPostFrameCallback((_) {
showDialog(
context: context,
builder: (_) => AlertDialog(
title: const Text('Error'),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Code: ${e.code}'),
Text('Message: ${e.message}'),
],
),
actions: <Widget>[
TextButton(
child: const Text('Continue'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
),
);
});
}
}
}
1 change: 1 addition & 0 deletions lib/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ class HomePageState extends State<HomePage> {
if (kIsWeb ||
Platform.isAndroid ||
Platform.isIOS ||
Platform.isMacOS ||
Platform.isWindows) {
_openPage(
AdmissionGuidePage(),
Expand Down
8 changes: 0 additions & 8 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1738,14 +1738,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.1"
webview_windows:
dependency: "direct main"
description:
name: webview_windows
sha256: "47fcad5875a45db29dbb5c9e6709bf5c88dcc429049872701343f91ed7255730"
url: "https://pub.dev"
source: hosted
version: "0.4.0"
win32:
dependency: transitive
description:
Expand Down
1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ dependencies:
cookie_jar: ^4.0.3
flutter_inappwebview: ^6.1.5
desktop_webview_window: ^0.2.3
webview_windows: ^0.4.0
timeago: ^3.0.2
google_sign_in_dartio: ^0.3.0
json_annotation: ^4.6.0
Expand Down
3 changes: 0 additions & 3 deletions windows/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <printing/printing_plugin.h>
#include <share_plus/share_plus_windows_plugin_c_api.h>
#include <url_launcher_windows/url_launcher_windows.h>
#include <webview_windows/webview_windows_plugin.h>

void RegisterPlugins(flutter::PluginRegistry* registry) {
DesktopWebviewWindowPluginRegisterWithRegistrar(
Expand All @@ -33,6 +32,4 @@ void RegisterPlugins(flutter::PluginRegistry* registry) {
registry->GetRegistrarForPlugin("SharePlusWindowsPluginCApi"));
UrlLauncherWindowsRegisterWithRegistrar(
registry->GetRegistrarForPlugin("UrlLauncherWindows"));
WebviewWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("WebviewWindowsPlugin"));
}
1 change: 0 additions & 1 deletion windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ list(APPEND FLUTTER_PLUGIN_LIST
printing
share_plus
url_launcher_windows
webview_windows
)

list(APPEND FLUTTER_FFI_PLUGIN_LIST
Expand Down

0 comments on commit 637136c

Please sign in to comment.