web_synchronization_tool/lib/main_page.dart

146 lines
3.9 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;
bool asyncState = false;
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
WebStorageManager.instance().deleteAllData();
}
/// 注入触摸监听
addTouchendEventJS(){
final clickJsUS = UserScript(groupName: 'touchend',source: JavaScriptString.touchendEventJSString, injectionTime: UserScriptInjectionTime.AT_DOCUMENT_START);
mainController.addUserScript(userScript: clickJsUS);
mainController.addJavaScriptHandler(handlerName: 'touchend', callback: (args){
if (asyncState){
int x = double.parse(args.first.toString()).toInt();
int y = double.parse(args.last.toString()).toInt();
controller.evaluateJavascript(source: JavaScriptString.clickJSString(x, y) );
// controller.evaluateJavascript(source: JavaScriptString.touchendJsString(x, y) );
// controller.evaluateJavascript(source: JavaScriptString.getClassTouchendJsString(x, y) );
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
height: 50,
color: Colors.white,
padding: const EdgeInsets.symmetric(horizontal: 50),
child: Row(
children: [
TextButton(
onPressed: () {
controller.evaluateJavascript(source: JavaScriptString.clickJSString(50, 100) );
},
child: const Text('模拟点击测试')),
TextButton(
onPressed: () {
mainController.evaluateJavascript(source: JavaScriptString.inputJsString(45) );
controller.evaluateJavascript(source: JavaScriptString.inputJsString(45) );
},
child: const Text('模拟输入测试')),
Row(
children: [
const Text('同步'),
Switch(value: asyncState, onChanged: (value){
setState(() {
asyncState = value;
});
})
],
)
],
),
),
Expanded(child: pageViewWidget()),
],
),
);
}
Widget pageViewWidget() {
return Row(
children: <Widget>[
Expanded(
child: Container(
color: Colors.yellow,
child: KeepAlivePage(
child: WebWidget(controlerCallBack: (_mainController) {
mainController = _mainController;
addTouchendEventJS();
}),
),
),
),
Expanded(
child: Container(
color: Colors.blue,
child: 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;
}
}