Track, nyomkövetés hozzáadása
This commit is contained in:
@@ -4,6 +4,8 @@ import 'package:terepi_seged/pages/tracking/presentation/controllers/tracking_co
|
||||
class TrackingBinding extends Bindings {
|
||||
@override
|
||||
void dependencies() {
|
||||
Get.lazyPut(() => TrackingController());
|
||||
if (!Get.isRegistered<TrackingController>()) {
|
||||
Get.put(TrackingController(), permanent: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,313 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:latlong2/latlong.dart';
|
||||
import 'package:share_plus/share_plus.dart';
|
||||
|
||||
class TrackingController extends GetxController {}
|
||||
import '../../../../services/location_source.dart';
|
||||
import '../../../../services/phone_gps_source.dart';
|
||||
import '../../../../services/track_database.dart';
|
||||
import '../../../../services/gpx_exporter.dart';
|
||||
import '../../../../models/track.dart';
|
||||
|
||||
// ─── Foreground task handler ─────────────────────────────────────────────────
|
||||
// Ez a függvény a háttér-isolate-ban fut. Csak a meglevő pozíció-streamet
|
||||
// tartja fenn, a tényleges adatfeldolgozás a főszálban történik.
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void startTrackingCallback() {
|
||||
FlutterForegroundTask.setTaskHandler(_TrackingTaskHandler());
|
||||
}
|
||||
|
||||
class _TrackingTaskHandler extends TaskHandler {
|
||||
@override
|
||||
Future<void> onStart(DateTime timestamp, TaskStarter starter) async {}
|
||||
|
||||
@override
|
||||
void onRepeatEvent(DateTime timestamp) {
|
||||
// A Geolocator stream a főizoláton folyik tovább, itt nincs teendő.
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> onDestroy(DateTime timestamp, bool isTimeout) async {}
|
||||
}
|
||||
|
||||
// ─── TrackingController ──────────────────────────────────────────────────────
|
||||
|
||||
class TrackingController extends GetxController {
|
||||
// ── Állapot ────────────────────────────────────────────────────────────────
|
||||
final isRecording = false.obs;
|
||||
final isPaused = false.obs;
|
||||
final currentTrack = Rxn<Track>();
|
||||
|
||||
/// Aktuális session pontjai a térképhez (csak a memóriában).
|
||||
final livePoints = <LatLng>[].obs;
|
||||
|
||||
/// Aktuális sebesség [km/h].
|
||||
final currentSpeedKmh = 0.0.obs;
|
||||
|
||||
/// Megtett távolság [m] az aktuális sessionben.
|
||||
final sessionDistance = 0.0.obs;
|
||||
|
||||
/// Eltelt idő formátuma óó:pp:mm.
|
||||
final elapsedFormatted = '00:00:00'.obs;
|
||||
|
||||
/// Előtér/háttér állapot jelzése.
|
||||
final isInBackground = false.obs;
|
||||
|
||||
// ── Mentett track-ek listája ────────────────────────────────────────────────
|
||||
final savedTracks = <Track>[].obs;
|
||||
|
||||
// ── Belső állapot ──────────────────────────────────────────────────────────
|
||||
LocationSource? _source;
|
||||
StreamSubscription<SourcePosition>? _positionSub;
|
||||
Timer? _elapsedTimer;
|
||||
|
||||
TrackPoint? _lastPoint;
|
||||
DateTime? _sessionStart;
|
||||
double _accumulatedDistance = 0;
|
||||
|
||||
final _db = TrackDatabase.instance;
|
||||
final _exporter = GpxExporter();
|
||||
|
||||
// ── Inicializálás ──────────────────────────────────────────────────────────
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
_initForegroundTask();
|
||||
loadSavedTracks();
|
||||
}
|
||||
|
||||
void _initForegroundTask() {
|
||||
FlutterForegroundTask.init(
|
||||
androidNotificationOptions: AndroidNotificationOptions(
|
||||
channelId: 'tracking_channel',
|
||||
channelName: 'Nyomvonal rögzítés',
|
||||
channelDescription: 'Aktív track rögzítése folyamatban',
|
||||
channelImportance: NotificationChannelImportance.LOW,
|
||||
priority: NotificationPriority.LOW,
|
||||
),
|
||||
iosNotificationOptions: const IOSNotificationOptions(
|
||||
showNotification: true,
|
||||
playSound: false,
|
||||
),
|
||||
foregroundTaskOptions: ForegroundTaskOptions(
|
||||
eventAction: ForegroundTaskEventAction.repeat(1000),
|
||||
autoRunOnBoot: false,
|
||||
allowWakeLock: true,
|
||||
allowWifiLock: false,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// ── Publikus API ───────────────────────────────────────────────────────────
|
||||
|
||||
/// Elindítja a rögzítést a megadott forrással (alapértelmezett: telefon GPS).
|
||||
Future<void> startRecording({LocationSource? source}) async {
|
||||
if (isRecording.value) return;
|
||||
|
||||
_source = source ?? PhoneGpsSource(intervalMs: 1000, distanceFilter: 2.0);
|
||||
|
||||
// Track létrehozása az adatbázisban
|
||||
final now = DateTime.now();
|
||||
final name = DateFormat('yyyy-MM-dd HH:mm').format(now);
|
||||
final trackId = await _db.insertTrack(Track(
|
||||
name: name,
|
||||
startTime: now,
|
||||
source: _source!.displayName,
|
||||
));
|
||||
currentTrack.value = await _db.getTrack(trackId);
|
||||
|
||||
// Állapot reset
|
||||
livePoints.clear();
|
||||
sessionDistance.value = 0;
|
||||
_accumulatedDistance = 0;
|
||||
_lastPoint = null;
|
||||
_sessionStart = now;
|
||||
|
||||
// Foreground service indítása (háttér-működéshez)
|
||||
await FlutterForegroundTask.startService(
|
||||
notificationTitle: 'Track rögzítése',
|
||||
notificationText: name,
|
||||
callback: startTrackingCallback,
|
||||
);
|
||||
|
||||
// GPS stream feliratkozás
|
||||
_positionSub = _source!.positionStream.listen(
|
||||
_onPosition,
|
||||
onError: (e) {
|
||||
Get.snackbar('GPS hiba', e.toString(),
|
||||
backgroundColor: Colors.red, colorText: Colors.white);
|
||||
stopRecording();
|
||||
},
|
||||
);
|
||||
|
||||
// Időmérő
|
||||
_startElapsedTimer();
|
||||
|
||||
isRecording.value = true;
|
||||
isPaused.value = false;
|
||||
}
|
||||
|
||||
void pauseRecording() {
|
||||
if (!isRecording.value || isPaused.value) return;
|
||||
_positionSub?.pause();
|
||||
_elapsedTimer?.cancel();
|
||||
isPaused.value = true;
|
||||
FlutterForegroundTask.updateService(
|
||||
notificationTitle: 'Track szüneteltetve',
|
||||
notificationText: currentTrack.value?.name ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
void resumeRecording() {
|
||||
if (!isPaused.value) return;
|
||||
_positionSub?.resume();
|
||||
_startElapsedTimer();
|
||||
isPaused.value = false;
|
||||
FlutterForegroundTask.updateService(
|
||||
notificationTitle: 'Track rögzítése',
|
||||
notificationText: currentTrack.value?.name ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> stopRecording() async {
|
||||
if (!isRecording.value) return;
|
||||
|
||||
await _positionSub?.cancel();
|
||||
_positionSub = null;
|
||||
_elapsedTimer?.cancel();
|
||||
_elapsedTimer = null;
|
||||
|
||||
// Track lezárása az adatbázisban
|
||||
final track = currentTrack.value;
|
||||
if (track?.id != null) {
|
||||
final finished = track!.copyWith(
|
||||
status: TrackStatus.finished,
|
||||
endTime: DateTime.now(),
|
||||
);
|
||||
await _db.updateTrack(finished);
|
||||
currentTrack.value = finished;
|
||||
}
|
||||
|
||||
await FlutterForegroundTask.stopService();
|
||||
await _source?.dispose();
|
||||
_source = null;
|
||||
|
||||
isRecording.value = false;
|
||||
isPaused.value = false;
|
||||
|
||||
await loadSavedTracks();
|
||||
}
|
||||
|
||||
Future<void> loadSavedTracks() async {
|
||||
savedTracks.value = await _db.listTracks();
|
||||
}
|
||||
|
||||
Future<void> deleteTrack(int id) async {
|
||||
await _db.deleteTrack(id);
|
||||
await loadSavedTracks();
|
||||
}
|
||||
|
||||
/// GPX export + rendszer megosztás dialóg.
|
||||
Future<void> exportTrack(Track track) async {
|
||||
try {
|
||||
final path = await _exporter.export(track);
|
||||
await Share.shareXFiles([XFile(path)],
|
||||
subject: 'Nyomvonal: ${track.name}');
|
||||
} catch (e) {
|
||||
Get.snackbar('Export hiba', e.toString(),
|
||||
backgroundColor: Colors.red, colorText: Colors.white);
|
||||
}
|
||||
}
|
||||
|
||||
/// Visszatölt egy mentett track pontjait a térképre (megtekintés).
|
||||
Future<List<LatLng>> loadTrackPoints(int trackId) async {
|
||||
final pts = await _db.getLatLons(trackId);
|
||||
return pts.map((p) => LatLng(p.lat, p.lon)).toList();
|
||||
}
|
||||
|
||||
// ── Belső logika ───────────────────────────────────────────────────────────
|
||||
|
||||
Future<void> _onPosition(SourcePosition pos) async {
|
||||
if (isPaused.value) return;
|
||||
|
||||
final trackId = currentTrack.value?.id;
|
||||
if (trackId == null) return;
|
||||
|
||||
// Távolság a legutóbbi ponttól
|
||||
double segmentDist = 0;
|
||||
if (_lastPoint != null) {
|
||||
segmentDist = haversineMeters(
|
||||
_lastPoint!.latitude,
|
||||
_lastPoint!.longitude,
|
||||
pos.latitude,
|
||||
pos.longitude,
|
||||
);
|
||||
// Szűrés: ugrásszerű változás (pl. GPS lock elvesztése) ignorálása
|
||||
if (segmentDist > 100) return;
|
||||
}
|
||||
|
||||
_accumulatedDistance += segmentDist;
|
||||
sessionDistance.value = _accumulatedDistance;
|
||||
|
||||
// Sebesség km/h-ban
|
||||
currentSpeedKmh.value = (pos.speed ?? 0) * 3.6;
|
||||
|
||||
// Pont mentése
|
||||
final point = TrackPoint(
|
||||
trackId: trackId,
|
||||
latitude: pos.latitude,
|
||||
longitude: pos.longitude,
|
||||
altitude: pos.altitude,
|
||||
accuracy: pos.accuracy,
|
||||
speed: pos.speed,
|
||||
heading: pos.heading,
|
||||
timestamp: pos.timestamp,
|
||||
);
|
||||
await _db.addPoint(point, _accumulatedDistance);
|
||||
|
||||
_lastPoint = point;
|
||||
|
||||
// UI frissítés
|
||||
livePoints.add(LatLng(pos.latitude, pos.longitude));
|
||||
|
||||
// Értesítés frissítése
|
||||
if (livePoints.length % 10 == 0) {
|
||||
final dist = _formatDistance(_accumulatedDistance);
|
||||
FlutterForegroundTask.updateService(
|
||||
notificationTitle: 'Track rögzítése – $dist',
|
||||
notificationText: elapsedFormatted.value,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _startElapsedTimer() {
|
||||
_elapsedTimer = Timer.periodic(const Duration(seconds: 1), (_) {
|
||||
if (_sessionStart == null) return;
|
||||
final d = DateTime.now().difference(_sessionStart!);
|
||||
final h = d.inHours.toString().padLeft(2, '0');
|
||||
final m = (d.inMinutes % 60).toString().padLeft(2, '0');
|
||||
final s = (d.inSeconds % 60).toString().padLeft(2, '0');
|
||||
elapsedFormatted.value = '$h:$m:$s';
|
||||
});
|
||||
}
|
||||
|
||||
String _formatDistance(double m) {
|
||||
if (m < 1000) return '${m.toStringAsFixed(0)} m';
|
||||
return '${(m / 1000).toStringAsFixed(2)} km';
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
stopRecording();
|
||||
if (!isRecording.value) {
|
||||
_positionSub?.cancel();
|
||||
_elapsedTimer?.cancel();
|
||||
}
|
||||
super.onClose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,259 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_map/flutter_map.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:latlong2/latlong.dart';
|
||||
|
||||
import '../controllers/tracking_controller.dart';
|
||||
import '../../../../models/track.dart';
|
||||
|
||||
/// Mentett track-ek listája megtekintési és export lehetőséggel.
|
||||
class TrackListView extends GetView<TrackingController> {
|
||||
const TrackListView({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Mentett track-ek'),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh),
|
||||
onPressed: controller.loadSavedTracks,
|
||||
)
|
||||
],
|
||||
),
|
||||
body: Obx(() {
|
||||
final tracks = controller.savedTracks;
|
||||
if (tracks.isEmpty) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.route, size: 64, color: Colors.grey.shade300),
|
||||
const SizedBox(height: 12),
|
||||
Text('Még nincs mentett nyomvonal',
|
||||
style: TextStyle(color: Colors.grey.shade500)),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return ListView.separated(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
itemCount: tracks.length,
|
||||
separatorBuilder: (_, __) => const Divider(height: 1),
|
||||
itemBuilder: (context, i) => _TrackTile(track: tracks[i]),
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _TrackTile extends GetView<TrackingController> {
|
||||
final Track track;
|
||||
const _TrackTile({required this.track});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final statusColor = switch (track.status) {
|
||||
TrackStatus.recording => Colors.red,
|
||||
TrackStatus.paused => Colors.orange,
|
||||
TrackStatus.finished => Colors.green,
|
||||
};
|
||||
|
||||
return Dismissible(
|
||||
key: ValueKey(track.id),
|
||||
direction: DismissDirection.endToStart,
|
||||
confirmDismiss: (_) => _confirm(context),
|
||||
onDismissed: (_) => controller.deleteTrack(track.id!),
|
||||
background: Container(
|
||||
color: Colors.red,
|
||||
alignment: Alignment.centerRight,
|
||||
padding: const EdgeInsets.only(right: 20),
|
||||
child: const Icon(Icons.delete, color: Colors.white),
|
||||
),
|
||||
child: ListTile(
|
||||
leading: CircleAvatar(
|
||||
backgroundColor: statusColor.withOpacity(0.15),
|
||||
child: Icon(Icons.route, color: statusColor),
|
||||
),
|
||||
title: Text(track.name,
|
||||
style: const TextStyle(fontWeight: FontWeight.w600)),
|
||||
subtitle: Text(
|
||||
'${track.distanceFormatted} · ${track.durationFormatted}'
|
||||
' · ${track.pointCount} pont\n${track.source}',
|
||||
),
|
||||
isThreeLine: true,
|
||||
trailing: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.map_outlined),
|
||||
tooltip: 'Megtekintés',
|
||||
onPressed: () => Get.to(() => _TrackPreviewView(track: track)),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.share),
|
||||
tooltip: 'GPX export',
|
||||
onPressed: () => controller.exportTrack(track),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<bool> _confirm(BuildContext context) async {
|
||||
return await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (_) => AlertDialog(
|
||||
title: const Text('Track törlése'),
|
||||
content: Text('"${track.name}" törlése visszavonohatalan.'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Mégse')),
|
||||
FilledButton.tonal(
|
||||
style: FilledButton.styleFrom(
|
||||
backgroundColor: Colors.red.shade50),
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: const Text('Törlés',
|
||||
style: TextStyle(color: Colors.red))),
|
||||
],
|
||||
),
|
||||
) ??
|
||||
false;
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Track előnézeti térkép ──────────────────────────────────────────────────
|
||||
|
||||
class _TrackPreviewView extends GetView<TrackingController> {
|
||||
final Track track;
|
||||
const _TrackPreviewView({required this.track});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(track.name),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.share),
|
||||
onPressed: () => controller.exportTrack(track),
|
||||
)
|
||||
],
|
||||
),
|
||||
body: FutureBuilder<List<LatLng>>(
|
||||
future: controller.loadTrackPoints(track.id!),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (snapshot.hasError) {
|
||||
return Center(child: Text('Hiba: ${snapshot.error}'));
|
||||
}
|
||||
final points = snapshot.data ?? [];
|
||||
if (points.isEmpty) {
|
||||
return const Center(child: Text('Nincs megjeleníthető pont.'));
|
||||
}
|
||||
|
||||
// Bounding box számítás automatikus zoom-hoz
|
||||
final lats = points.map((p) => p.latitude).toList();
|
||||
final lons = points.map((p) => p.longitude).toList();
|
||||
final center = LatLng(
|
||||
(lats.reduce((a, b) => a + b)) / lats.length,
|
||||
(lons.reduce((a, b) => a + b)) / lons.length,
|
||||
);
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: FlutterMap(
|
||||
options: MapOptions(
|
||||
initialCenter: center,
|
||||
initialZoom: 14,
|
||||
initialCameraFit: CameraFit.coordinates(
|
||||
coordinates: points,
|
||||
padding: const EdgeInsets.all(40),
|
||||
),
|
||||
),
|
||||
children: [
|
||||
TileLayer(
|
||||
urlTemplate:
|
||||
'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
|
||||
userAgentPackageName: 'hu.app_dev.terepi_seged',
|
||||
),
|
||||
PolylineLayer(
|
||||
polylines: [
|
||||
Polyline(
|
||||
points: points,
|
||||
color: Colors.blue.shade700,
|
||||
strokeWidth: 4.0,
|
||||
borderColor: Colors.blue.shade200,
|
||||
borderStrokeWidth: 1.5,
|
||||
),
|
||||
],
|
||||
),
|
||||
MarkerLayer(
|
||||
markers: [
|
||||
Marker(
|
||||
point: points.first,
|
||||
child: const Icon(Icons.flag,
|
||||
color: Colors.green, size: 28),
|
||||
),
|
||||
Marker(
|
||||
point: points.last,
|
||||
child: const Icon(Icons.flag,
|
||||
color: Colors.red, size: 28),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
// Statisztika sáv alul
|
||||
Container(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
padding: EdgeInsets.fromLTRB(
|
||||
24, 12, 24, 12 + MediaQuery.of(context).padding.bottom),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||
children: [
|
||||
_InfoChip(
|
||||
icon: Icons.straighten, text: track.distanceFormatted),
|
||||
_InfoChip(
|
||||
icon: Icons.timer_outlined,
|
||||
text: track.durationFormatted),
|
||||
_InfoChip(
|
||||
icon: Icons.location_on_outlined,
|
||||
text: '${track.pointCount} pont'),
|
||||
_InfoChip(icon: Icons.sensors, text: track.source),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _InfoChip extends StatelessWidget {
|
||||
final IconData icon;
|
||||
final String text;
|
||||
const _InfoChip({required this.icon, required this.text});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(icon, size: 15, color: Theme.of(context).colorScheme.primary),
|
||||
const SizedBox(width: 4),
|
||||
Text(text, style: const TextStyle(fontSize: 13)),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,390 @@
|
||||
import 'package:get/get.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_map/flutter_map.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:latlong2/latlong.dart';
|
||||
|
||||
import '../controllers/tracking_controller.dart';
|
||||
import '../../../../models/track.dart';
|
||||
import 'track_list_view.dart';
|
||||
|
||||
class TrackingView extends GetView<TrackingController> {
|
||||
const TrackingView({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container();
|
||||
return Scaffold(
|
||||
extendBody: true,
|
||||
appBar: AppBar(
|
||||
title: const Text('Nyomvonal'),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.list_alt),
|
||||
tooltip: 'Mentett track-ek',
|
||||
onPressed: () => Get.to(() => const TrackListView()),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Stack(
|
||||
children: [
|
||||
_LiveMap(),
|
||||
_StatsPanel(),
|
||||
],
|
||||
),
|
||||
bottomNavigationBar: _ControlBar(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Térkép ─────────────────────────────────────────────────────────────────
|
||||
|
||||
class _LiveMap extends GetView<TrackingController> {
|
||||
const _LiveMap();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final mapController = MapController();
|
||||
|
||||
return Obx(() {
|
||||
final points = controller.livePoints;
|
||||
final center = points.isNotEmpty
|
||||
? points.last
|
||||
: const LatLng(47.5, 19.0); // Magyarország közepe
|
||||
|
||||
// Középre követ rögzítés közben
|
||||
if (controller.isRecording.value && points.isNotEmpty) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
mapController.move(points.last, mapController.camera.zoom);
|
||||
});
|
||||
}
|
||||
|
||||
return FlutterMap(
|
||||
mapController: mapController,
|
||||
options: MapOptions(
|
||||
initialCenter: center,
|
||||
initialZoom: 15.0,
|
||||
interactionOptions: const InteractionOptions(
|
||||
flags: InteractiveFlag.all,
|
||||
),
|
||||
),
|
||||
children: [
|
||||
TileLayer(
|
||||
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
|
||||
userAgentPackageName: 'hu.app_dev.terepi_seged',
|
||||
),
|
||||
if (points.length >= 2)
|
||||
PolylineLayer(
|
||||
polylines: [
|
||||
Polyline(
|
||||
points: points,
|
||||
color: Colors.blue.shade700,
|
||||
strokeWidth: 4.0,
|
||||
borderColor: Colors.blue.shade200,
|
||||
borderStrokeWidth: 1.5,
|
||||
),
|
||||
],
|
||||
),
|
||||
if (points.isNotEmpty)
|
||||
MarkerLayer(
|
||||
markers: [
|
||||
// Kezdőpont
|
||||
Marker(
|
||||
point: points.first,
|
||||
child: const Icon(Icons.flag, color: Colors.green, size: 28),
|
||||
),
|
||||
// Aktuális pozíció
|
||||
Marker(
|
||||
point: points.last,
|
||||
child: _PulsingDot(
|
||||
color:
|
||||
controller.isPaused.value ? Colors.orange : Colors.blue,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Pulzáló pont az aktuális pozícióhoz.
|
||||
class _PulsingDot extends StatefulWidget {
|
||||
final Color color;
|
||||
const _PulsingDot({required this.color});
|
||||
|
||||
@override
|
||||
State<_PulsingDot> createState() => _PulsingDotState();
|
||||
}
|
||||
|
||||
class _PulsingDotState extends State<_PulsingDot>
|
||||
with SingleTickerProviderStateMixin {
|
||||
late AnimationController _anim;
|
||||
late Animation<double> _scale;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_anim = AnimationController(
|
||||
vsync: this, duration: const Duration(milliseconds: 1000))
|
||||
..repeat(reverse: true);
|
||||
_scale = Tween(begin: 0.8, end: 1.3)
|
||||
.animate(CurvedAnimation(parent: _anim, curve: Curves.easeInOut));
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_anim.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ScaleTransition(
|
||||
scale: _scale,
|
||||
child: Container(
|
||||
width: 18,
|
||||
height: 18,
|
||||
decoration: BoxDecoration(
|
||||
color: widget.color,
|
||||
shape: BoxShape.circle,
|
||||
border: Border.all(color: Colors.white, width: 2),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: widget.color.withOpacity(0.5),
|
||||
blurRadius: 8,
|
||||
spreadRadius: 2)
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Statisztika panel ────────────────────────────────────────────────────────
|
||||
|
||||
class _StatsPanel extends GetView<TrackingController> {
|
||||
const _StatsPanel();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Obx(() {
|
||||
if (!controller.isRecording.value && controller.livePoints.isEmpty) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
return Positioned(
|
||||
top: 12,
|
||||
left: 12,
|
||||
right: 12,
|
||||
child: Card(
|
||||
elevation: 4,
|
||||
shape:
|
||||
RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||||
color: Theme.of(context).colorScheme.surface.withOpacity(0.92),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||
children: [
|
||||
_Stat(
|
||||
icon: Icons.timer_outlined,
|
||||
label: 'Idő',
|
||||
value: controller.elapsedFormatted.value,
|
||||
),
|
||||
_Divider(),
|
||||
_Stat(
|
||||
icon: Icons.straighten,
|
||||
label: 'Távolság',
|
||||
value: _formatDist(controller.sessionDistance.value),
|
||||
),
|
||||
_Divider(),
|
||||
_Stat(
|
||||
icon: Icons.speed,
|
||||
label: 'Sebesség',
|
||||
value:
|
||||
'${controller.currentSpeedKmh.value.toStringAsFixed(1)} km/h',
|
||||
),
|
||||
_Divider(),
|
||||
_Stat(
|
||||
icon: Icons.location_on_outlined,
|
||||
label: 'Pontok',
|
||||
value: controller.livePoints.length.toString(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
String _formatDist(double m) {
|
||||
if (m < 1000) return '${m.toStringAsFixed(0)} m';
|
||||
return '${(m / 1000).toStringAsFixed(2)} km';
|
||||
}
|
||||
}
|
||||
|
||||
class _Stat extends StatelessWidget {
|
||||
final IconData icon;
|
||||
final String label;
|
||||
final String value;
|
||||
const _Stat({required this.icon, required this.label, required this.value});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(icon, size: 16, color: Theme.of(context).colorScheme.primary),
|
||||
const SizedBox(height: 2),
|
||||
Text(value,
|
||||
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 15)),
|
||||
Text(label,
|
||||
style: TextStyle(fontSize: 10, color: Colors.grey.shade600)),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _Divider extends StatelessWidget {
|
||||
const _Divider();
|
||||
@override
|
||||
Widget build(BuildContext context) =>
|
||||
Container(height: 36, width: 1, color: Colors.grey.shade300);
|
||||
}
|
||||
|
||||
// ─── Vezérlő sáv ────────────────────────────────────────────────────────────
|
||||
|
||||
class _ControlBar extends GetView<TrackingController> {
|
||||
const _ControlBar();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Obx(() {
|
||||
final recording = controller.isRecording.value;
|
||||
final paused = controller.isPaused.value;
|
||||
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withOpacity(0.12),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, -2))
|
||||
],
|
||||
),
|
||||
padding: EdgeInsets.fromLTRB(
|
||||
24, 12, 24, 12 + MediaQuery.of(context).padding.bottom),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: [
|
||||
if (!recording) ...[
|
||||
// ── Nincs aktív track ──
|
||||
_RoundButton(
|
||||
icon: Icons.fiber_manual_record,
|
||||
label: 'Indítás',
|
||||
color: Colors.red,
|
||||
onTap: () => controller.startRecording(),
|
||||
),
|
||||
] else ...[
|
||||
// ── Aktív track ──
|
||||
_RoundButton(
|
||||
icon: paused ? Icons.play_arrow : Icons.pause,
|
||||
label: paused ? 'Folytatás' : 'Szünet',
|
||||
color: Colors.orange,
|
||||
onTap: paused
|
||||
? controller.resumeRecording
|
||||
: controller.pauseRecording,
|
||||
),
|
||||
// Nagy Stop gomb középen
|
||||
GestureDetector(
|
||||
onTap: () => _confirmStop(context),
|
||||
child: Container(
|
||||
width: 68,
|
||||
height: 68,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.red,
|
||||
shape: BoxShape.circle,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.red.withOpacity(0.4),
|
||||
blurRadius: 12,
|
||||
spreadRadius: 2)
|
||||
],
|
||||
),
|
||||
child: const Icon(Icons.stop, color: Colors.white, size: 32),
|
||||
),
|
||||
),
|
||||
_RoundButton(
|
||||
icon: Icons.share,
|
||||
label: 'Export',
|
||||
color: Colors.blue,
|
||||
onTap: () {
|
||||
final t = controller.currentTrack.value;
|
||||
if (t != null) controller.exportTrack(t);
|
||||
},
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _confirmStop(BuildContext context) async {
|
||||
final ok = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (_) => AlertDialog(
|
||||
title: const Text('Track leállítása'),
|
||||
content: const Text(
|
||||
'Leállítja a rögzítést? A track automatikusan mentésre kerül.'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Mégse')),
|
||||
FilledButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: const Text('Leállítás')),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (ok == true) controller.stopRecording();
|
||||
}
|
||||
}
|
||||
|
||||
class _RoundButton extends StatelessWidget {
|
||||
final IconData icon;
|
||||
final String label;
|
||||
final Color color;
|
||||
final VoidCallback onTap;
|
||||
const _RoundButton(
|
||||
{required this.icon,
|
||||
required this.label,
|
||||
required this.color,
|
||||
required this.onTap});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: onTap,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Container(
|
||||
width: 52,
|
||||
height: 52,
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.12),
|
||||
shape: BoxShape.circle,
|
||||
border: Border.all(color: color, width: 1.5),
|
||||
),
|
||||
child: Icon(icon, color: color, size: 26),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(label, style: TextStyle(fontSize: 11, color: color)),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user