From 39e0db1f5df67c771286e6c06582a9a279713a5b Mon Sep 17 00:00:00 2001 From: Reimar Date: Tue, 27 Aug 2024 11:48:11 +0200 Subject: [PATCH] Allow clicking on favorites --- Mobile/lib/main.dart | 49 ++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/Mobile/lib/main.dart b/Mobile/lib/main.dart index 4792b5a..74af895 100644 --- a/Mobile/lib/main.dart +++ b/Mobile/lib/main.dart @@ -50,7 +50,7 @@ class _MyHomePageState extends State { List _favorites = []; LatLng? _selectedPoint; - void _showLocation(TapPosition _, LatLng point) async { + void _onTap(TapPosition _, LatLng point) async { setState(() => _selectedPoint = point); final dynamic location; @@ -60,6 +60,11 @@ class _MyHomePageState extends State { headers: {'User-Agent': 'SkanTravels/1.0'}, ); + if (mounted && response.statusCode != 200) { + ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Unable to fetch information about this location (HTTP ${response.statusCode})'))); + return; + } + location = jsonDecode(response.body); } catch (_) { if (!mounted) return; @@ -73,6 +78,14 @@ class _MyHomePageState extends State { if (!mounted) return; + if (location['name'] != null && location['display_name'] != null) { + await _showLocation(point, location['name'], location['display_name']); + } + + setState(() => _selectedPoint = null); + } + + Future _showLocation(LatLng point, String name, String description) async { await showModalBottomSheet( barrierColor: Colors.black.withOpacity(0.3), context: context, @@ -87,17 +100,17 @@ class _MyHomePageState extends State { Expanded(child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - Text(location['name'], style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 24)), + Text(name, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 24)), const SizedBox(height: 10), - Text(location['display_name']), + Text(description), ], )), Column(children: [ IconButton( - icon: const Icon(Icons.star), - iconSize: 32, - color: _favorites.where((fav) => fav.lat == point.latitude && fav.lng == point.longitude).isEmpty ? Colors.grey : Colors.yellow, - onPressed: () => _toggleFavorite(point, location['name'], location['display_name'], setModalState, context) + icon: const Icon(Icons.star), + iconSize: 32, + color: _favorites.where((fav) => fav.lat == point.latitude && fav.lng == point.longitude).isEmpty ? Colors.grey : Colors.yellow, + onPressed: () => _toggleFavorite(point, name, description, setModalState, context) ), const IconButton(icon: Icon(Icons.rate_review), iconSize: 32, onPressed: null), ]), @@ -107,8 +120,6 @@ class _MyHomePageState extends State { }); }, ); - - setState(() => _selectedPoint = null); } void _toggleFavorite(LatLng point, String name, String description, StateSetter setModalState, BuildContext context) async { @@ -130,7 +141,7 @@ class _MyHomePageState extends State { return; } - _fetchFavorites(); + await _fetchFavorites(); setModalState(() {}); return; @@ -147,7 +158,7 @@ class _MyHomePageState extends State { setModalState(() {}); } - void _fetchFavorites() async { + Future _fetchFavorites() async { final response = await api.request(context, api.ApiService.app, 'GET', '/favorites', null); if (response == null) return; @@ -179,7 +190,7 @@ class _MyHomePageState extends State { options: MapOptions( initialCenter: const LatLng(55.9397, 9.5156), initialZoom: 7.0, - onTap: _showLocation, + onTap: _onTap, ), children: [ openStreetMapTileLayer, @@ -205,10 +216,18 @@ class _MyHomePageState extends State { width: 30, height: 50, alignment: Alignment.center, - child: const Stack( + child: Stack( children: [ - Icon(Icons.location_pin, size: 30, color: Colors.yellow), - Icon(Icons.location_on_outlined, size: 30, color: Colors.black), + IconButton( + padding: const EdgeInsets.only(bottom: 10), + icon: const Icon(Icons.location_pin, size: 30, color: Colors.yellow), + onPressed: () => _showLocation(LatLng(favorite.lat, favorite.lng), favorite.name, favorite.description), + ), + IconButton( + padding: const EdgeInsets.only(bottom: 10), + icon: const Icon(Icons.location_on_outlined, size: 30, color: Colors.black), + onPressed: () => _showLocation(LatLng(favorite.lat, favorite.lng), favorite.name, favorite.description), + ), ] ), )