flutter_google_places_hoc081098 2.0.0 flutter_google_places_hoc081098: ^2.0.0 copied to clipboard
Google places autocomplete widgets for flutter. No wrapper, use https://pub.dartlang.org/packages/google_maps_webservice. Better flutter_google_places, updated by @hoc081098
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_google_places_hoc081098/flutter_google_places_hoc081098.dart';
import 'package:flutter_google_places_hoc081098/google_maps_webservice_places.dart';
import 'package:google_api_headers/google_api_headers.dart';
import 'custom.dart';
const kGoogleApiKey = 'API_KEY';
void main() => runApp(const RoutesWidget());
final customTheme = ThemeData(
primarySwatch: Colors.blue,
brightness: Brightness.dark,
colorScheme: ColorScheme.fromSwatch(
primarySwatch: Colors.blue,
brightness: Brightness.dark,
accentColor: Colors.redAccent,
),
inputDecorationTheme: const InputDecorationTheme(
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(4.00)),
),
contentPadding: EdgeInsets.symmetric(
vertical: 12.50,
horizontal: 10.00,
),
),
useMaterial3: true,
);
class RoutesWidget extends StatelessWidget {
const RoutesWidget({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My App',
theme: customTheme,
routes: {
'/': (_) => const MyApp(),
'/search': (_) => CustomSearchScaffold(),
},
);
}
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Mode _mode = Mode.overlay;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('My App'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_buildDropdownMenu(),
const SizedBox(height: 12),
ElevatedButton(
onPressed: _handlePressButton,
child: const Text('Search places'),
),
const SizedBox(height: 12),
ElevatedButton(
onPressed: () {
Navigator.of(context).pushNamed('/search');
},
child: const Text('Custom'),
),
],
),
),
);
}
Widget _buildDropdownMenu() {
return DropdownButton<Mode>(
value: _mode,
items: const <DropdownMenuItem<Mode>>[
DropdownMenuItem<Mode>(
value: Mode.overlay,
child: Text('Overlay'),
),
DropdownMenuItem<Mode>(
value: Mode.fullscreen,
child: Text('Fullscreen'),
),
],
onChanged: (m) {
if (m != null) {
setState(() => _mode = m);
}
},
);
}
Future<void> _handlePressButton() async {
void onError(PlacesAutocompleteResponse response) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(response.errorMessage ?? 'Unknown error'),
),
);
}
// show input autocomplete with selected mode
// then get the Prediction selected
final p = await PlacesAutocomplete.show(
context: context,
apiKey: kGoogleApiKey,
onError: onError,
mode: _mode,
language: 'fr',
components: [const Component(Component.country, 'fr')],
resultTextStyle: Theme.of(context).textTheme.titleMedium,
);
if (!mounted) {
return;
}
await displayPrediction(p, ScaffoldMessenger.of(context));
}
}
Future<void> displayPrediction(
Prediction? p, ScaffoldMessengerState messengerState) async {
if (p == null) {
return;
}
// get detail (lat/lng)
final places = GoogleMapsPlaces(
apiKey: kGoogleApiKey,
apiHeaders: await const GoogleApiHeaders().getHeaders(),
);
final detail = await places.getDetailsByPlaceId(p.placeId!);
final geometry = detail.result.geometry!;
final lat = geometry.location.lat;
final lng = geometry.location.lng;
messengerState.showSnackBar(
SnackBar(
content: Text('${p.description} - $lat/$lng'),
),
);
}