-
Notifications
You must be signed in to change notification settings - Fork 64
/
42.flutter_listview_search.dart
127 lines (109 loc) · 3.62 KB
/
42.flutter_listview_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
127
import 'package:flutter/material.dart';
import 'package:ig_posts/season_2/63.transperant_design_pattern.dart';
import 'package:ig_posts/utils/colors.dart';
import 'package:provider/provider.dart';
class Item {
final int itemId;
final String itemTitle;
const Item({
required this.itemId,
required this.itemTitle,
});
}
class CustomSearchNotifier extends ChangeNotifier {
final List<Item> items = [
Item(itemId: 0, itemTitle: "Apple"),
Item(itemId: 1, itemTitle: "Banana"),
Item(itemId: 2, itemTitle: "Pineapple"),
Item(itemId: 3, itemTitle: "Zebra"),
];
final List<Item> queriedItems = [];
String? query;
void setQuery({required String value}) {
query = value;
notifyListeners();
}
void queryData() {
queriedItems.clear();
for (Item item in items) {
if (query != null) {
if (!queriedItems.contains(item)) {
if (item.itemTitle.toLowerCase().contains(query!.toLowerCase())) {
queriedItems.add(item);
notifyListeners();
}
}
}
}
}
void clearSearch() {
queriedItems.clear();
query = null;
notifyListeners();
}
}
class CustomFlutterSearchView extends StatefulWidget {
const CustomFlutterSearchView({super.key});
@override
State<CustomFlutterSearchView> createState() =>
_CustomFlutterSearchViewState();
}
class _CustomFlutterSearchViewState extends State<CustomFlutterSearchView> {
CustomSearchNotifier customSearchNotifier({required bool renderUI}) =>
Provider.of<CustomSearchNotifier>(context, listen: renderUI);
final TextEditingController textEditingController = TextEditingController();
@override
void dispose() {
textEditingController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
bool isQueryNull = customSearchNotifier(renderUI: true).query == null;
List<Item> items = isQueryNull
? customSearchNotifier(renderUI: true).items
: customSearchNotifier(renderUI: true).queriedItems;
return Scaffold(
backgroundColor: KConstantColors.bgColor,
appBar: CustomAppBar(title: "Flutter Search"),
body: Column(
children: [
SizedBox(height: 20),
TextField(
onChanged: (val) {
if (val != "") {
customSearchNotifier(renderUI: false).setQuery(value: val);
customSearchNotifier(renderUI: false).queryData();
} else {
customSearchNotifier(renderUI: false).clearSearch();
}
},
style: KConstantTextstyles.light(fontSize: 12),
controller: textEditingController,
decoration: InputDecoration(
hintStyle: TextStyle(color: KConstantColors.whiteColor),
hintText: "Search anything..."),
),
SizedBox(height: 20),
ListView.builder(
shrinkWrap: true,
itemCount: items.length,
itemBuilder: (BuildContext context, int index) {
Item item = items[index];
return ListTile(
leading: CircleAvatar(
backgroundColor: KConstantColors.bgColorFaint,
child: Center(
child: Text(item.itemId.toString(),
style: KConstantTextstyles.light(fontSize: 12)),
),
),
title: Text(item.itemTitle,
style: KConstantTextstyles.light(fontSize: 12)),
);
},
),
],
));
}
}