80 lines
2.1 KiB
Dart
80 lines
2.1 KiB
Dart
import 'dart:convert';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
|
|
import 'package:maibu_satabot_v2/core/router/route_paths.dart';
|
|
import 'package:webview_flutter/webview_flutter.dart';
|
|
|
|
class DjPage extends StatefulWidget {
|
|
const DjPage({super.key});
|
|
|
|
@override
|
|
State<DjPage> createState() => _DjPageState();
|
|
}
|
|
|
|
class _DjPageState extends State<DjPage> {
|
|
late final WebViewController controller;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
|
|
final url = "https://pvs.satabot.com/?platform=flutter&t=${DateTime.now().millisecondsSinceEpoch}";
|
|
|
|
controller = WebViewController()
|
|
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
|
..addJavaScriptChannel(
|
|
'MapChannel',
|
|
onMessageReceived: (JavaScriptMessage message) {
|
|
_handleJsMessage(message.message);
|
|
},
|
|
)
|
|
..loadRequest(Uri.parse(url));
|
|
}
|
|
|
|
/// 处理 H5 返回的数据
|
|
void _handleJsMessage(String msg) {
|
|
try {
|
|
final data = jsonDecode(msg);
|
|
|
|
final action = data["action"];
|
|
final page = data["page"];
|
|
|
|
if (action == "navigate") {
|
|
if (page == "gaode") {
|
|
_openGaode();
|
|
}
|
|
|
|
if (page == "StatusFragment") {
|
|
context.go(RoutePaths.routePlan);
|
|
}
|
|
}
|
|
} catch (e) {
|
|
debugPrint("解析H5消息失败: $e");
|
|
}
|
|
}
|
|
|
|
/// 打开高德地图页面
|
|
void _openGaode() {
|
|
context.go(RoutePaths.routePlan);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
leading: IconButton(
|
|
icon: const Icon(Icons.arrow_back, color: Colors.white),
|
|
onPressed: () {
|
|
context.go(RoutePaths.home);
|
|
},
|
|
),
|
|
title: Text(AppLocalizations.of(context).translate('route_planning.title'), style: const TextStyle(color: Colors.white)),
|
|
backgroundColor: const Color(0xFF1677FF),
|
|
foregroundColor: Colors.white,
|
|
),
|
|
body: WebViewWidget(controller: controller),
|
|
);
|
|
}
|
|
}
|