Created
June 15, 2022 07:05
-
-
Save TasnuvaOshin/f0b547037c2f4af718207d58984c61f7 to your computer and use it in GitHub Desktop.
google places search in flutter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:google_api_headers/google_api_headers.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_google_places/flutter_google_places.dart'; | |
import 'package:google_maps_webservice/places.dart'; | |
import 'package:uuid/uuid.dart'; | |
const kGoogleApiKey = ""; //add your key here | |
main() { | |
runApp(RoutesWidget()); | |
} | |
class RoutesWidget extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) => MaterialApp( | |
title: "My App", | |
debugShowCheckedModeBanner: false, | |
routes: { | |
"/": (_) => CustomSearchScaffold(), | |
}, | |
); | |
} | |
Future<Null> displayPrediction(Prediction p, ScaffoldState scaffold) async { | |
if (p != null) { | |
// get detail (lat/lng) | |
GoogleMapsPlaces _places = GoogleMapsPlaces( | |
apiKey: kGoogleApiKey, | |
apiHeaders: await const GoogleApiHeaders().getHeaders(), | |
); | |
PlacesDetailsResponse detail = | |
await _places.getDetailsByPlaceId(p.placeId!); | |
final lat = detail.result.geometry!.location.lat; | |
final lng = detail.result.geometry!.location.lng; | |
/** | |
* Afther click on the search result, the app will display the result in the | |
*/ | |
// scaffold.showSnackBar( | |
// SnackBar(content: Text("${p.description} - $lat/$lng")), | |
// ); | |
} | |
} | |
class CustomSearchScaffold extends PlacesAutocompleteWidget { | |
CustomSearchScaffold() | |
: super( | |
apiKey: kGoogleApiKey, | |
sessionToken: Uuid().v4(), | |
language: "en", | |
types: ['address'], | |
components: [], | |
strictbounds: false); | |
@override | |
_CustomSearchScaffoldState createState() => _CustomSearchScaffoldState(); | |
} | |
class _CustomSearchScaffoldState extends PlacesAutocompleteState { | |
final homeScaffoldKey = GlobalKey<ScaffoldState>(); | |
final searchScaffoldKey = GlobalKey<ScaffoldState>(); | |
@override | |
Widget build(BuildContext context) { | |
final appBar = AppBar( | |
title: AppBarPlacesAutoCompleteTextField(), | |
); | |
final body = PlacesAutocompleteResult( | |
onTap: (p) { | |
displayPrediction(p, searchScaffoldKey.currentState!); | |
}, | |
logo: Container()); | |
return Scaffold( | |
key: searchScaffoldKey, | |
bottomNavigationBar: BottomSheet( | |
elevation: 12.0, | |
backgroundColor: Colors.white, | |
onClosing: () { | |
print("Bottom sheet closed"); | |
}, | |
builder: (context) { | |
return Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Container( | |
height: MediaQuery.of(context).size.height * 0.3, | |
child: body, | |
), | |
); | |
}, | |
), | |
appBar: appBar, | |
body: Container( | |
child: Text("Your Body"), | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment