MapLegend constructor

const MapLegend(
  1. MapElement source, {
  2. bool shouldAlwaysShowScrollbar = false,
  3. Widget? title,
  4. MapLegendPosition position = MapLegendPosition.top,
  5. Offset? offset,
  6. MapLegendOverflowMode overflowMode = MapLegendOverflowMode.wrap,
  7. Axis? direction,
  8. EdgeInsetsGeometry? padding = const EdgeInsets.all(10.0),
  9. double spacing = 10.0,
  10. MapIconType iconType = MapIconType.circle,
  11. Size iconSize = const Size(8.0, 8.0),
  12. TextStyle? textStyle,
  13. bool enableToggleInteraction = false,
  14. Color? toggledItemColor,
  15. Color? toggledItemStrokeColor,
  16. double toggledItemStrokeWidth = 1.0,
  17. double toggledItemOpacity = 0.5,
})

Creates a legend with different styles like circle, rectangle, triangle and square for the bubbles or shapes.

Information provided in the legend helps to identify the data rendered in the map shapes or bubbles.

Defaults to null.

By default, legend will not be shown.

Legend for shape

Set MapLegend.source to MapElement.shape to show legend for shapes.

If MapShapeSource.shapeColorMappers is not null, then MapColorMapper.color and MapColorMapper.text will be used for the legend item's icon and the legend item's text respectively.

If MapShapeSource.shapeColorMappers is null, the color returned in the MapShapeSource.shapeColorValueMapper will be applied to the legend item's icon and the legend item's text will be taken from the MapShapeSource.shapeDataField.

In a rare case, if both the MapShapeSource.shapeColorMappers and the MapShapeSource.shapeColorValueMapper properties are null, the legend item's text will be taken from the MapShapeSource.shapeDataField property and the legend item's icon will have the default color.

Legend for bubbles

Set MapLegend.source to MapElement.bubble to show legend for bubbles.

If MapShapeSource.bubbleColorMappers is not null, then MapColorMapper.color and MapColorMapper.text will be used for the legend item's icon and the legend item's text respectively.

If MapShapeSource.bubbleColorMappers is null, the color returned in the MapShapeSource.bubbleColorValueMapper will be applied to the legend item's icon and the legend item's text will be taken from the MapShapeSource.shapeDataField.

If both the MapShapeSource.bubbleColorMappers and the MapShapeSource.bubbleColorValueMapper properties are null, the legend item's text will be taken from the MapShapeSource.shapeDataField property and the legend item's icon will have the default color.

late List<DataModel> _data;
late MapShapeSource _mapSource;

 @override
 void initState() {
   super.initState();

   _data = <DataModel>[
     DataModel('India', 280, "Low"),
     DataModel('United States of America', 190, "High"),
     DataModel('Pakistan', 37, "Low"),
   ];

   _mapSource = MapShapeSource.asset(
     "assets/world_map.json",
     shapeDataField: "name",
     dataCount: _data.length,
     primaryValueMapper: (int index) {
       return _data[index].country;
     },
     shapeColorValueMapper: (int index) {
       return _data[index].storage;
     },
     shapeColorMappers: [
       MapColorMapper(value: "Low", color: Colors.red),
       MapColorMapper(value: "High", color: Colors.green)
     ],
   );
 }

 @override
 Widget build(BuildContext context) {
   return Scaffold(
     appBar: AppBar(
       title: Text('Default legend'),
     ),
     body: Center(
       child: SfMaps(
         layers: [
           MapShapeLayer(
             source: _mapSource,
             legend: MapLegend(MapElement.shape),
           )
         ],
       ),
     ),
   );
 }

class DataModel {
  const DataModel(this.country, this.count, this.storage);

  final String country;
  final double count;
  final String storage;
}

See also:

  • MapLegend.bar() named constructor, for bar legend type.

Implementation

const MapLegend(
  this.source, {
  this.shouldAlwaysShowScrollbar = false,
  this.title,
  this.position = MapLegendPosition.top,
  this.offset,
  this.overflowMode = MapLegendOverflowMode.wrap,
  this.direction,
  this.padding = const EdgeInsets.all(10.0),
  this.spacing = 10.0,
  this.iconType = MapIconType.circle,
  this.iconSize = const Size(8.0, 8.0),
  this.textStyle,
  this.enableToggleInteraction = false,
  this.toggledItemColor,
  this.toggledItemStrokeColor,
  this.toggledItemStrokeWidth = 1.0,
  this.toggledItemOpacity = 0.5,
})  : _type = _LegendType.vector,
      segmentSize = null,
      labelsPlacement = null,
      edgeLabelsPlacement = MapLegendEdgeLabelsPlacement.inside,
      labelOverflow = MapLabelOverflow.visible,
      segmentPaintingStyle = MapLegendPaintingStyle.solid,
      showPointerOnHover = false,
      pointerBuilder = null,
      pointerColor = null,
      pointerSize = Size.zero,
      assert(spacing >= 0),
      assert(toggledItemStrokeWidth >= 0),
      assert(toggledItemOpacity >= 0 && toggledItemOpacity <= 1);