mod: webview功能完善

This commit is contained in:
guozhigq
2023-06-21 18:19:29 +08:00
parent e738d58766
commit ef2e5b1751
2 changed files with 45 additions and 12 deletions

View File

@ -16,6 +16,8 @@ class WebviewController extends GetxController {
String type = ''; String type = '';
String pageTitle = ''; String pageTitle = '';
final WebViewController controller = WebViewController(); final WebViewController controller = WebViewController();
RxInt loadProgress = 0.obs;
RxBool loadShow = true.obs;
@override @override
void onInit() { void onInit() {
@ -41,13 +43,16 @@ class WebviewController extends GetxController {
// 页面加载 // 页面加载
onProgress: (int progress) { onProgress: (int progress) {
// Update loading bar. // Update loading bar.
loadProgress.value = progress;
}, },
onPageStarted: (String url) {}, onPageStarted: (String url) {},
// 加载完成 // 加载完成
onPageFinished: (String url) async { onPageFinished: (String url) async {
if (type == 'login' && (url.startsWith( loadShow.value = false;
'https://passport.bilibili.com/web/sso/exchange_cookie') || if (type == 'login' &&
url.startsWith('https://m.bilibili.com/'))) { (url.startsWith(
'https://passport.bilibili.com/web/sso/exchange_cookie') ||
url.startsWith('https://m.bilibili.com/'))) {
try { try {
var cookies = var cookies =
await WebviewCookieManager().getCookies(HttpString.baseUrl); await WebviewCookieManager().getCookies(HttpString.baseUrl);
@ -73,7 +78,7 @@ class WebviewController extends GetxController {
}, },
onWebResourceError: (WebResourceError error) {}, onWebResourceError: (WebResourceError error) {},
onNavigationRequest: (NavigationRequest request) { onNavigationRequest: (NavigationRequest request) {
if (request.url.startsWith('https://www.youtube.com/')) { if (request.url.startsWith('bilibili//')) {
return NavigationDecision.prevent; return NavigationDecision.prevent;
} }
return NavigationDecision.navigate; return NavigationDecision.navigate;

View File

@ -16,14 +16,42 @@ class _WebviewPageState extends State<WebviewPage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
centerTitle: false, centerTitle: false,
title: Text( title: Text(
_webviewController.pageTitle, _webviewController.pageTitle,
style: Theme.of(context).textTheme.titleMedium, style: Theme.of(context).textTheme.titleMedium,
),
actions: [
IconButton(
onPressed: () {
_webviewController.controller.reload();
},
icon: const Icon(
Icons.refresh,
size: 22,
),
),
const SizedBox(width: 10)
],
), ),
), body: Column(
body: WebViewWidget(controller: _webviewController.controller), children: [
); Obx(
() => AnimatedContainer(
curve: Curves.easeInOut,
duration: const Duration(milliseconds: 350),
height: _webviewController.loadShow.value ? 4 : 0,
child: LinearProgressIndicator(
key: ValueKey(_webviewController.loadProgress),
value: _webviewController.loadProgress / 100,
),
),
),
Expanded(
child: WebViewWidget(controller: _webviewController.controller),
),
],
));
} }
} }