Flutter - 秒表和地理定位器
Flutter - Stopwatch and Geolocator
我真的卡在了这部分代码上(我是 Flutter 的初学者)。我想显示秒表计时器和与地理定位器的总距离,但秒表只会在“地理定位”移动时更新。
当按下按钮时,我调用这个:
void _toggleListening() {
if (_positionStreamSubscription == null) {
const LocationOptions locationOptions =
LocationOptions(accuracy: LocationAccuracy.best);
final Stream<Position> positionStream =
Geolocator().getPositionStream(locationOptions);
_positionStreamSubscription = positionStream.listen((Position position) {
setState(() {
_positions.add(position);
_allPositions = _positions;
_positionRealTime = position;
updatePinOnMap();
print('all lenght ${_allPositions.length}');
totalDistanceAux = 0.0;
for (var i = 0; i < _allPositions.length - 1; i++) {
totalDistanceAux = totalDistanceAux +
distance.as(
LengthUnit.Meter,
LatLng(_allPositions[i].latitude, _allPositions[i].longitude),
LatLng(_allPositions[i + 1].latitude,
_allPositions[i + 1].longitude),
);
}
totalDistance = totalDistanceAux;
print('total distance $totalDistance');
});
});
_positionStreamSubscription.pause();
_stopwatch.stop();
} else {
setState(() {
if (_positionStreamSubscription.isPaused == true) {
_positionStreamSubscription.resume();
_stopwatch.start();
}
});
}
}
我正在使用 FutureBuilder 进行展示:
FutureBuilder<GeolocationStatus>(
future: Geolocator().checkGeolocationPermissionStatus(),
builder: (BuildContext context,
AsyncSnapshot<GeolocationStatus> snapshot) {
距离和计时器工作正常,唯一的问题是计时器只是在有一些移动时更新。
这是我在 Whosebug 的第一个问题,如果我做错了什么,抱歉。谢谢!
如果您想定期重建小部件,请添加 timer_builder 包并像这样使用它:
TimerBuilder.periodic(Duration(seconds: 1),
builder: (context) {
return YourWidget();
}
)
我真的卡在了这部分代码上(我是 Flutter 的初学者)。我想显示秒表计时器和与地理定位器的总距离,但秒表只会在“地理定位”移动时更新。
当按下按钮时,我调用这个:
void _toggleListening() {
if (_positionStreamSubscription == null) {
const LocationOptions locationOptions =
LocationOptions(accuracy: LocationAccuracy.best);
final Stream<Position> positionStream =
Geolocator().getPositionStream(locationOptions);
_positionStreamSubscription = positionStream.listen((Position position) {
setState(() {
_positions.add(position);
_allPositions = _positions;
_positionRealTime = position;
updatePinOnMap();
print('all lenght ${_allPositions.length}');
totalDistanceAux = 0.0;
for (var i = 0; i < _allPositions.length - 1; i++) {
totalDistanceAux = totalDistanceAux +
distance.as(
LengthUnit.Meter,
LatLng(_allPositions[i].latitude, _allPositions[i].longitude),
LatLng(_allPositions[i + 1].latitude,
_allPositions[i + 1].longitude),
);
}
totalDistance = totalDistanceAux;
print('total distance $totalDistance');
});
});
_positionStreamSubscription.pause();
_stopwatch.stop();
} else {
setState(() {
if (_positionStreamSubscription.isPaused == true) {
_positionStreamSubscription.resume();
_stopwatch.start();
}
});
}
}
我正在使用 FutureBuilder 进行展示:
FutureBuilder<GeolocationStatus>(
future: Geolocator().checkGeolocationPermissionStatus(),
builder: (BuildContext context,
AsyncSnapshot<GeolocationStatus> snapshot) {
距离和计时器工作正常,唯一的问题是计时器只是在有一些移动时更新。 这是我在 Whosebug 的第一个问题,如果我做错了什么,抱歉。谢谢!
如果您想定期重建小部件,请添加 timer_builder 包并像这样使用它:
TimerBuilder.periodic(Duration(seconds: 1),
builder: (context) {
return YourWidget();
}
)