MapLegend.bar constructor Null safety

const MapLegend.bar(
  1. MapElement source,
  2. {Widget? title,
  3. MapLegendOverflowMode overflowMode = MapLegendOverflowMode.scroll,
  4. EdgeInsetsGeometry? padding = const EdgeInsets.all(10.0),
  5. MapLegendPosition position = MapLegendPosition.top,
  6. Offset? offset,
  7. double spacing = 2.0,
  8. Size? segmentSize,
  9. TextStyle? textStyle,
  10. Axis? direction,
  11. MapLegendLabelsPlacement? labelsPlacement,
  12. MapLegendEdgeLabelsPlacement edgeLabelsPlacement = MapLegendEdgeLabelsPlacement.inside,
  13. MapLabelOverflow labelOverflow = MapLabelOverflow.visible,
  14. MapLegendPaintingStyle segmentPaintingStyle = MapLegendPaintingStyle.solid,
  15. bool showPointerOnHover = false,
  16. MapLegendPointerBuilder? pointerBuilder,
  17. Color? pointerColor,
  18. Size pointerSize = const Size(16, 12)}
)

Creates a bar type legend 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.

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('Bar legend'),
     ),
     body: Center(
       child: SfMaps(
         layers: [
           MapShapeLayer(
             source: _mapSource,
             legend: MapLegend.bar(MapElement.shape),
           )
         ],
       ),
     ),
   );
 }

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

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

See also:

  • MapLegend(), for legend type with different styles like circle, diamond, rectangle and triangle.

Implementation

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