import 'dart:async'; import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; import 'package:webview_flutter/webview_flutter.dart'; import '../../common/Global.dart'; import '../../network/RequestCenter.dart'; class CommonlyWebPage extends StatefulWidget { String? url; final String? title; final bool isLocalUrl; CommonlyWebPage({super.key,this.url, this.isLocalUrl = false, this.title}); @override State createState() => CommonlyWebPageState(); } class CommonlyWebPageState extends State { StreamSubscription? subscription; String Token = ""; WebViewController? _webViewController; @override void initState() { // TODO: implement initState super.initState(); } @override void dispose() { //subscription?.cancel(); // TODO: implement dispose super.dispose(); } // 获取原生的值 invokeNativeMethod(String method, Map map) async { dynamic args; try { args = await Global.method.invokeMethod(method, map); } on PlatformException catch (e) {} } @override Widget build(BuildContext context) { return Scaffold(appBar: _buildAppbar(), body: _buildBody()); } _buildAppbar() { return AppBar( brightness: Brightness.light, iconTheme: IconThemeData(color: Colors.black), flexibleSpace: Container( decoration: BoxDecoration( color: Color(0xFFBB72E0), // gradient: LinearGradient(colors: [ // Color(0xFF52B5FF), // Color(0xFF8869F2), // ], begin: Alignment.centerLeft, end: Alignment.centerRight), ), ), centerTitle: true, elevation: 0, leading: IconButton( color: Colors.white, icon: ImageIcon(AssetImage('assets/images/ic_return.png')), onPressed: () { Navigator.pop(context); }), title: Text( widget.title!, style: TextStyle( color: Colors.white, fontSize: 17, fontWeight: FontWeight.w600, ), ), actions: []); } _buildBody() { return Column( children: [ SizedBox( height: 1, width: double.infinity, child: const DecoratedBox(decoration: BoxDecoration(color: Color(0xFFEEEEEE))), ), Expanded( flex: 1, child: WebView( initialUrl: widget.isLocalUrl ? Uri.dataFromString(widget.url!, mimeType: 'text/html', encoding: Encoding.getByName('utf-8')).toString() : widget.url, javascriptMode: JavascriptMode.unrestricted, javascriptChannels: [ JavascriptChannel( name: 'close', //handleName onMessageReceived: (JavascriptMessage message) { Navigator.pop(context); }), JavascriptChannel( name: 'getUserInfos', //handleName onMessageReceived: (JavascriptMessage message) {}), JavascriptChannel( name: 'Toast', //handleName onMessageReceived: (JavascriptMessage message) { print(message.message); //接收到js返回的数据 //收到js返回并作出应答 String callbackname = message.message; String data = "收到消息调用了"; String script = "$callbackname($data)"; }), ].toSet(), onWebViewCreated: (WebViewController controller) { _webViewController = controller; if (widget.isLocalUrl) { _loadHtmlAssets(controller); } else { controller.loadUrl(widget.url!); } controller.canGoBack().then((value) => debugPrint(value.toString())); controller.canGoForward().then((value) => debugPrint(value.toString())); controller.currentUrl().then((value) => debugPrint(value)); }, onPageFinished: (String value) { _webViewController!.evaluateJavascript('document.title').then((title) => debugPrint(title)); }, ), ) ], ); } //加载本地文件 _loadHtmlAssets(WebViewController controller) async { String htmlPath = await rootBundle.loadString(widget.url! + "?" + Token); controller.loadUrl(Uri.dataFromString(htmlPath, mimeType: 'text/html', encoding: Encoding.getByName('utf-8')).toString()); } }