further refactoring and location search dev
This commit is contained in:
@ -1,7 +1,29 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:sendtrain/widgets/generic/elements/form_text_input.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
class Suggestion {
|
||||
final String placeId;
|
||||
final String description;
|
||||
final String address;
|
||||
|
||||
Suggestion(this.placeId, this.description, this.address);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Suggestion(description: $description, placeId: $placeId)';
|
||||
}
|
||||
|
||||
Map toJson() => {
|
||||
'placeId': placeId,
|
||||
'name': description,
|
||||
'address': address,
|
||||
};
|
||||
}
|
||||
|
||||
class FormSearchInput extends StatefulWidget {
|
||||
const FormSearchInput({super.key, required this.sessionController});
|
||||
@ -26,15 +48,55 @@ class _FormSearchInputState extends State<FormSearchInput> {
|
||||
_currentQuery = query;
|
||||
|
||||
// In a real application, there should be some error handling here.
|
||||
final Iterable<String> options = await _FakeAPI.search(_currentQuery!);
|
||||
// final Iterable<String> options = await _FakeAPI.search(_currentQuery!);
|
||||
if (query.isNotEmpty) {
|
||||
final List<Suggestion>? suggestions =
|
||||
await fetchSuggestions(_currentQuery!, 'en');
|
||||
|
||||
// If another search happened after this one, throw away these options.
|
||||
if (_currentQuery != query) {
|
||||
// If another search happened after this one, throw away these options.
|
||||
if (_currentQuery != query) {
|
||||
return null;
|
||||
}
|
||||
_currentQuery = null;
|
||||
|
||||
return suggestions?.map((suggestion) => json.encode(suggestion));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
_currentQuery = null;
|
||||
}
|
||||
|
||||
return options;
|
||||
final sessionToken = Uuid().v4();
|
||||
final apiKey = "AIzaSyBCjMCEAyyNVpsnVYvZj6VL1mmB98Vd6AE";
|
||||
final client = Client();
|
||||
|
||||
Future<List<Suggestion>?> fetchSuggestions(String input, String lang) async {
|
||||
var headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Goog-Api-Key': apiKey,
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
'X-Goog-FieldMask': 'places.displayName,places.id,places.formattedAddress'
|
||||
};
|
||||
var request = Request('POST',
|
||||
Uri.parse('https://places.googleapis.com/v1/places:searchText'));
|
||||
request.body = json.encode({"textQuery": input});
|
||||
request.headers.addAll(headers);
|
||||
|
||||
StreamedResponse response = await request.send();
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final result = json.decode(await response.stream.bytesToString());
|
||||
|
||||
if (result.isNotEmpty) {
|
||||
return result['places']
|
||||
.map<Suggestion>((p) => Suggestion(
|
||||
p['id'], p['displayName']['text'], p['formattedAddress']))
|
||||
.toList();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
throw Exception(response.reasonPhrase);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@ -46,64 +108,44 @@ class _FormSearchInputState extends State<FormSearchInput> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SearchAnchor(
|
||||
builder: (BuildContext context, SearchController controller) {
|
||||
return FormTextInput(
|
||||
controller: widget.sessionController,
|
||||
title: 'Location (optional)',
|
||||
icon: Icon(Icons.search_rounded),
|
||||
maxLines: 2,
|
||||
onTap: () {
|
||||
controller.openView();
|
||||
});
|
||||
}, suggestionsBuilder:
|
||||
(BuildContext context, SearchController controller) async {
|
||||
final List<String>? options =
|
||||
(await _debouncedSearch(controller.text))?.toList();
|
||||
if (options == null) {
|
||||
return _lastOptions;
|
||||
}
|
||||
_lastOptions = List<ListTile>.generate(options.length, (int index) {
|
||||
final String item = options[index];
|
||||
return ListTile(
|
||||
title: Text(item),
|
||||
onTap: () {
|
||||
if (item.isNotEmpty) {
|
||||
widget.sessionController.text = item;
|
||||
}
|
||||
controller.closeView(item);
|
||||
// debugPrint('You just selected $item');
|
||||
// Navigator.of(context).pop();
|
||||
},
|
||||
);
|
||||
builder: (BuildContext context, SearchController controller) {
|
||||
return FormTextInput(
|
||||
controller: widget.sessionController,
|
||||
title: 'Location (optional)',
|
||||
icon: Icon(Icons.search_rounded),
|
||||
maxLines: 2,
|
||||
onTap: () {
|
||||
controller.openView();
|
||||
});
|
||||
}, suggestionsBuilder:
|
||||
(BuildContext context, SearchController controller) async {
|
||||
final List<String>? options =
|
||||
(await _debouncedSearch(controller.text))?.toList();
|
||||
if (options == null) {
|
||||
return _lastOptions;
|
||||
}
|
||||
_lastOptions = List<ListTile>.generate(options.length, (int index) {
|
||||
final String item = options[index];
|
||||
return ListTile(
|
||||
title: Text(json.decode(item)['name']),
|
||||
onTap: () {
|
||||
if (item.isNotEmpty) {
|
||||
widget.sessionController.text = json.decode(item)['name'];
|
||||
}
|
||||
client.close();
|
||||
controller.closeView(item);
|
||||
// debugPrint('You just selected $item');
|
||||
// Navigator.of(context).pop();
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
return _lastOptions;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const Duration fakeAPIDuration = Duration(seconds: 1);
|
||||
const Duration debounceDuration = Duration(milliseconds: 500);
|
||||
|
||||
class _FakeAPI {
|
||||
static const List<String> _kOptions = <String>[
|
||||
'aardvark',
|
||||
'bobcat',
|
||||
'chameleon',
|
||||
];
|
||||
|
||||
// Searches the options, but injects a fake "network" delay.
|
||||
static Future<Iterable<String>> search(String query) async {
|
||||
await Future<void>.delayed(fakeAPIDuration); // Fake 1 second delay.
|
||||
if (query == '') {
|
||||
return const Iterable<String>.empty();
|
||||
}
|
||||
return _kOptions.where((String option) {
|
||||
return option.contains(query.toLowerCase());
|
||||
return _lastOptions;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const Duration debounceDuration = Duration(milliseconds: 500);
|
||||
typedef _Debounceable<S, T> = Future<S?> Function(T parameter);
|
||||
|
||||
/// Returns a new function that is a debounced version of the given function.
|
||||
|
Reference in New Issue
Block a user