119 lines
3.2 KiB
Dart
119 lines
3.2 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:geolocator/geolocator.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
void main() {
|
|
runApp(const WalkerApp());
|
|
}
|
|
|
|
class WalkerApp extends StatelessWidget {
|
|
const WalkerApp({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
title: 'Blockwalker',
|
|
theme: ThemeData(primarySwatch: Colors.blue),
|
|
home: const TrackingScreen(),
|
|
);
|
|
}
|
|
}
|
|
|
|
class TrackingScreen extends StatefulWidget {
|
|
const TrackingScreen({super.key});
|
|
|
|
@override
|
|
State<TrackingScreen> createState() => _TrackingScreenState();
|
|
}
|
|
|
|
class _TrackingScreenState extends State<TrackingScreen> {
|
|
bool _tracking = false;
|
|
List<String> _logs = [];
|
|
Timer? _timer;
|
|
|
|
@override
|
|
void dispose() {
|
|
_timer?.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _startTracking() async {
|
|
final permission = await Geolocator.requestPermission();
|
|
if (permission == LocationPermission.denied ||
|
|
permission == LocationPermission.deniedForever) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Location permission required')),
|
|
);
|
|
return;
|
|
}
|
|
|
|
setState(() => _tracking = true);
|
|
_timer = Timer.periodic(const Duration(seconds: 5), (_) async {
|
|
final pos = await Geolocator.getCurrentPosition(
|
|
desiredAccuracy: LocationAccuracy.high,
|
|
);
|
|
final log = '${DateTime.now().toIso8601String()} — (${pos.latitude}, ${pos.longitude})';
|
|
setState(() => _logs.insert(0, log));
|
|
|
|
final timestamp = DateTime.now().toIso8601String();
|
|
await _sendLocationToBackend(pos.latitude, pos.longitude, timestamp);
|
|
});
|
|
}
|
|
|
|
Future<void> _sendLocationToBackend(double lat, double lon, String timestamp) async {
|
|
final url = Uri.parse('http://sam.local:3008/api/location');
|
|
|
|
final response = await http.post(
|
|
url,
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: jsonEncode({
|
|
'latitude': lat,
|
|
'longitude': lon,
|
|
'timestamp': timestamp,
|
|
}),
|
|
);
|
|
|
|
if (response.statusCode != 200) {
|
|
debugPrint('Failed to send location: ${response.statusCode}');
|
|
} else {
|
|
debugPrint('Location sent successfully');
|
|
}
|
|
}
|
|
|
|
void _stopTracking() {
|
|
_timer?.cancel();
|
|
setState(() => _tracking = false);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Blockwalker')),
|
|
body: Column(
|
|
children: [
|
|
const SizedBox(height: 20),
|
|
ElevatedButton(
|
|
onPressed: _tracking ? _stopTracking : _startTracking,
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: _tracking ? Colors.red : Colors.green,
|
|
),
|
|
child: Text(_tracking ? 'Stop Tracking' : 'Begin Tracking'),
|
|
),
|
|
const SizedBox(height: 20),
|
|
Expanded(
|
|
child: ListView.builder(
|
|
itemCount: _logs.length,
|
|
itemBuilder: (context, index) => ListTile(
|
|
dense: true,
|
|
title: Text(_logs[index]),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|