109 lines
3.7 KiB
Dart
109 lines
3.7 KiB
Dart
|
|
import 'package:flutter/material.dart';
|
||
|
|
import 'package:flutter_map/flutter_map.dart';
|
||
|
|
import 'package:latlong2/latlong.dart';
|
||
|
|
|
||
|
|
import '../../../../core/di/injection.dart';
|
||
|
|
import '../../../../core/localization/app_localizations.dart';
|
||
|
|
import '../../../../core/network/mqtt/domain/entities/mower_obstacle_entity.dart';
|
||
|
|
import '../../../../core/network/mqtt/domain/repositories/mower_realtime_repository.dart';
|
||
|
|
|
||
|
|
/// 三处业务地图共用的实时感知层,生命周期与地图一致。
|
||
|
|
class MowerObstacleLayer extends StatefulWidget {
|
||
|
|
const MowerObstacleLayer({super.key, required this.deviceSn});
|
||
|
|
|
||
|
|
final String deviceSn;
|
||
|
|
|
||
|
|
@override
|
||
|
|
State<MowerObstacleLayer> createState() => _MowerObstacleLayerState();
|
||
|
|
}
|
||
|
|
|
||
|
|
class _MowerObstacleLayerState extends State<MowerObstacleLayer> {
|
||
|
|
late Stream<List<MowerObstacleEntity>> _stream = _watchObstacles();
|
||
|
|
|
||
|
|
Stream<List<MowerObstacleEntity>> _watchObstacles() {
|
||
|
|
final sn = widget.deviceSn.trim();
|
||
|
|
return sn.isEmpty
|
||
|
|
? const Stream.empty()
|
||
|
|
: sl<MowerRealtimeRepository>().watchObstacles(sn);
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
void didUpdateWidget(covariant MowerObstacleLayer oldWidget) {
|
||
|
|
super.didUpdateWidget(oldWidget);
|
||
|
|
if (oldWidget.deviceSn.trim() != widget.deviceSn.trim()) {
|
||
|
|
_stream = _watchObstacles();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
Widget build(BuildContext context) {
|
||
|
|
final l10n = AppLocalizations.of(context);
|
||
|
|
return StreamBuilder<List<MowerObstacleEntity>>(
|
||
|
|
// 换设备后丢弃旧快照,等待新设备的首帧。
|
||
|
|
key: ValueKey(widget.deviceSn.trim()),
|
||
|
|
stream: _stream,
|
||
|
|
builder: (context, snapshot) {
|
||
|
|
final objects = snapshot.hasError
|
||
|
|
? const <MowerObstacleEntity>[]
|
||
|
|
: snapshot.data ?? const <MowerObstacleEntity>[];
|
||
|
|
return MarkerLayer(
|
||
|
|
markers: [
|
||
|
|
for (final object in objects)
|
||
|
|
Marker(
|
||
|
|
point: LatLng(object.latitude, object.longitude),
|
||
|
|
width: 36,
|
||
|
|
height: 36,
|
||
|
|
child: _marker(object, l10n),
|
||
|
|
),
|
||
|
|
],
|
||
|
|
);
|
||
|
|
},
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
Widget _marker(MowerObstacleEntity object, AppLocalizations l10n) {
|
||
|
|
final (icon, color) = switch (object.classId) {
|
||
|
|
0 => (Icons.directions_bus, const Color(0xFF7C3AED)),
|
||
|
|
1 => (Icons.directions_car, const Color(0xFF1677FF)),
|
||
|
|
2 => (Icons.two_wheeler, const Color(0xFFEA580C)),
|
||
|
|
3 => (Icons.person, const Color(0xFFE11D48)),
|
||
|
|
4 => (Icons.local_shipping, const Color(0xFF0D9488)),
|
||
|
|
_ => (Icons.warning_amber_rounded, const Color(0xFF64748B)),
|
||
|
|
};
|
||
|
|
final category = l10n.translate('perception_obstacle.${object.categoryKey}');
|
||
|
|
final title = object.trackId == null
|
||
|
|
? category
|
||
|
|
: '$category #${object.trackId}';
|
||
|
|
final details = <String>[
|
||
|
|
title,
|
||
|
|
if (object.distance != null && object.distance! >= 0)
|
||
|
|
l10n.translate('perception_obstacle.distance', {
|
||
|
|
'value': object.distance!.toStringAsFixed(1),
|
||
|
|
}),
|
||
|
|
if (object.confidence != null &&
|
||
|
|
object.confidence! >= 0 && object.confidence! <= 1)
|
||
|
|
l10n.translate('perception_obstacle.confidence', {
|
||
|
|
'value': (object.confidence! * 100).toStringAsFixed(0),
|
||
|
|
}),
|
||
|
|
];
|
||
|
|
return Tooltip(
|
||
|
|
message: details.join('\n'),
|
||
|
|
triggerMode: TooltipTriggerMode.tap,
|
||
|
|
child: Semantics(
|
||
|
|
label: title,
|
||
|
|
child: Container(
|
||
|
|
decoration: BoxDecoration(
|
||
|
|
color: Colors.white,
|
||
|
|
shape: BoxShape.circle,
|
||
|
|
border: Border.all(color: color, width: 2),
|
||
|
|
boxShadow: const [
|
||
|
|
BoxShadow(color: Colors.black26, blurRadius: 3),
|
||
|
|
],
|
||
|
|
),
|
||
|
|
child: Icon(icon, color: color, size: 24),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|