v1.0.1
This commit is contained in:
58
lib/modules/common/webview/webview_controller.dart
Normal file
58
lib/modules/common/webview/webview_controller.dart
Normal file
@@ -0,0 +1,58 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_dmzj/app/app_color.dart';
|
||||
import 'package:flutter_dmzj/app/controller/base_controller.dart';
|
||||
import 'package:flutter_dmzj/app/log.dart';
|
||||
import 'package:flutter_dmzj/services/user_service.dart';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
import 'package:webview_flutter/webview_flutter.dart';
|
||||
|
||||
class WebViewPageController extends BaseController {
|
||||
final String url;
|
||||
WebViewPageController(this.url);
|
||||
final WebViewController webViewController = WebViewController();
|
||||
var title = "加载中".obs;
|
||||
@override
|
||||
void onInit() {
|
||||
initWebView();
|
||||
super.onInit();
|
||||
}
|
||||
|
||||
void initWebView() async {
|
||||
webViewController.setJavaScriptMode(JavaScriptMode.unrestricted);
|
||||
|
||||
webViewController.setBackgroundColor(
|
||||
Get.isDarkMode ? Colors.black : AppColor.backgroundColor);
|
||||
webViewController.setNavigationDelegate(
|
||||
NavigationDelegate(
|
||||
onPageStarted: (String url) {
|
||||
pageLoadding.value = true;
|
||||
},
|
||||
onPageFinished: (String url) async {
|
||||
pageLoadding.value = false;
|
||||
title.value = (await webViewController.getTitle()) ?? "";
|
||||
},
|
||||
onNavigationRequest: (NavigationRequest request) {
|
||||
var uri = Uri.parse(request.url);
|
||||
Log.d(request.url);
|
||||
if (uri.scheme == "https" || uri.scheme == "http") {
|
||||
return NavigationDecision.navigate;
|
||||
}
|
||||
|
||||
return NavigationDecision.prevent;
|
||||
},
|
||||
),
|
||||
);
|
||||
webViewController.loadRequest(Uri.parse(url), headers: {
|
||||
"Cookie": UserService.instance.userProfile.value?.cookieVal ?? "",
|
||||
});
|
||||
|
||||
/// TODO 无法加载Mixed Content
|
||||
/// 19年的问题了,Flutter还没解决...
|
||||
/// https://github.com/flutter/flutter/issues/43595
|
||||
}
|
||||
|
||||
void refreshWeb() {
|
||||
webViewController.reload();
|
||||
}
|
||||
}
|
||||
133
lib/modules/common/webview/webview_page.dart
Normal file
133
lib/modules/common/webview/webview_page.dart
Normal file
@@ -0,0 +1,133 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_dmzj/app/utils.dart';
|
||||
import 'package:flutter_dmzj/modules/common/webview/webview_controller.dart';
|
||||
import 'package:flutter_dmzj/widgets/status/app_error_widget.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:url_launcher/url_launcher_string.dart';
|
||||
import 'package:webview_flutter/webview_flutter.dart';
|
||||
|
||||
class WebViewPage extends StatelessWidget {
|
||||
final String url;
|
||||
final WebViewPageController controller;
|
||||
WebViewPage({required this.url, Key? key})
|
||||
: controller = Get.put(
|
||||
WebViewPageController(url),
|
||||
tag: DateTime.now().millisecondsSinceEpoch.toString(),
|
||||
),
|
||||
super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Obx(() => Text(controller.title.value)),
|
||||
),
|
||||
body: Stack(
|
||||
children: [
|
||||
Obx(
|
||||
() => Offstage(
|
||||
offstage: controller.pageLoadding.value,
|
||||
child: WebViewWidget(
|
||||
controller: controller.webViewController,
|
||||
),
|
||||
),
|
||||
),
|
||||
Obx(
|
||||
() => Offstage(
|
||||
offstage: !controller.pageError.value,
|
||||
child: AppErrorWidget(
|
||||
errorMsg: controller.errorMsg.value,
|
||||
onRefresh: () => controller.refreshWeb(),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
bottomNavigationBar: Stack(
|
||||
children: [
|
||||
BottomAppBar(
|
||||
child: SizedBox(
|
||||
height: 56,
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: IconButton(
|
||||
onPressed: () {
|
||||
controller.webViewController.goBack();
|
||||
},
|
||||
icon: const Icon(
|
||||
Icons.chevron_left,
|
||||
),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: IconButton(
|
||||
onPressed: () {
|
||||
controller.webViewController.reload();
|
||||
},
|
||||
icon: const Icon(
|
||||
Icons.refresh,
|
||||
),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: IconButton(
|
||||
onPressed: () {
|
||||
controller.webViewController.goForward();
|
||||
},
|
||||
icon: const Icon(
|
||||
Icons.chevron_right,
|
||||
),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: IconButton(
|
||||
onPressed: () async {
|
||||
Utils.share(
|
||||
(await controller.webViewController.currentUrl())
|
||||
.toString(),
|
||||
);
|
||||
},
|
||||
icon: const Icon(
|
||||
Icons.share,
|
||||
size: 20,
|
||||
),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: IconButton(
|
||||
onPressed: () async {
|
||||
var url =
|
||||
await controller.webViewController.currentUrl();
|
||||
if (url != null) {
|
||||
launchUrlString(url,
|
||||
mode: LaunchMode.externalApplication);
|
||||
}
|
||||
},
|
||||
icon: const Icon(
|
||||
Icons.open_in_browser,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
Positioned.fill(
|
||||
top: 0,
|
||||
left: 0,
|
||||
child: Obx(
|
||||
() => Offstage(
|
||||
offstage: !controller.pageLoadding.value,
|
||||
child: Container(
|
||||
alignment: Alignment.topLeft,
|
||||
child: const LinearProgressIndicator(),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user