117 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Dart
		
	
	
	
		
		
			
		
	
	
			117 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Dart
		
	
	
	
|  | import 'package:flutter/material.dart'; | ||
|  | import 'package:flutter_inappwebview/flutter_inappwebview.dart'; | ||
|  | import 'package:web_synchronization_tool/web_widget.dart'; | ||
|  | 
 | ||
|  | import 'JavaScriptString.dart'; | ||
|  | 
 | ||
|  | class MainPage extends StatefulWidget { | ||
|  |   const MainPage({super.key}); | ||
|  | 
 | ||
|  |   @override | ||
|  |   State<MainPage> createState() => _MainPageState(); | ||
|  | } | ||
|  | 
 | ||
|  | class _MainPageState extends State<MainPage> { | ||
|  | 
 | ||
|  |   late InAppWebViewController mainController; | ||
|  |   late InAppWebViewController controller; | ||
|  | 
 | ||
|  |   @override | ||
|  |   void initState() { | ||
|  |     super.initState(); | ||
|  | 
 | ||
|  | 
 | ||
|  |   } | ||
|  | 
 | ||
|  |   /// 注入点击监听
 | ||
|  |   addClickEventJS(){ | ||
|  | 
 | ||
|  |     final clickJsUS = UserScript(groupName: 'click',source: JavaScriptString.clickEventkJSString, injectionTime: UserScriptInjectionTime.AT_DOCUMENT_START); | ||
|  | 
 | ||
|  |     mainController.addUserScript(userScript: clickJsUS); | ||
|  | 
 | ||
|  |     mainController.addJavaScriptHandler(handlerName: 'Click', callback: (args){ | ||
|  |       controller.evaluateJavascript(source: JavaScriptString.clickJSString(args.first, args.last) ); | ||
|  |     }); | ||
|  | 
 | ||
|  |   } | ||
|  | 
 | ||
|  |   @override | ||
|  |   Widget build(BuildContext context) { | ||
|  |     return Scaffold( | ||
|  |       body: Column( | ||
|  |         children: [ | ||
|  |           Container( | ||
|  |             height: 50, | ||
|  |             color: Colors.white, | ||
|  |             padding: const EdgeInsets.symmetric(horizontal: 50), | ||
|  |             child: Row( | ||
|  |               children: [ | ||
|  |                 TextButton( | ||
|  |                     onPressed: () { | ||
|  |                       controller.evaluateJavascript(source: JavaScriptString.clickJSString(600, 280) ); | ||
|  |                     }, | ||
|  |                     child: const Text('模拟点击测试')), | ||
|  |                 TextButton( | ||
|  |                     onPressed: () { | ||
|  |                       mainController.evaluateJavascript(source: JavaScriptString.inputJsString(45) ); | ||
|  |                       controller.evaluateJavascript(source: JavaScriptString.inputJsString(45) ); | ||
|  |                     }, | ||
|  |                     child: const Text('模拟输入测试')), | ||
|  |                 const SizedBox( | ||
|  |                   width: 50, | ||
|  |                   child: TextField( | ||
|  |                     keyboardType: TextInputType.number, | ||
|  |                     decoration: InputDecoration(prefixText: '网页数量'), | ||
|  |                   ), | ||
|  |                 ) | ||
|  |               ], | ||
|  |             ), | ||
|  |           ), | ||
|  |           Expanded(child: pageViewWidget()), | ||
|  |         ], | ||
|  |       ), | ||
|  |     ); | ||
|  |   } | ||
|  | 
 | ||
|  |   Widget pageViewWidget() { | ||
|  |     return PageView( | ||
|  |       children: <Widget>[ | ||
|  |         KeepAlivePage( | ||
|  |           child: WebWidget(controlerCallBack: (_mainController) { | ||
|  |             mainController = _mainController; | ||
|  |            | ||
|  |             addClickEventJS(); | ||
|  |           }), | ||
|  |         ), | ||
|  |         KeepAlivePage( | ||
|  |           child: WebWidget(controlerCallBack: (_controller) { | ||
|  |             controller = _controller; | ||
|  |           }), | ||
|  |         ) | ||
|  |       ], | ||
|  |     ); | ||
|  |   } | ||
|  | 
 | ||
|  | } | ||
|  | 
 | ||
|  | class KeepAlivePage extends StatefulWidget { | ||
|  |   final Widget child; | ||
|  | 
 | ||
|  |   KeepAlivePage({super.key , required this.child}); | ||
|  | 
 | ||
|  |   @override | ||
|  |   _KeepAlivePageState createState() => _KeepAlivePageState(); | ||
|  | } | ||
|  | 
 | ||
|  | class _KeepAlivePageState extends State<KeepAlivePage> with AutomaticKeepAliveClientMixin { | ||
|  |   @override | ||
|  |   bool get wantKeepAlive => true; | ||
|  | 
 | ||
|  |   @override | ||
|  |   Widget build(BuildContext context) { | ||
|  |     super.build(context); | ||
|  |     return widget.child; | ||
|  |   } | ||
|  | } |