diff --git a/packages/smooth_app/lib/knowledge_panel/knowledge_panels/knowledge_panel_card.dart b/packages/smooth_app/lib/knowledge_panel/knowledge_panels/knowledge_panel_card.dart index 82f53fef727c..98c67e9d744c 100644 --- a/packages/smooth_app/lib/knowledge_panel/knowledge_panels/knowledge_panel_card.dart +++ b/packages/smooth_app/lib/knowledge_panel/knowledge_panels/knowledge_panel_card.dart @@ -31,6 +31,7 @@ class KnowledgePanelCard extends StatelessWidget { final UserPreferences userPreferences = context.watch(); final KnowledgePanel? panel = KnowledgePanelsBuilder.getKnowledgePanel(product, panelId); + if (panel == null) { return EMPTY_WIDGET; } diff --git a/packages/smooth_app/lib/knowledge_panel/knowledge_panels/knowledge_panel_expanded_card.dart b/packages/smooth_app/lib/knowledge_panel/knowledge_panels/knowledge_panel_expanded_card.dart index 3a47e3d2acf9..cb8e9e9320bf 100644 --- a/packages/smooth_app/lib/knowledge_panel/knowledge_panels/knowledge_panel_expanded_card.dart +++ b/packages/smooth_app/lib/knowledge_panel/knowledge_panels/knowledge_panel_expanded_card.dart @@ -1,6 +1,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:openfoodfacts/openfoodfacts.dart'; +import 'package:provider/provider.dart'; import 'package:smooth_app/generic_lib/design_constants.dart'; import 'package:smooth_app/knowledge_panel/knowledge_panels_builder.dart'; @@ -47,9 +48,13 @@ class KnowledgePanelExpandedCard extends StatelessWidget { } } } - return Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: elementWidgets, + return Provider( + lazy: true, + create: (_) => panel, + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: elementWidgets, + ), ); } diff --git a/packages/smooth_app/lib/knowledge_panel/knowledge_panels/knowledge_panel_world_map_card.dart b/packages/smooth_app/lib/knowledge_panel/knowledge_panels/knowledge_panel_world_map_card.dart index a352587fd530..12af3a2313f4 100644 --- a/packages/smooth_app/lib/knowledge_panel/knowledge_panels/knowledge_panel_world_map_card.dart +++ b/packages/smooth_app/lib/knowledge_panel/knowledge_panels/knowledge_panel_world_map_card.dart @@ -3,8 +3,10 @@ import 'package:flutter/material.dart'; import 'package:flutter_map/flutter_map.dart'; import 'package:latlong2/latlong.dart'; import 'package:openfoodfacts/openfoodfacts.dart'; +import 'package:provider/provider.dart'; import 'package:smooth_app/generic_lib/design_constants.dart'; import 'package:smooth_app/helpers/launch_url_helper.dart'; +import 'package:smooth_app/pages/product/world_map_page.dart'; class KnowledgePanelWorldMapCard extends StatelessWidget { const KnowledgePanelWorldMapCard(this.mapElement); @@ -20,6 +22,7 @@ class KnowledgePanelWorldMapCard extends StatelessWidget { const double markerSize = 30; final List markers = []; final List coordinates = []; + void addCoordinate(final LatLng latLng) { coordinates.add(latLng); markers.add( @@ -40,11 +43,79 @@ class KnowledgePanelWorldMapCard extends StatelessWidget { } } + final MapOptions mapOptions = _generateMapOptions( + coordinates: coordinates, + markerSize: markerSize, + interactive: false, + ); + + final List children = [ + TileLayer( + urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png', + userAgentPackageName: 'org.openfoodfacts.app', + ), + MarkerLayer(markers: markers), + RichAttributionWidget( + animationConfig: const ScaleRAWA(), + showFlutterMapAttribution: false, + attributions: [ + TextSourceAttribution( + 'OpenStreetMap contributors', + onTap: () => LaunchUrlHelper.launchURL( + 'https://www.openstreetmap.org/copyright', + ), + ), + ], + ), + ]; + + return Padding( + padding: const EdgeInsetsDirectional.only(bottom: MEDIUM_SPACE), + child: SizedBox( + height: 200, + child: InkWell( + onTap: () { + Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => WorldMapPage( + title: _getTitle(context), + mapOptions: _generateMapOptions( + coordinates: coordinates, + markerSize: markerSize, + initialZoom: 12.0, + interactive: true, + ), + children: children, + ), + ), + ); + }, + child: IgnorePointer( + ignoring: true, + child: FlutterMap( + options: mapOptions, + children: children, + ), + ), + ), + ), + ); + } + + MapOptions _generateMapOptions({ + required List coordinates, + required double markerSize, + double initialZoom = 6.0, + bool interactive = false, + }) { final MapOptions mapOptions; if (coordinates.length == 1) { mapOptions = MapOptions( initialCenter: coordinates.first, - initialZoom: 6.0, + initialZoom: initialZoom, + interactionOptions: InteractionOptions( + flags: interactive ? InteractiveFlag.all : InteractiveFlag.none, + ), ); } else { mapOptions = MapOptions( @@ -52,39 +123,22 @@ class KnowledgePanelWorldMapCard extends StatelessWidget { coordinates: coordinates, maxZoom: 13.0, forceIntegerZoomLevel: true, - padding: const EdgeInsets.all(markerSize), + padding: EdgeInsets.all(markerSize), + ), + interactionOptions: InteractionOptions( + flags: interactive ? InteractiveFlag.all : InteractiveFlag.none, ), ); } - return Padding( - padding: const EdgeInsetsDirectional.only(bottom: MEDIUM_SPACE), - child: SizedBox( - height: 200, - child: FlutterMap( - options: mapOptions, - children: [ - TileLayer( - urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png', - userAgentPackageName: 'org.openfoodfacts.app', - ), - MarkerLayer(markers: markers), - RichAttributionWidget( - popupInitialDisplayDuration: const Duration(seconds: 5), - animationConfig: const ScaleRAWA(), - showFlutterMapAttribution: false, - attributions: [ - TextSourceAttribution( - 'OpenStreetMap contributors', - onTap: () => LaunchUrlHelper.launchURL( - 'https://www.openstreetmap.org/copyright', - ), - ), - ], - ), - ], - ), - ), - ); + return mapOptions; + } + + String? _getTitle(BuildContext context) { + try { + return context.read().titleElement?.title; + } catch (_) { + return null; + } } @override diff --git a/packages/smooth_app/lib/pages/product/world_map_page.dart b/packages/smooth_app/lib/pages/product/world_map_page.dart new file mode 100644 index 000000000000..1bd75493959b --- /dev/null +++ b/packages/smooth_app/lib/pages/product/world_map_page.dart @@ -0,0 +1,32 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_map/flutter_map.dart'; +import 'package:smooth_app/widgets/smooth_scaffold.dart'; + +class WorldMapPage extends StatelessWidget { + const WorldMapPage({ + required this.title, + required this.mapOptions, + required this.children, + super.key, + }); + + final String? title; + final MapOptions mapOptions; + final List children; + + @override + Widget build(BuildContext context) { + return SmoothScaffold( + appBar: AppBar( + title: Text(title ?? ''), + backgroundColor: + AppBarTheme.of(context).backgroundColor?.withValues(alpha: 0.8), + ), + extendBodyBehindAppBar: true, + body: FlutterMap( + options: mapOptions, + children: children, + ), + ); + } +}