-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathstreambuilder_test.dart
More file actions
122 lines (113 loc) · 4.4 KB
/
streambuilder_test.dart
File metadata and controls
122 lines (113 loc) · 4.4 KB
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
118
119
120
121
122
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:geoflutterfire2/geoflutterfire2.dart';
import 'package:rxdart/rxdart.dart';
class StreamTestWidget extends StatefulWidget {
@override
_StreamTestWidgetState createState() => _StreamTestWidgetState();
}
class _StreamTestWidgetState extends State<StreamTestWidget> {
late Stream<List<DocumentSnapshot<Map<String, dynamic>>>> stream;
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final GeoFlutterFire geo = GeoFlutterFire();
// ignore: close_sinks
final BehaviorSubject<double> radius = BehaviorSubject<double>.seeded(1.0);
double _value = 20.0;
String _label = '';
@override
void initState() {
super.initState();
final center = geo.point(latitude: 12.960632, longitude: 77.641603);
stream = radius.switchMap((double rad) {
var collectionReference = _firestore.collection('locations').withConverter<Map<String, dynamic>>(
fromFirestore: (snapshot, _) => snapshot.data() ?? {},
toFirestore: (value, _) => value,
);
// GeoFlutterFire returns a stream of List<DocumentSnapshot<Object?>>.
// Map each document to the correct type.
return geo.collection(collectionRef: collectionReference).within(
center: center,
radius: rad,
field: 'position',
strictMode: true,
).map((docs) => docs.map((doc) => doc as DocumentSnapshot<Map<String, dynamic>>).toList());
});
}
@override
void dispose() {
_latitudeController.dispose();
_longitudeController.dispose();
radius.close();
super.dispose();
}
final TextEditingController _latitudeController = TextEditingController();
final TextEditingController _longitudeController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Column(
children: <Widget>[
Expanded(
child: StreamBuilder<List<DocumentSnapshot<Map<String, dynamic>>>>(
stream: stream,
builder: (BuildContext context,
AsyncSnapshot<List<DocumentSnapshot<Map<String, dynamic>>>> snapshots) {
if (snapshots.connectionState == ConnectionState.active && snapshots.hasData) {
print('data ${snapshots.data}');
return Container(
height: MediaQuery.of(context).size.height * 2 / 3,
child: ListView.builder(
itemCount: snapshots.data!.length,
itemBuilder: (context, index) {
final doc = snapshots.data![index];
// Force non-null since we expect documents to contain data.
final Map<String, dynamic> data = doc.data()!;
print('doc with id ${doc.id} distance ${data['distance']}');
// Assume that data['position'] is a Map with a 'geopoint' key.
final GeoPoint point = (data['position'] as Map<String, dynamic>)['geopoint'] as GeoPoint;
return ListTile(
title: Text(
doc.id,
style: const TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text('${point.latitude}, ${point.longitude}'),
trailing: Text(
data['documentType'] == DocumentChangeType.added ? 'Added' : 'Modified',
),
);
},
),
);
} else {
return const Center(child: CircularProgressIndicator());
}
},
),
),
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Slider(
min: 1,
max: 200,
divisions: 4,
value: _value,
label: _label,
activeColor: Colors.blue,
inactiveColor: Colors.blue.withValues(alpha: 0.2),
onChanged: (double value) => changed(value),
),
),
],
),
);
}
void changed(double value) {
setState(() {
_value = value;
print(_value);
_label = '${_value.toInt()} kms';
});
radius.add(value);
}
}