flutter_demo_gps/lib/provider/gps_model.dart

45 lines
1.2 KiB
Dart
Raw Normal View History

2022-10-18 15:07:13 +02:00
import 'package:flutter/material.dart';
import 'package:location/location.dart';
import 'package:gps/models/gps_point.dart';
import 'package:gps/utils/gps_persister.dart';
class GpsModel with ChangeNotifier {
bool _recording = false;
GpsPoint _currentLocation = GpsPoint();
List<GpsPoint> _recordedLocations = [];
bool get isRecording => _recording;
GpsPoint get currentLocation => _currentLocation;
List<GpsPoint> get locations => List.unmodifiable(_recordedLocations);
GpsModel(Stream<LocationData> stream) {
stream.listen((event) {
_currentLocation = GpsPoint(
latitude: event.latitude ?? 0.0,
longitude: event.longitude ?? 0.0,
accuracy: event.accuracy ?? 0.0,
speed: event.speed ?? 0.0,
heading: event.heading ?? 0.0,
time: event.time ?? 0.0,
satelliteNumber: event.satelliteNumber ?? 0,
provider: event.provider ?? "none");
if (_recording) {
_recordedLocations.add(_currentLocation);
}
notifyListeners();
});
}
void toggleRecording() {
_recording = !_recording;
notifyListeners();
}
void save(GpsPersister s) {
s.save(locations);
_recordedLocations = [];
}
}