DRY up some search and places code
This commit is contained in:
@ -2,17 +2,28 @@ import 'dart:async';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:sendtrain/services/apis/google_places_service.dart';
|
||||
import 'package:sendtrain/services/functional/debouncer.dart';
|
||||
import 'package:sendtrain/widgets/generic/elements/form_text_input.dart';
|
||||
|
||||
class Suggestion<T> {
|
||||
T type;
|
||||
|
||||
Suggestion(this.type);
|
||||
}
|
||||
|
||||
class FormSearchInput extends StatefulWidget {
|
||||
const FormSearchInput(
|
||||
{super.key, required this.sessionController, required this.service, this.requestCallback, this.optionalPayload});
|
||||
{super.key,
|
||||
required this.sessionController,
|
||||
required this.service,
|
||||
this.title,
|
||||
this.callback,
|
||||
this.optionalPayload});
|
||||
|
||||
final String? title;
|
||||
final TextEditingController sessionController;
|
||||
final dynamic service;
|
||||
final Function? requestCallback;
|
||||
final Function? callback;
|
||||
final dynamic optionalPayload;
|
||||
|
||||
@override
|
||||
@ -23,7 +34,7 @@ class _FormSearchInputState extends State<FormSearchInput> {
|
||||
String? _currentQuery;
|
||||
|
||||
late final service = widget.service;
|
||||
late final requestCallback = widget.requestCallback;
|
||||
late final callback = widget.callback;
|
||||
// The most recent suggestions received from the API.
|
||||
late Iterable<Widget> _lastOptions = <Widget>[];
|
||||
late final Debouncer debouncer;
|
||||
@ -68,7 +79,7 @@ class _FormSearchInputState extends State<FormSearchInput> {
|
||||
builder: (BuildContext context, SearchController controller) {
|
||||
return FormTextInput(
|
||||
controller: widget.sessionController,
|
||||
title: 'Location (optional)',
|
||||
title: widget.title ?? "",
|
||||
icon: Icon(Icons.search_rounded),
|
||||
maxLines: 2,
|
||||
requiresValidation: false,
|
||||
@ -84,24 +95,15 @@ class _FormSearchInputState extends State<FormSearchInput> {
|
||||
}
|
||||
_lastOptions = List<ListTile>.generate(options.length, (int index) {
|
||||
final Suggestion item = options[index];
|
||||
final dynamic content = item.type;
|
||||
return ListTile(
|
||||
title: Text(item.description),
|
||||
title: Text(content.description),
|
||||
onTap: () async {
|
||||
// widget.optionalPayload = service.fetchPhoto(json.decode(item.image));
|
||||
if (item.imageReferences != null) {
|
||||
// get a random photo item from the returned result
|
||||
Map<String, dynamic> photo = item.imageReferences![
|
||||
Random().nextInt(item.imageReferences!.length)];
|
||||
|
||||
await service.fetchPhoto(photo['name']).then((photoMap) {
|
||||
widget.optionalPayload.photoUri = photoMap['photoUri'];
|
||||
});
|
||||
if (callback != null) {
|
||||
callback!(content, service);
|
||||
}
|
||||
|
||||
widget.optionalPayload.address = item.address;
|
||||
widget.sessionController.text = item.description;
|
||||
service.finish();
|
||||
controller.closeView(item.description);
|
||||
controller.closeView(null);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
Reference in New Issue
Block a user