Skip to content

Commit

Permalink
#15: Implemented Search Delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
burhanrashid52 committed Nov 17, 2019
1 parent 19e341d commit 663110e
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 49 deletions.
7 changes: 3 additions & 4 deletions lib/pages/map/map_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import 'package:flutter_app/pages/places/places_models.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class MapBloc extends BlocBase {
MapBloc(this.placesApiService) {
_fetchLocations("A");
}

MapBloc(this.placesApiService);

final PlacesApiService placesApiService;

Expand All @@ -32,7 +31,7 @@ class MapBloc extends BlocBase {
_mapTypeController.sink.add(mayType);
}

void _fetchLocations(String placeQuery) {
void fetchLocations(String placeQuery) {
placesApiService.searchPlaces(placeQuery).then((value) {
_officesController.sink.add(value.candidates);
});
Expand Down
85 changes: 40 additions & 45 deletions lib/pages/map/map_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_app/bloc/bloc_provider.dart';
import 'package:flutter_app/pages/map/map_bloc.dart';
import 'package:flutter_app/pages/map/place_widget.dart';
import 'package:flutter_app/pages/places/search_places.dart';
import 'package:flutter_app/pages/places/places_models.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

Expand All @@ -12,9 +13,8 @@ class MapPage extends StatelessWidget {

@override
Widget build(BuildContext context) {
final MapBloc mapBloc = BlocProvider.of(context);

_createMapTypeListener(mapBloc.mayType);
final MapBloc mapBloc = BlocProvider.of(context);

return Scaffold(
appBar: AppBar(
Expand All @@ -32,67 +32,62 @@ class MapPage extends StatelessWidget {
icon: Icon(Icons.dashboard),
onPressed: () => mapBloc.setMayType(MapType.terrain),
),
IconButton(
icon: Icon(Icons.search),
onPressed: () {
showSearch(
context: context,
delegate: SearchPlaces(mapBloc),
);
},
),
],
),
body: Stack(
children: <Widget>[
Container(
child: StreamBuilder<MapType>(
stream: mapBloc.mayType,
initialData: MapType.hybrid,
builder: (context, snapshotType) {
return buildGoogleMap(snapshotType.data);
},
),
),
MyGoogleMap(),
//TODO: Fix the scroll and item are not visible
Container(
child: StreamBuilder<List<Candidate>>(
stream: mapBloc.offices,
builder: (context, snapshot) {
if (snapshot.hasData) {
return PlaceWidget(snapshot.data);
}
return CircularProgressIndicator();
},
),
StreamBuilder<List<Candidate>>(
stream: mapBloc.offices,
builder: (context, snapshot) {
if (snapshot.hasData) {
return PlaceWidget(snapshot.data);
}
return CircularProgressIndicator();
},
)
],
),
);
}
}

class MyGoogleMap extends StatelessWidget {
final Completer<GoogleMapController> _controller = Completer();

GoogleMap buildGoogleMap(MapType mayType) {
return GoogleMap(
mapType: mayType,
initialCameraPosition: buildInitialCamera(),
zoomGesturesEnabled: true,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
@override
Widget build(BuildContext context) {
final MapBloc mapBloc = BlocProvider.of(context);
return StreamBuilder<MapType>(
stream: mapBloc.mayType,
initialData: MapType.hybrid,
builder: (context, snapshotType) {
return GoogleMap(
mapType: snapshotType.data,
initialCameraPosition: _buildInitialCamera(),
zoomGesturesEnabled: true,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
);
},
);
}

/*Set<Marker> buildMarkerSet(List<Office> offices) {
Set<Marker> markers = Set<Marker>();
for (var office in offices) {
markers.add(office.toMarker());
}
return markers;
}*/

CameraPosition buildInitialCamera() {
CameraPosition _buildInitialCamera() {
return CameraPosition(
target: LatLng(37.42796133580664, -122.085749655962),
zoom: 14.4746,
);
}

void _createMapTypeListener(Stream<MapType> mayType) {
mayType.listen((value) {
if (_controller != null) {
// _controller
}
});
}
}
1 change: 1 addition & 0 deletions lib/pages/places/places_api_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const SEARCH_PLACE_ENDPOINT = BASE_URL + PLACE_PARAMS;
class PlacesApiService {
Future<PlaceResponse> searchPlaces(String placeQuery) async {
final googlePlacesURL = SEARCH_PLACE_ENDPOINT + placeQuery;
print(googlePlacesURL);
// Retrieve the locations of Google offices
final response = await http.get(googlePlacesURL);
if (response.statusCode == 200) {
Expand Down
129 changes: 129 additions & 0 deletions lib/pages/places/places_models.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions lib/pages/places/search_places.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'package:flutter/material.dart';
import 'package:flutter_app/pages/map/map_bloc.dart';
import 'package:flutter_app/pages/map/place_widget.dart';
import 'package:flutter_app/pages/places/places_models.dart';

class SearchPlaces extends SearchDelegate<Candidate> {


SearchPlaces(this._mapBloc);

final MapBloc _mapBloc;

@override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
icon: Icon(Icons.close),
onPressed: () {
close(context, null);
},
)
];
}

@override
Widget buildLeading(BuildContext context) {
return IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
close(context, null);
},
);
}

@override
Widget buildResults(BuildContext context) {
return _buildPlaceStream();
}

@override
Widget buildSuggestions(BuildContext context) {
_mapBloc.fetchLocations(query);
return _buildPlaceStream();
}

StreamBuilder _buildPlaceStream() {
return StreamBuilder<List<Candidate>>(
stream: _mapBloc.offices,
builder: (context, snapshot) {
if (snapshot.hasData) {
return PlaceWidget(snapshot.data);
}
return CircularProgressIndicator();
},
);
}
}

0 comments on commit 663110e

Please sign in to comment.