Newer
Older
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_core/theme.dart';
import 'package:syncfusion_flutter_maps/maps.dart';
/// ChoroplethMap is a stateful widget that contains a choropleth map.
/// The map is created using the Syncfusion Flutter Maps library and
/// coordinates fetched from the server.
class ChoroplethMap extends StatefulWidget {
const ChoroplethMap({
Key? key,
required this.relation,
required this.measurements,
required this.onSelectionChanged,
final Uint8List relation;
final List<Measurement> measurements;
final void Function(int selectedIndex) onSelectionChanged;
ChoroplethMapState createState() => ChoroplethMapState();
class ChoroplethMapState extends State<ChoroplethMap> {
int selectedIndex = -1; // Subdivision/map tile index
late MapShapeSource dataSource;
late final MapZoomPanBehavior _zoomPanBehavior = MapZoomPanBehavior();
void updateDataSource() {
_initDataSource();
}
@override
void initState() {
super.initState();
_initDataSource();
}
void _initDataSource() {
dataSource = MapShapeSource.memory(
widget.relation,
shapeDataField: 'sub_div_id',
dataCount: widget.subdivisions.length,
primaryValueMapper: (int index) => widget.subdivisions[index].sub_div_id,
shapeColorValueMapper: (int index) => widget.subdivisions[index].avgThickness, // NB will later be minThickness
shapeColorMappers: const [
MapColorMapper(
color: Color(0xffff0000),
text: '{0},{1}'),
MapColorMapper(
from: 4,
to: 8,
color: Color(0xffff6a00),
MapColorMapper(
from: 8,
to: 12,
color: Color(0xFFb1ff00),
MapColorMapper(
from: 12,
to: 400,
color: Color(0xFF00d6ff),
@override
Widget build(BuildContext context) {
updateDataSource();
return Stack(
children: [
data: SfMapsThemeData( // Coloring of selected shape
selectionStrokeWidth: 3.5,
selectionStrokeColor: Colors.blue[600],
),
child: SfMaps(
layers: [
MapShapeLayer(
legend: MapLegend.bar(
MapElement.shape,
position: MapLegendPosition.bottom,
segmentSize: const Size(70.0, 7.0),
textStyle: smallTextStyle,
),
zoomPanBehavior: _zoomPanBehavior,
strokeColor: Colors.blue.shade50,
onSelectionChanged: (int index) {
widget.onSelectionChanged(selectedIndex);