Show the live map on the run screen

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWZ9CKZsNxtD5Jr9bYD
This commit is contained in:
Krzysztof Rudnicki 2026-11-10 16:27:42 +01:00
parent bd12b8711d
commit aaaecca3be
3 changed files with 42 additions and 39 deletions

View File

@ -4,9 +4,9 @@
/// ///
/// * `RunMap.route(points: track.points)` -- a finished run. The camera /// * `RunMap.route(points: track.points)` -- a finished run. The camera
/// fits the whole route ([fitPadding] keeps it clear of overlays). /// fits the whole route ([fitPadding] keeps it clear of overlays).
/// * `RunMap.live(trackListenable: notifier)` -- the live screen. Pass a /// * `RunMap.live(pointsListenable: recorder.livePoints)` -- the live
/// `ValueListenable<RunTrack?>` (a `ValueNotifier<RunTrack?>`, or the /// screen. Pass a `ValueListenable<List<RunPoint>>` (the recorder's
/// recorder's own, anything that holds the current track); the route /// accepted points, oldest first); the route
/// redraws on every change and, in follow mode (the default), the camera /// redraws on every change and, in follow mode (the default), the camera
/// keeps the last point centred. Dragging the map ends follow mode; the /// keeps the last point centred. Dragging the map ends follow mode; the
/// recentre button (icon only) turns it back on. The listenable must stay /// recentre button (icon only) turns it back on. The listenable must stay
@ -48,11 +48,11 @@ class RunMap extends StatefulWidget {
super.key, super.key,
this.fitPadding = const EdgeInsets.all(40), this.fitPadding = const EdgeInsets.all(40),
this.tileProviderOpener = openOsmTileProvider, this.tileProviderOpener = openOsmTileProvider,
}) : trackListenable = null; }) : pointsListenable = null;
/// A live run: follows the last point of the track in [trackListenable]. /// A live run: follows the last point in [pointsListenable].
const RunMap.live({ const RunMap.live({
required ValueListenable<RunTrack?> this.trackListenable, required ValueListenable<List<RunPoint>> this.pointsListenable,
super.key, super.key,
this.tileProviderOpener = openOsmTileProvider, this.tileProviderOpener = openOsmTileProvider,
}) : points = null, }) : points = null,
@ -61,8 +61,8 @@ class RunMap extends StatefulWidget {
/// The route in detail mode; null in live mode. /// The route in detail mode; null in live mode.
final List<RunPoint>? points; final List<RunPoint>? points;
/// The current track in live mode; null in detail mode. /// The accepted points in live mode; null in detail mode.
final ValueListenable<RunTrack?>? trackListenable; final ValueListenable<List<RunPoint>>? pointsListenable;
/// Space kept free around the fitted route (detail mode). /// Space kept free around the fitted route (detail mode).
final EdgeInsets fitPadding; final EdgeInsets fitPadding;
@ -81,16 +81,16 @@ class _RunMapState extends State<RunMap> {
bool _follow = true; bool _follow = true;
bool _warned = false; bool _warned = false;
bool get _live => widget.trackListenable != null; bool get _live => widget.pointsListenable != null;
List<RunPoint> get _points => _live List<RunPoint> get _points => _live
? widget.trackListenable!.value?.points ?? const [] ? widget.pointsListenable!.value
: widget.points!; : widget.points!;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
widget.trackListenable?.addListener(_followLast); widget.pointsListenable?.addListener(_followLast);
unawaited( unawaited(
widget.tileProviderOpener().then((p) { widget.tileProviderOpener().then((p) {
if (mounted) setState(() => _tiles = p); if (mounted) setState(() => _tiles = p);
@ -100,7 +100,7 @@ class _RunMapState extends State<RunMap> {
@override @override
void dispose() { void dispose() {
widget.trackListenable?.removeListener(_followLast); widget.pointsListenable?.removeListener(_followLast);
_controller.dispose(); _controller.dispose();
super.dispose(); super.dispose();
} }
@ -173,15 +173,14 @@ class _RunMapState extends State<RunMap> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final listenable = widget.trackListenable; final listenable = widget.pointsListenable;
if (listenable == null) return _map(context, widget.points!); if (listenable == null) return _map(context, widget.points!);
return Stack( return Stack(
fit: StackFit.expand, fit: StackFit.expand,
children: [ children: [
ValueListenableBuilder<RunTrack?>( ValueListenableBuilder<List<RunPoint>>(
valueListenable: listenable, valueListenable: listenable,
builder: (context, track, _) => builder: (context, points, _) => _map(context, points),
_map(context, track?.points ?? const []),
), ),
if (!_follow) if (!_follow)
Positioned( Positioned(

View File

@ -4,20 +4,28 @@ library;
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:workout_app/models/run_track.dart'; import 'package:workout_app/models/run_track.dart';
import 'package:workout_app/services/map_tile_cache.dart';
import 'package:workout_app/ui/theme.dart'; import 'package:workout_app/ui/theme.dart';
import 'package:workout_app/widgets/run_map.dart';
/// Placeholder for the live map: replace the body of [build] with the map /// The live map on the run screen: [RunMap.live] over the recorder's points,
/// widget, keeping the [ValueListenable] as its source of points. /// clipped to the card shape.
/// ///
/// `RunScreen` fills this space from `mapBuilder`, defaulting to this widget, /// `RunScreen` fills this space from `mapBuilder`, defaulting to this widget.
/// so a map can also be dropped in by passing a builder with no edit here.
class RunMapSlot extends StatelessWidget { class RunMapSlot extends StatelessWidget {
/// Creates the slot; [points] updates with every accepted GPS fix. /// Creates the slot; [points] updates with every accepted GPS fix.
const RunMapSlot({required this.points, super.key}); const RunMapSlot({
required this.points,
super.key,
this.tileProviderOpener = openOsmTileProvider,
});
/// The accepted points of the run so far, oldest first. /// The accepted points of the run so far, oldest first.
final ValueListenable<List<RunPoint>> points; final ValueListenable<List<RunPoint>> points;
/// Where the map's tiles come from; tests pass a fake.
final TileProviderOpener tileProviderOpener;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final scheme = Theme.of(context).colorScheme; final scheme = Theme.of(context).colorScheme;
@ -27,13 +35,10 @@ class RunMapSlot extends StatelessWidget {
color: scheme.surfaceContainerHigh, color: scheme.surfaceContainerHigh,
borderRadius: BorderRadius.circular(AppRadius.md), borderRadius: BorderRadius.circular(AppRadius.md),
), ),
child: Center( clipBehavior: Clip.antiAlias,
child: Icon( child: RunMap.live(
Icons.map_outlined, pointsListenable: points,
size: 48, tileProviderOpener: tileProviderOpener,
color: scheme.onSurfaceVariant,
semanticLabel: 'Map',
),
), ),
); );
} }

View File

@ -5,7 +5,6 @@ import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:latlong2/latlong.dart'; import 'package:latlong2/latlong.dart';
import 'package:workout_app/models/run_track.dart'; import 'package:workout_app/models/run_track.dart';
import 'package:workout_app/services/map_tile_cache.dart';
import 'package:workout_app/widgets/run_map.dart'; import 'package:workout_app/widgets/run_map.dart';
import '_run_map_fixtures.dart'; import '_run_map_fixtures.dart';
@ -161,17 +160,17 @@ void main() {
testWidgets('follows the last point and draws as the track grows', ( testWidgets('follows the last point and draws as the track grows', (
tester, tester,
) async { ) async {
final track = ValueNotifier<RunTrack?>(null); final track = ValueNotifier<List<RunPoint>>(const []);
addTearDown(track.dispose); addTearDown(track.dispose);
await _pump( await _pump(
tester, tester,
RunMap.live(trackListenable: track, tileProviderOpener: fakeTiles()), RunMap.live(pointsListenable: track, tileProviderOpener: fakeTiles()),
); );
expect(find.byType(PolylineLayer), findsNothing); expect(find.byType(PolylineLayer), findsNothing);
expect(find.byType(MarkerLayer), findsNothing); expect(find.byType(MarkerLayer), findsNothing);
final full = buildTrack(count: 4); final full = buildTrack(count: 4);
track.value = full; track.value = full.points;
await tester.pump(); await tester.pump();
expect(find.byType(PolylineLayer), findsOneWidget); expect(find.byType(PolylineLayer), findsOneWidget);
expect( expect(
@ -189,11 +188,11 @@ void main() {
tester, tester,
) async { ) async {
final full = buildTrack(count: 4); final full = buildTrack(count: 4);
final track = ValueNotifier<RunTrack?>(full); final track = ValueNotifier<List<RunPoint>>(full.points);
addTearDown(track.dispose); addTearDown(track.dispose);
await _pump( await _pump(
tester, tester,
RunMap.live(trackListenable: track, tileProviderOpener: fakeTiles()), RunMap.live(pointsListenable: track, tileProviderOpener: fakeTiles()),
); );
await tester.drag(find.byType(FlutterMap), const Offset(120, 0)); await tester.drag(find.byType(FlutterMap), const Offset(120, 0));
await tester.pump(); await tester.pump();
@ -202,26 +201,26 @@ void main() {
expect(dragged, isNot(closeTo(full.points.last.longitude, 1e-6))); expect(dragged, isNot(closeTo(full.points.last.longitude, 1e-6)));
// New fixes no longer pull the camera while follow is off. // New fixes no longer pull the camera while follow is off.
track.value = buildTrack(count: 6); track.value = buildTrack(count: 6).points;
await tester.pump(); await tester.pump();
expect(_camera(tester).center.longitude, closeTo(dragged, 1e-9)); expect(_camera(tester).center.longitude, closeTo(dragged, 1e-9));
await tester.tap(find.byIcon(Icons.my_location)); await tester.tap(find.byIcon(Icons.my_location));
await tester.pump(); await tester.pump();
expect(find.byIcon(Icons.my_location), findsNothing); expect(find.byIcon(Icons.my_location), findsNothing);
final last = track.value!.points.last; final last = track.value.last;
expect(_camera(tester).center.longitude, closeTo(last.longitude, 1e-6)); expect(_camera(tester).center.longitude, closeTo(last.longitude, 1e-6));
}); });
testWidgets('stops listening when disposed', (tester) async { testWidgets('stops listening when disposed', (tester) async {
final track = ValueNotifier<RunTrack?>(buildTrack()); final track = ValueNotifier<List<RunPoint>>(buildTrack().points);
addTearDown(track.dispose); addTearDown(track.dispose);
await _pump( await _pump(
tester, tester,
RunMap.live(trackListenable: track, tileProviderOpener: fakeTiles()), RunMap.live(pointsListenable: track, tileProviderOpener: fakeTiles()),
); );
await tester.pumpWidget(const SizedBox()); await tester.pumpWidget(const SizedBox());
track.value = buildTrack(count: 7); track.value = buildTrack(count: 7).points;
await tester.pump(); await tester.pump();
expect(tester.takeException(), isNull); expect(tester.takeException(), isNull);
}); });