f1e88b1ac3
- Add shared_preferences for persisting server URL - Add SettingsService and PlatformService - Add server URL input field on non-web platforms - Make ApiConfig baseUrl configurable at runtime - Add Android location permissions (ACCESS_FINE/COURSE_LOCATION, INTERNET) - Request location permission on login and map init - Fix geo_id type: use String instead of int (UUID format) - Align share_service with API spec: remove unique_id, use share_id only - Fix watch endpoint response: last_update instead of created_at - Add error handling with SnackBars for geo operations - Wrap login screen in SingleChildScrollView for keyboard handling - Update map tile layer with userAgentPackageName for OSM
290 lines
8.4 KiB
Dart
290 lines
8.4 KiB
Dart
import 'dart:async';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_map/flutter_map.dart';
|
|
import 'package:latlong2/latlong.dart';
|
|
import 'package:geolocator/geolocator.dart';
|
|
import 'package:provider/provider.dart';
|
|
import '../providers/auth_provider.dart';
|
|
import '../providers/map_provider.dart';
|
|
import '../providers/share_provider.dart';
|
|
import '../services/geo_service.dart';
|
|
|
|
class MapScreen extends StatefulWidget {
|
|
const MapScreen({super.key});
|
|
|
|
@override
|
|
State<MapScreen> createState() => _MapScreenState();
|
|
}
|
|
|
|
class _MapScreenState extends State<MapScreen> {
|
|
LatLng _center = const LatLng(40.7128, -74.0060);
|
|
bool _loading = true;
|
|
Timer? _trackingTimer;
|
|
Timer? _geoTimer;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_initLocationAndShare();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_trackingTimer?.cancel();
|
|
_geoTimer?.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _initLocationAndShare() async {
|
|
try {
|
|
if (!await Geolocator.isLocationServiceEnabled()) {
|
|
setState(() => _loading = false);
|
|
return;
|
|
}
|
|
|
|
final status = await Geolocator.checkPermission();
|
|
if (status == LocationPermission.denied) {
|
|
final requested = await Geolocator.requestPermission();
|
|
if (requested == LocationPermission.denied) {
|
|
return;
|
|
}
|
|
}
|
|
if (status == LocationPermission.deniedForever) {
|
|
return;
|
|
}
|
|
|
|
final position = await Geolocator.getCurrentPosition();
|
|
setState(() {
|
|
_center = LatLng(position.latitude, position.longitude);
|
|
});
|
|
|
|
if (!mounted) return;
|
|
|
|
final authProvider = context.read<AuthProvider>();
|
|
final shareProvider = context.read<ShareProvider>();
|
|
|
|
if (authProvider.token.isNotEmpty) {
|
|
try {
|
|
await shareProvider.createShare(
|
|
authProvider.token,
|
|
position.longitude,
|
|
position.latitude,
|
|
);
|
|
} catch (e) {
|
|
if (!mounted) return;
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('Ошибка геолокации: $e')),
|
|
);
|
|
}
|
|
}
|
|
|
|
if (!mounted) return;
|
|
setState(() => _loading = false);
|
|
|
|
_geoTimer = Timer.periodic(
|
|
const Duration(seconds: 30),
|
|
(_) => _updateGeolocation(),
|
|
);
|
|
} catch (_) {
|
|
setState(() => _loading = false);
|
|
}
|
|
}
|
|
|
|
Future<void> _updateGeolocation() async {
|
|
try {
|
|
final status = await Geolocator.checkPermission();
|
|
if (status == LocationPermission.denied) {
|
|
final requested = await Geolocator.requestPermission();
|
|
if (requested == LocationPermission.denied) return;
|
|
}
|
|
final position = await Geolocator.getCurrentPosition();
|
|
setState(() {
|
|
_center = LatLng(position.latitude, position.longitude);
|
|
});
|
|
|
|
if (!mounted) return;
|
|
|
|
final authProvider = context.read<AuthProvider>();
|
|
final shareProvider = context.read<ShareProvider>();
|
|
|
|
if (shareProvider.geoId.isEmpty) return;
|
|
|
|
await GeoService().updatePosition(
|
|
authProvider.token,
|
|
shareProvider.geoId,
|
|
position.longitude,
|
|
position.latitude,
|
|
);
|
|
} catch (e) {
|
|
if (!mounted) return;
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('Ошибка обновления позиции: $e')),
|
|
);
|
|
}
|
|
}
|
|
|
|
void _copyShareLink() {
|
|
final shareProvider = context.read<ShareProvider>();
|
|
if (shareProvider.shareId.isNotEmpty) {
|
|
Clipboard.setData(ClipboardData(text: 'watch/${shareProvider.shareId}'));
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Ссылка скопирована')),
|
|
);
|
|
}
|
|
}
|
|
|
|
void _startTracking() {
|
|
final authProvider = context.read<AuthProvider>();
|
|
final shareProvider = context.read<ShareProvider>();
|
|
|
|
if (shareProvider.trackingShareId.isEmpty) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) {
|
|
final TextEditingController _controller = TextEditingController();
|
|
return AlertDialog(
|
|
title: const Text('Отслеживание'),
|
|
content: SingleChildScrollView(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
const Text('Введите Share ID для отслеживания:'),
|
|
const SizedBox(height: 16),
|
|
TextField(
|
|
controller: _controller,
|
|
decoration: const InputDecoration(
|
|
hintText: 'Введите Share ID',
|
|
border: OutlineInputBorder(),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: const Text('Отмена'),
|
|
),
|
|
TextButton(
|
|
onPressed: () async {
|
|
final shareId = _controller.text.trim();
|
|
if (shareId.isNotEmpty) {
|
|
Navigator.pop(context);
|
|
final token = authProvider.token;
|
|
await shareProvider.startTracking(shareId, token);
|
|
_trackingTimer = Timer.periodic(
|
|
const Duration(seconds: 5),
|
|
(_) => shareProvider.updateTrackedPosition(),
|
|
);
|
|
}
|
|
},
|
|
child: const Text('Начать'),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
} else {
|
|
shareProvider.stopTracking();
|
|
_trackingTimer?.cancel();
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final authProvider = context.watch<AuthProvider>();
|
|
final _ = context.watch<MapProvider>();
|
|
final shareProvider = context.watch<ShareProvider>();
|
|
|
|
if (_loading) {
|
|
return Scaffold(
|
|
body: const Center(child: CircularProgressIndicator()),
|
|
);
|
|
}
|
|
|
|
Marker? trackedMarker;
|
|
if (shareProvider.trackedPosition != null) {
|
|
final pos = shareProvider.trackedPosition!;
|
|
final lat = pos['y'] is String ? double.parse(pos['y']) : pos['y'] as double;
|
|
final lng = pos['x'] is String ? double.parse(pos['x']) : pos['x'] as double;
|
|
trackedMarker = Marker(
|
|
point: LatLng(lat, lng),
|
|
width: 40,
|
|
height: 40,
|
|
child: const Icon(
|
|
Icons.person,
|
|
color: Colors.red,
|
|
size: 30,
|
|
),
|
|
);
|
|
}
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(
|
|
'Family Safety Map\n${_center.latitude.toStringAsFixed(4)}, ${_center.longitude.toStringAsFixed(4)}',
|
|
textAlign: TextAlign.center,
|
|
),
|
|
actions: [
|
|
IconButton(
|
|
icon: const Icon(Icons.refresh),
|
|
onPressed: _updateGeolocation,
|
|
),
|
|
IconButton(
|
|
icon: const Icon(Icons.share),
|
|
onPressed: _copyShareLink,
|
|
),
|
|
IconButton(
|
|
icon: Icon(
|
|
shareProvider.trackingShareId.isEmpty
|
|
? Icons.person_search
|
|
: Icons.person,
|
|
color: shareProvider.trackingShareId.isEmpty
|
|
? null
|
|
: Colors.red,
|
|
),
|
|
onPressed: _startTracking,
|
|
),
|
|
IconButton(
|
|
icon: const Icon(Icons.logout),
|
|
onPressed: () {
|
|
_geoTimer?.cancel();
|
|
authProvider.logout();
|
|
Navigator.of(context).pushReplacementNamed('/');
|
|
},
|
|
),
|
|
],
|
|
),
|
|
body: FlutterMap(
|
|
options: MapOptions(
|
|
initialCenter: _center,
|
|
initialZoom: 12.0,
|
|
),
|
|
children: [
|
|
TileLayer(
|
|
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
|
|
maxZoom: 20,
|
|
userAgentPackageName:'FamilySafety/1.0.0',
|
|
),
|
|
MarkerLayer(
|
|
markers: [
|
|
Marker(
|
|
point: _center,
|
|
width: 40,
|
|
height: 40,
|
|
child: const Icon(
|
|
Icons.my_location,
|
|
color: Colors.blue,
|
|
size: 30,)
|
|
|
|
),
|
|
if (trackedMarker != null) trackedMarker,
|
|
],
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
} |