-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
pagination.dart
153 lines (143 loc) · 4.87 KB
/
pagination.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import 'package:example/user_data.dart';
import 'package:example/user_model.dart';
import 'package:flutter/material.dart';
import 'package:searchfield/searchfield.dart';
class Pagination extends StatefulWidget {
const Pagination({Key? key}) : super(key: key);
@override
State<Pagination> createState() => _PaginationState();
}
class _PaginationState extends State<Pagination> {
final focus = FocusNode();
Future<List<UserModel>> getPaginatedSuggestions({int pageLength = 5}) async {
await Future.delayed(const Duration(seconds: 2));
int total = suggestions.length;
return List.generate(pageLength, (index) {
total++;
return UserModel.fromJson(users_data[total % users_data.length]);
});
}
static const surfaceGreen = Color.fromARGB(255, 237, 255, 227);
static const surfaceBlue = Color(0xffd3e8fb);
static const skyBlue = Color(0xfff3ddec);
var suggestions = <UserModel>[];
static const gradient = LinearGradient(
colors: [surfaceBlue, surfaceGreen, skyBlue],
stops: [0.25, 0.35, 0.9],
begin: Alignment.bottomRight,
end: Alignment.topLeft,
);
final suggestionDecoration = SuggestionDecoration(
border: Border.all(color: Colors.grey),
gradient: gradient,
borderRadius: BorderRadius.circular(24),
);
bool isLoading = false;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
final result = await getPaginatedSuggestions();
if (mounted) {
setState(() {
suggestions.addAll(result);
});
}
});
}
var selectedValue = null;
@override
Widget build(BuildContext context) {
Widget searchChild(UserModel user) => Padding(
padding: const EdgeInsets.symmetric(vertical: 4.0, horizontal: 12),
child: Text(user.name,
style: TextStyle(fontSize: 16, color: Colors.black)),
);
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SearchField<UserModel>(
onSearchTextChanged: (query) {
final filter = suggestions
.where((user) =>
user.name.toLowerCase().contains(query.toLowerCase()))
.toList();
return filter
.map((e) => SearchFieldListItem<UserModel>(e.name,
child: searchChild(e)))
.toList();
},
animationDuration: Duration.zero,
showEmpty: isLoading,
onScroll: (offset, maxOffset) async {
if (offset < maxOffset) return;
setState(() {
isLoading = true;
});
final result = await getPaginatedSuggestions();
setState(() {
suggestions.addAll(result);
isLoading = false;
});
},
selectedValue: selectedValue,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if (value == null || value.length < 4) {
return 'error';
}
return null;
},
emptyWidget: Container(
decoration: suggestionDecoration,
height: 200, // item*maxItems
child: const Center(
child: CircularProgressIndicator(
color: Colors.black,
))),
key: const Key('searchfield'),
hint: 'Load Paginated suggestions from network',
itemHeight: 50,
maxSuggestionsInViewPort: 4,
scrollbarDecoration: ScrollbarDecoration(),
onTapOutside: (x) {},
suggestionStyle: const TextStyle(fontSize: 20, color: Colors.black),
searchInputDecoration: SearchInputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(24),
borderSide: const BorderSide(
width: 1,
color: Colors.grey,
style: BorderStyle.solid,
),
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(24),
borderSide: const BorderSide(
width: 1,
color: Colors.black,
style: BorderStyle.solid,
),
),
fillColor: Colors.white,
contentPadding: const EdgeInsets.symmetric(
horizontal: 20,
),
),
suggestionsDecoration: suggestionDecoration,
suggestions: suggestions
.map((e) =>
SearchFieldListItem<UserModel>(e.name, child: searchChild(e)))
.toList(),
focusNode: focus,
suggestionState: Suggestion.expand,
onSuggestionTap: (SearchFieldListItem<UserModel> x) {
setState(() {
selectedValue = x;
});
},
),
],
);
}
}