-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.dart
117 lines (102 loc) · 3.07 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_heatmap/flutter_map_heatmap.dart';
import 'package:latlong2/latlong.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Heatmap Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'flutter_mapmap heat_map demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
StreamController<void> _rebuildStream = StreamController.broadcast();
List<WeightedLatLng> data = [];
List<Map<double, MaterialColor>> gradients = [
HeatMapOptions.defaultGradient,
{0.25: Colors.blue, 0.55: Colors.red, 0.85: Colors.pink, 1.0: Colors.purple}
];
var index = 0;
initState() {
_loadData();
super.initState();
}
@override
dispose() {
_rebuildStream.close();
super.dispose();
}
_loadData() async {
var str = await rootBundle.loadString('assets/initial_data.json');
List<dynamic> result = jsonDecode(str);
setState(() {
data = result
.map((e) => e as List<dynamic>)
.map((e) => WeightedLatLng(LatLng(e[0], e[1]), 1))
.toList();
});
}
void _incrementCounter() {
setState(() {
index = index == 0 ? 1 : 0;
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
_rebuildStream.add(null);
});
});
}
@override
Widget build(BuildContext context) {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
_rebuildStream.add(null);
});
final map = new FlutterMap(
options: new MapOptions(
initialCenter: new LatLng(57.8827, -6.0400), initialZoom: 8.0),
children: [
TileLayer(
urlTemplate: "https://tile.openstreetmap.org/{z}/{x}/{y}.png"),
if (data.isNotEmpty)
HeatMapLayer(
heatMapDataSource: InMemoryHeatMapDataSource(data: data),
heatMapOptions: HeatMapOptions(
gradient: this.gradients[this.index], minOpacity: 0.1),
reset: _rebuildStream.stream,
)
],
);
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
backgroundColor: Colors.pink,
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Container(child: map),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Switch Gradient',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}