123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- import 'package:flutter/material.dart';
- import 'package:shared/utils/log_utils.dart';
- import 'package:url_launcher/url_launcher.dart';
- import 'dart:io';
- import 'package:webview_flutter/webview_flutter.dart';
- import 'package:widgets/my_appbar.dart';
- import 'package:cs_resources/constants/color_constants.dart';
- class WebViewPage extends StatefulWidget {
- final String? initialUrl;
- bool? showAppbar = true;
- Map? arguments = {'title': '', 'initialUrl': ''};
- List<Widget>? actions = [];
- WebViewPage({Key? key, this.showAppbar, this.initialUrl, this.arguments, this.actions}) : super(key: key);
- @override
- _WebViewPageState createState() => _WebViewPageState();
- }
- class _WebViewPageState extends State<WebViewPage> {
- WebViewController? webViewController;
- final _key = UniqueKey();
- String title = '';
- bool _showAppbar = true;
- int _stackToView = 1;
- String? _initialUrl;
- double _webViewHeight = 200;
- @override
- void initState() {
- super.initState();
- title = widget.arguments != null ? widget.arguments!['title'] : null;
- _showAppbar = widget.showAppbar ?? true;
- _initialUrl = widget.initialUrl ?? widget.arguments!['initialUrl'];
- if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
- }
- @override
- void dispose() async {
- super.dispose();
-
- webViewController?.loadUrl('about:blank');
- webViewController?.clearCache();
- }
- @override
- void didUpdateWidget(covariant WebViewPage oldWidget) {
- super.didUpdateWidget(oldWidget);
- if (widget.initialUrl != oldWidget.initialUrl) {
- super.didUpdateWidget(oldWidget);
- }
- }
-
- _invokeJavascriptChannel(BuildContext context) {
- return [
- JavascriptChannel(
- name: 'Invoke',
- onMessageReceived: (JavascriptMessage message) {
-
-
-
-
-
- },
- ),
- JavascriptChannel(
- name: 'Invoke1',
- onMessageReceived: (JavascriptMessage message) {
-
-
-
-
- },
- ),
- JavascriptChannel(
- name: "integral",
- onMessageReceived: (JavascriptMessage message) {
-
-
-
-
-
- },
- ),
- JavascriptChannel(
- name: "MessageDeal",
- onMessageReceived: (JavascriptMessage message) async {
-
-
-
-
-
-
- },
- ),
- JavascriptChannel(
- name: "callWithDict",
- onMessageReceived: (JavascriptMessage message) {
-
-
- },
- ),
- ];
- }
-
- _getWebViewHeight() async {
-
-
-
-
-
-
-
-
-
-
-
- var originalHeight = await webViewController!.runJavascriptReturningResult("document.body.offsetHeight;");
- _webViewHeight = double.parse(originalHeight);
- setState(() {
- _webViewHeight = _webViewHeight <= 0 ? 300 : _webViewHeight;
- });
-
- }
-
- Future<bool> _onWillPop() async {
- if (webViewController == null) {
- Log.d("WebView都没有加载成功,直接返回退出即可");
-
- Navigator.of(context).pop();
- return false;
- } else {
-
- if (await webViewController!.canGoBack()) {
- Log.d("点击内部链接,可以后台,调用后退");
- await webViewController!.goBack();
- return false;
- } else {
- Log.d("点击内部链接,无法后退,直接返回");
- Navigator.of(context).pop();
- return true;
- }
- }
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- backgroundColor: Colors.white,
- appBar:
- _showAppbar ? MyAppBar.appBar(context, title, backgroundColor: Colors.white, actions: widget.actions, backCallback: _onWillPop) : null,
- body: SafeArea(
- bottom: true,
- top: false,
- child: IndexedStack(
- index: _stackToView,
- children: [
- Column(
- children: [
- Expanded(
- child: WillPopScope(
- onWillPop: _onWillPop,
- child: WebView(
- key: _key,
- initialUrl: _initialUrl,
- userAgent: '',
-
- javascriptMode: JavascriptMode.unrestricted,
-
- onWebViewCreated: (WebViewController controller) {
- webViewController = controller;
- },
- onProgress: (int progress) {
-
- if (progress == 100) {
- Future.delayed(const Duration(milliseconds: 500)).then((value) => {
-
- _getWebViewHeight()
- });
- }
- },
- onPageStarted: (String url) {
-
- },
- onPageFinished: (String url) {
- Log.d('onPageFinished=====>$url');
- if (mounted) {
- setState(() {
- _stackToView = 0;
- });
- }
- },
- onWebResourceError: (WebResourceError error) {
-
- },
- gestureNavigationEnabled: true,
-
- javascriptChannels: _invokeJavascriptChannel(context).toSet(),
- navigationDelegate: (NavigationRequest request) async {
-
- if (request.url.startsWith('tel:')) {
-
- if (!await launchUrl(Uri.parse(request.url))) throw '无法启动拨打电话功能';
- return NavigationDecision.prevent;
- }
-
- return NavigationDecision.navigate;
- },
- ),
- ),
- )
- ],
- ),
- Container(
- color: Colors.white,
- child: Center(
- child: CircularProgressIndicator(
- strokeWidth: 3,
- valueColor: AlwaysStoppedAnimation(ColorConstants.appBlue),
- ),
- ),
- ),
- ],
- ),
- ));
- }
- }
|