· 6 years ago · Oct 10, 2019, 02:00 AM
1import 'package:flutter/material.dart';
2import '../google_maps_webservice/places.dart';
3import 'dart:async';
4import 'package:honeybee/styles.dart';
5import '../search/list_studies/filter_bloc.dart';
6import '../search/list_studies/filter_event.dart';
7
8class SearchLocations extends StatefulWidget {
9 final FilterBloc filterBloc;
10 SearchLocations({Key key, this.filterBloc}) : super(key: key);
11 @override
12 _SearchLocations createState() => _SearchLocations();
13}
14
15class _SearchLocations extends State<SearchLocations> {
16final _places = new GoogleMapsPlaces(apiKey: "AIzaSyBnWDXQAU9nLwLcZmGjANIrhsFk8FsQz_k");
17
18 @override
19 void dispose() {
20 _places.dispose();
21 // widget.filterBloc.dispose(); need this?
22 super.dispose();
23 }
24
25 @override
26 Widget build(BuildContext context) {
27 return Padding(
28 padding: EdgeInsets.only(bottom: 2),
29 child: FlatButton(
30 onPressed: () {
31 showSearch(
32 context: context,
33 delegate: LocationSearchDelegate(placesAPIKey: _places, filterBloc: widget.filterBloc),
34 );
35 },
36 padding: EdgeInsets.all(0),
37 child: Container(
38 width: 300,
39 decoration: BoxDecoration(
40 color: Colors.white,
41 borderRadius: BorderRadius.circular(5),
42 border: Border.all(color: Colors.grey[600])),
43 padding: EdgeInsets.all(10.0),
44 child: _textStyling(widget.filterBloc.currentState.location)
45 )
46 )
47 );
48 }
49
50 Widget _textStyling(text) {
51 if (text == 'Enter a Location or Address') {
52 return Text(text, style: Styles.homepageInputs);
53 } else {
54 return Text(text, style:Styles.homepageInputsBlack);
55 }
56 }
57}
58
59class LocationSearchDelegate extends SearchDelegate {
60 final placesAPIKey;
61 final filterBloc;
62 LocationSearchDelegate({this.placesAPIKey, this.filterBloc}) : super();
63
64 @override
65 List<Widget> buildActions(BuildContext context) {
66 return [
67 IconButton(
68 icon: Icon(Icons.clear),
69 onPressed: () {
70 query = '';
71 },
72 )
73 ];
74 }
75
76 @override
77 Widget buildLeading(BuildContext context) {
78 return IconButton(
79 icon: Icon(Icons.arrow_back),
80 onPressed: () {
81 close(context, null);
82 },
83 );
84 }
85
86 @override
87 Widget buildResults(BuildContext context) {
88 return Container();
89 }
90
91 final _initialResults = [["Current Location",""], ["Toronto","ChIJpTvG15DL1IkRd8S0KlBVNTI"]]; //need to put a retrieval API for current location placeID
92
93 @override
94 Widget buildSuggestions(BuildContext context) {
95 return StreamBuilder<List<List<String>>>(
96 initialData: _initialResults,
97 stream: _getData(),
98 builder: (context, AsyncSnapshot<List<List<String>>> snapshot) {
99 if (snapshot.hasError) {
100 return Text('Error: ${snapshot.error}');
101 }
102 switch (snapshot.connectionState) {
103 case ConnectionState.waiting:
104 return CircularProgressIndicator();
105 default:
106 if (snapshot.data.isEmpty) {
107 return ListView(children: <Widget>[
108 ListTile(title: Text("Current Location"), ), //onTap: filterBloc.dispatch(ChangeLocation(location: value))
109 ListTile(title: Text("Toronto"))
110 ]);
111 }
112 final results = snapshot.data;
113 return ListView(
114 children: results.map<ListTile>((a) =>
115 ListTile(
116 title: Text(a[0]), //[0] is the title of the location, [1] is the placeID
117 onTap: () async { filterBloc.dispatch(ChangeLocation(locationAndPlaceID: a)); close(context, a);},
118 )
119 ).toList(),
120 );
121 }
122 }
123 );
124 }
125
126 Stream<List<List<String>>> _getData() async* {
127 var values = List<List<String>>();
128 PlacesAutocompleteResponse res = await placesAPIKey.autocomplete(query);
129 if (res.isOkay) {
130 res.predictions.forEach((Prediction p) {
131 values.add([p.description, p.placeId]);
132 });
133 }
134 else {
135 print(res.errorMessage);
136 }
137 yield values;
138 }
139
140}
141
142/*{
143 if (snapshot.hasError) {
144 return Text('Error: ${snapshot.error}');
145 }
146 switch (snapshot.connectionState) {
147 case ConnectionState.waiting:
148 return CircularProgressIndicator();
149 default:
150 if (snapshot.data.isEmpty) {
151 return ListView(children: <Widget>[
152 ListTile(title: Text("Current Location"), ), //onTap: filterBloc.dispatch(ChangeLocation(location: value))
153 ListTile(title: Text("Toronto"))
154 ]);
155 }
156 return ListView.builder(
157 itemCount: snapshot.data.length,
158 itemBuilder: (context, index) {
159 return ListTile(
160 title: Text(snapshot.data[index]),
161 onTap: () async {close(context, a);},
162 );
163 }
164 );
165 }
166 }*/
167
168/* LISTVIEW NOT BUILDER:
169{
170 if (snapshot.hasError) {
171 return Text('Error: ${snapshot.error}');
172 }
173 switch (snapshot.connectionState) {
174 case ConnectionState.waiting:
175 return CircularProgressIndicator();
176 default:
177 if (snapshot.data.isEmpty) {
178 return ListView(children: <Widget>[
179 ListTile(title: Text("Current Location"), ), //onTap: filterBloc.dispatch(ChangeLocation(location: value))
180 ListTile(title: Text("Toronto"))
181 ]);
182 }
183 final results = snapshot.data;
184 return ListView(
185 children: results.map<ListTile>((a) =>
186 ListTile(
187 title: Text(a.toString()),
188 onTap: () async {close(context, a);},
189 )
190 ).toList(),
191 );
192 }
193 }
194 */