201 lines
5.7 KiB
Dart
201 lines
5.7 KiB
Dart
part of '../widgets.dart';
|
|
|
|
/// -----------------------------------------------
|
|
/// [SearchView]
|
|
|
|
class SearchView<T> extends StatefulComponent {
|
|
const SearchView({super.key});
|
|
|
|
@override
|
|
State<SearchView> createState() => _SearchViewState();
|
|
}
|
|
|
|
class _SearchViewState<T> extends State<SearchView> {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return const Placeholder();
|
|
}
|
|
}
|
|
|
|
class LocalSearchView<T> extends SearchView<T> {
|
|
final List<T> list;
|
|
final Widget Function(T) itemBuilder;
|
|
final bool Function(T, String) filter;
|
|
final Widget header;
|
|
final List<T> Function(String)? onSearch;
|
|
|
|
LocalSearchView({
|
|
Key? key,
|
|
required this.list,
|
|
required this.itemBuilder,
|
|
required this.filter,
|
|
List<T> Function(String)? onSearch,
|
|
Widget? header,
|
|
}) : header = header ?? const SizedBox.shrink(),
|
|
onSearch = onSearch ??
|
|
((String query) =>
|
|
list.where((documents) => filter(documents, query)).toList()),
|
|
super(key: key);
|
|
|
|
// return documents.where((documents) => filter(documents, query)).toList();
|
|
|
|
@override
|
|
LocalSearchViewState<T> createState() => LocalSearchViewState<T>();
|
|
}
|
|
|
|
class LocalSearchViewState<T> extends State<LocalSearchView<T>> {
|
|
TextEditingController editingController = TextEditingController();
|
|
late List<T> filteredItems;
|
|
|
|
@override
|
|
void initState() {
|
|
filteredItems = widget.list;
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
void filter(value) {
|
|
safeSetState(() {
|
|
filteredItems = widget.onSearch!(value);
|
|
});
|
|
}
|
|
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
mainAxisSize: MainAxisSize.max,
|
|
children: <Widget>[
|
|
Expanded(
|
|
child: ListView.builder(
|
|
shrinkWrap: true,
|
|
itemCount: filteredItems.length + 1,
|
|
itemBuilder: (context, index) {
|
|
if (index == 0) return widget.header;
|
|
return widget.itemBuilder(filteredItems[index - 1]);
|
|
},
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.all(30.0),
|
|
child: TextField(
|
|
onChanged: filter,
|
|
controller: editingController,
|
|
cursorColor: Colors.black,
|
|
decoration: InputDecoration(
|
|
prefixIcon: Icon(Icons.search),
|
|
focusColor: Colors.black,
|
|
hoverColor: Colors.black,
|
|
fillColor: Colors.black,
|
|
iconColor: Colors.black,
|
|
contentPadding:
|
|
EdgeInsets.symmetric(vertical: 10.0, horizontal: 10.0),
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.all(Radius.circular(15.0)),
|
|
borderSide:
|
|
BorderSide(color: Colors.black), // Set border color here
|
|
),
|
|
focusedBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.all(Radius.circular(15.0)),
|
|
borderSide: BorderSide(
|
|
color: Colors.black), // Set focused border color here
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
class RemoteSearchListView<T> extends SearchView<T> {
|
|
final List<T> items;
|
|
final String title;
|
|
final Widget Function(T) itemBuilder;
|
|
final Future<List<T>> Function(String) fetchItems;
|
|
|
|
RemoteSearchListView({
|
|
Key? key,
|
|
required this.items,
|
|
required this.title,
|
|
required this.itemBuilder,
|
|
required this.fetchItems,
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
_RemoteSearchViewState<T> createState() => _RemoteSearchViewState<T>();
|
|
}
|
|
|
|
class _RemoteSearchViewState<T> extends State<RemoteSearchListView<T>> {
|
|
TextEditingController editingController = TextEditingController();
|
|
late List<T> filteredItems;
|
|
bool isLoading = false;
|
|
|
|
@override
|
|
void initState() {
|
|
filteredItems = widget.items;
|
|
super.initState();
|
|
}
|
|
|
|
void filterSearchResults(String query) async {
|
|
setState(() {
|
|
isLoading = true;
|
|
});
|
|
final results = await widget.fetchItems(query);
|
|
setState(() {
|
|
filteredItems = results;
|
|
isLoading = false;
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(widget.title),
|
|
),
|
|
body: Container(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
mainAxisSize: MainAxisSize.max,
|
|
children: <Widget>[
|
|
Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: TextField(
|
|
onChanged: (value) {
|
|
filterSearchResults(value);
|
|
},
|
|
controller: editingController,
|
|
decoration: InputDecoration(
|
|
labelText: "Search",
|
|
hintText: "Search",
|
|
prefixIcon: Icon(Icons.search),
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.all(Radius.circular(25.0)),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(15, 0, 50, 0),
|
|
child: Text('Últimos Documentos'),
|
|
),
|
|
isLoading
|
|
? Center(child: CircularProgressIndicator())
|
|
: Expanded(
|
|
child: ListView.builder(
|
|
shrinkWrap: true,
|
|
itemCount: filteredItems.length,
|
|
itemBuilder: (context, index) {
|
|
return widget.itemBuilder(filteredItems[index]);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|