-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsort_and_search.dart
126 lines (112 loc) · 3.36 KB
/
sort_and_search.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import 'package:async_controller/async_controller.dart';
import 'package:faker/faker.dart';
import 'package:flutter/material.dart';
import 'helpers.dart';
enum Sorting { ascending, descending }
class SearchingController extends FilteringAsyncController<String> {
@override
Future<List<String>> fetchBase() async {
await Future<void>.delayed(Duration(seconds: 1));
final faker = Faker();
return List.generate(100, (_) => faker.person.name());
}
@override
Future<List<String>> transform(List<String> data) async {
final result = data.toList();
// apply searching
if (_searchText?.isNotEmpty == true) {
final searchingFor = _searchText!.toLowerCase();
bool shouldRemove(String x) {
return !x.toLowerCase().contains(searchingFor);
}
result.removeWhere(shouldRemove);
}
// apply sorting
result.sort((lhs, rhs) {
if (sorting == Sorting.ascending) {
return lhs.compareTo(rhs);
} else {
return rhs.compareTo(lhs);
}
});
return result;
}
String? _searchText;
Sorting sorting = Sorting.ascending;
void setText(String value) {
_searchText = value;
setNeedsLocalTransform();
}
void toggleSorting() {
if (sorting == Sorting.ascending) {
sorting = Sorting.descending;
} else {
sorting = Sorting.ascending;
}
setNeedsLocalTransform();
}
}
class SortAndSearchPage extends StatefulWidget with ExamplePage {
@override
String get title => 'Sort & search';
@override
_SortAndSearchPageState createState() => _SortAndSearchPageState();
}
class _SortAndSearchPageState extends State<SortAndSearchPage> {
final _controller = SearchingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: widget.buildAppBar(),
body: Column(
children: <Widget>[
Stack(
children: <Widget>[
TextField(
onChanged: _controller.setText,
decoration: InputDecoration(
hintText: 'Search',
prefixIcon: Icon(Icons.search),
),
),
Align(
alignment: Alignment.centerRight,
child: TextButton(
onPressed: _controller.toggleSorting,
child: _controller.buildAsyncProperty<Sorting>(
selector: () => _controller.sorting,
builder: (context, sorting) {
final asc = sorting == Sorting.ascending;
if (asc) {
return Text('A -> Z');
} else {
return Text('Z -> A');
}
},
),
),
),
],
),
Expanded(
child: _controller.buildAsyncData(
builder: (context, data) {
return ListView.builder(
itemCount: data.length,
itemBuilder: (context, i) {
return ListTile(
title: Text(data[i]),
);
},
);
},
decorator: const PagedListDecoration(
noDataContent: Text('I found nothing...'),
),
),
),
],
),
);
}
}