MapLegend constructor Null safety

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