SendTrain/lib/widgets/generic/elements/form_search_input.dart
2024-12-27 15:59:09 -05:00

157 lines
4.4 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:sendtrain/widgets/generic/elements/form_text_input.dart';
class FormSearchInput extends StatefulWidget {
const FormSearchInput({super.key, required this.sessionController});
final TextEditingController sessionController;
@override
State<FormSearchInput> createState() => _FormSearchInputState();
}
class _FormSearchInputState extends State<FormSearchInput> {
String? _currentQuery;
// The most recent suggestions received from the API.
late Iterable<Widget> _lastOptions = <Widget>[];
late final _Debounceable<Iterable<String>?, String> _debouncedSearch;
// Calls the "remote" API to search with the given query. Returns null when
// the call has been made obsolete.
Future<Iterable<String>?> _search(String query) async {
_currentQuery = query;
// In a real application, there should be some error handling here.
final Iterable<String> options = await _FakeAPI.search(_currentQuery!);
// If another search happened after this one, throw away these options.
if (_currentQuery != query) {
return null;
}
_currentQuery = null;
return options;
}
@override
void initState() {
super.initState();
_debouncedSearch = _debounce<Iterable<String>?, String>(_search);
}
@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();
},
);
});
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());
});
}
}
typedef _Debounceable<S, T> = Future<S?> Function(T parameter);
/// Returns a new function that is a debounced version of the given function.
///
/// This means that the original function will be called only after no calls
/// have been made for the given Duration.
_Debounceable<S, T> _debounce<S, T>(_Debounceable<S?, T> function) {
_DebounceTimer? debounceTimer;
return (T parameter) async {
if (debounceTimer != null && !debounceTimer!.isCompleted) {
debounceTimer!.cancel();
}
debounceTimer = _DebounceTimer();
try {
await debounceTimer!.future;
} on _CancelException {
return null;
}
return function(parameter);
};
}
// A wrapper around Timer used for debouncing.
class _DebounceTimer {
_DebounceTimer() {
_timer = Timer(debounceDuration, _onComplete);
}
late final Timer _timer;
final Completer<void> _completer = Completer<void>();
void _onComplete() {
_completer.complete();
}
Future<void> get future => _completer.future;
bool get isCompleted => _completer.isCompleted;
void cancel() {
_timer.cancel();
_completer.completeError(const _CancelException());
}
}
// An exception indicating that the timer was canceled.
class _CancelException implements Exception {
const _CancelException();
}