142 lines
4.8 KiB
Dart
142 lines
4.8 KiB
Dart
import 'dart:collection';
|
|
import 'dart:io';
|
|
import 'package:get/get.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
|
|
import 'package:pad_start/pages/page1/qr.dart';
|
|
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
|
|
import 'package:wechat_camera_picker/wechat_camera_picker.dart';
|
|
|
|
import 'utils/pickAssets.dart';
|
|
|
|
|
|
class WebViewPage extends StatefulWidget {
|
|
const WebViewPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<WebViewPage> createState() => _WebViewPageState();
|
|
}
|
|
|
|
class _WebViewPageState extends State<WebViewPage> {
|
|
/// Base
|
|
String url = 'http://10.80.23.79:3388';
|
|
String title = 'Document';
|
|
///
|
|
final GlobalKey webViewKey = GlobalKey();
|
|
InAppWebViewController? webViewController;
|
|
InAppWebViewGroupOptions options = InAppWebViewGroupOptions(
|
|
crossPlatform: InAppWebViewOptions(
|
|
useShouldOverrideUrlLoading: true,
|
|
mediaPlaybackRequiresUserGesture: false),
|
|
android: AndroidInAppWebViewOptions(
|
|
useHybridComposition: true,
|
|
),
|
|
ios: IOSInAppWebViewOptions(
|
|
allowsInlineMediaPlayback: true,
|
|
));
|
|
late PullToRefreshController pullToRefreshController;
|
|
late ContextMenu contextMenu;
|
|
|
|
@override
|
|
initState(){
|
|
pullToRefreshController = PullToRefreshController(
|
|
options: PullToRefreshOptions(
|
|
color: Colors.blue,
|
|
),
|
|
onRefresh: () async {
|
|
if (Platform.isAndroid) {
|
|
webViewController?.reload();
|
|
} else if (Platform.isIOS) {
|
|
webViewController?.loadUrl(
|
|
urlRequest: URLRequest(url: await webViewController?.getUrl()));
|
|
}
|
|
}
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Column(
|
|
children: [
|
|
Text('Flutter Call Js', style: TextStyle(
|
|
fontSize: 38
|
|
),),
|
|
SizedBox(height: 20,),
|
|
RaisedButton(onPressed: (){
|
|
webViewController?.callAsyncJavaScript(functionBody: 'message.success("123")', arguments: {}, );
|
|
}, child: Text('message: success'),),
|
|
RaisedButton(onPressed: (){
|
|
webViewController?.callAsyncJavaScript(functionBody: 'message.warning("123")', arguments: {}, );
|
|
}, child: Text('message: warning'),),
|
|
RaisedButton(onPressed: (){
|
|
print("发送消息");
|
|
webViewController?.postWebMessage(message: WebMessage(
|
|
data: "Hello",
|
|
ports: [],
|
|
));
|
|
}, child: Text('发送消息'),),
|
|
],
|
|
),
|
|
AppBar(
|
|
title: Text(title, style: TextStyle(
|
|
fontSize: 18,
|
|
),),
|
|
centerTitle: true,
|
|
),
|
|
Expanded(
|
|
child: Container(
|
|
// InAppWebView Widget
|
|
child: InAppWebView(
|
|
key: webViewKey,
|
|
initialUrlRequest:
|
|
URLRequest(url: Uri.parse(this.url)),
|
|
initialUserScripts: UnmodifiableListView<UserScript>([]),
|
|
initialOptions: options,
|
|
pullToRefreshController: pullToRefreshController,
|
|
onWebViewCreated: (controller) async {
|
|
webViewController = controller;
|
|
webViewController?.getUrl().then((res){
|
|
print('请求URL');
|
|
print(res);
|
|
});
|
|
},
|
|
onLoadStart: (controller, url) {
|
|
setState(() {
|
|
this.url = url.toString();
|
|
});
|
|
},
|
|
// 安卓权限请求
|
|
androidOnPermissionRequest:
|
|
(controller, origin, resources) async {
|
|
return PermissionRequestResponse(
|
|
resources: resources,
|
|
action: PermissionRequestResponseAction.GRANT);
|
|
},
|
|
onConsoleMessage: (controller, consoleMessage) async {
|
|
print(consoleMessage);
|
|
print(consoleMessage.message);
|
|
print(consoleMessage.messageLevel);
|
|
if(consoleMessage.message=='pluginAssets'){
|
|
final List<AssetEntity>? result = await AssetPicker.pickAssets(context, pickerConfig: const AssetPickerConfig(
|
|
));
|
|
}
|
|
if(consoleMessage.message=='pluginQR'){
|
|
Get.to(QrWidget());
|
|
}
|
|
if(consoleMessage.message=='pluginVideo'){
|
|
final AssetEntity? entity = await CameraPicker.pickFromCamera(context, pickerConfig: const CameraPickerConfig(
|
|
enableRecording: true,
|
|
));
|
|
}
|
|
},
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|