flutter_google_places 0.1.3
flutter_google_places: ^0.1.3 copied to clipboard
Google places autocomplete widgets for flutter. No wrapper, use https://pub.dartlang.org/packages/google_maps_webservice
example/lib/main.dart
import 'dart:async';
import 'package:google_maps_webservice/places.dart';
import 'package:flutter_google_places/flutter_google_places.dart';
import 'package:flutter/material.dart';
const kGoogleApiKey = "API_KEY";
// to get places detail (lat/lng)
GoogleMapsPlaces _places = GoogleMapsPlaces(kGoogleApiKey);
main() {
runApp(RoutesWidget());
}
class RoutesWidget extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(
title: "My App",
theme: ThemeData(
primarySwatch: Colors.blue,
brightness: Brightness.dark,
accentColor: Colors.redAccent,
inputDecorationTheme: InputDecorationTheme(
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(4.00)),
),
contentPadding: EdgeInsets.symmetric(
vertical: 12.50,
horizontal: 10.00,
),
),
),
routes: {
"/": (_) => MyApp(),
"/search": (_) => CustomSearchScaffold(),
},
);
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
final homeScaffoldKey = GlobalKey<ScaffoldState>();
final searchScaffoldKey = GlobalKey<ScaffoldState>();
class _MyAppState extends State<MyApp> {
Mode _mode = Mode.overlay;
@override
Widget build(BuildContext context) {
return Scaffold(
key: homeScaffoldKey,
appBar: AppBar(
title: Text("My App"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
DropdownButton(
value: _mode,
items: <DropdownMenuItem<Mode>>[
DropdownMenuItem<Mode>(
child: Text("Overlay"), value: Mode.overlay),
DropdownMenuItem<Mode>(
child: Text("Fullscreen"), value: Mode.fullscreen),
],
onChanged: (m) {
setState(() {
_mode = m;
});
}),
RaisedButton(
onPressed: () async {
// show input autocomplete with selected mode
// then get the Prediction selected
Prediction p = await PlacesAutocomplete.show(
context: context,
apiKey: kGoogleApiKey,
onError: (res) {
homeScaffoldKey.currentState.showSnackBar(
SnackBar(content: Text(res.errorMessage)));
},
mode: _mode,
language: "fr",
components: [Component(Component.country, "fr")]);
displayPrediction(p, homeScaffoldKey.currentState);
},
child: Text("Search places")),
RaisedButton(
child: Text("Custom"),
onPressed: () {
Navigator.of(context).pushNamed("/search");
},
),
],
)),
);
}
}
Future<Null> displayPrediction(Prediction p, ScaffoldState scaffold) async {
if (p != null) {
// get detail (lat/lng)
PlacesDetailsResponse detail = await _places.getDetailsByPlaceId(p.placeId);
final lat = detail.result.geometry.location.lat;
final lng = detail.result.geometry.location.lng;
scaffold
.showSnackBar(SnackBar(content: Text("${p.description} - $lat/$lng")));
}
}
// custom scaffold that handle search
// basically your widget need to extends [GooglePlacesAutocompleteWidget]
// and your state [GooglePlacesAutocompleteState]
class CustomSearchScaffold extends PlacesAutocompleteWidget {
CustomSearchScaffold()
: super(
apiKey: kGoogleApiKey,
language: "en",
components: [Component(Component.country, "uk")],
);
@override
_CustomSearchScaffoldState createState() => _CustomSearchScaffoldState();
}
class _CustomSearchScaffoldState extends PlacesAutocompleteState {
@override
Widget build(BuildContext context) {
final appBar = AppBar(title: AppBarPlacesAutoCompleteTextField());
final body = PlacesAutocompleteResult(onTap: (p) {
displayPrediction(p, searchScaffoldKey.currentState);
});
return Scaffold(key: searchScaffoldKey, appBar: appBar, body: body);
}
@override
void onResponseError(PlacesAutocompleteResponse response) {
super.onResponseError(response);
searchScaffoldKey.currentState.showSnackBar(
SnackBar(content: Text(response.errorMessage)),
);
}
@override
void onResponse(PlacesAutocompleteResponse response) {
super.onResponse(response);
if (response != null && response.predictions.isNotEmpty) {
searchScaffoldKey.currentState.showSnackBar(
SnackBar(content: Text("Got answer")),
);
}
}
}