-
Notifications
You must be signed in to change notification settings - Fork 64
/
29.flutter_dynamic_search.dart
121 lines (111 loc) · 3.66 KB
/
29.flutter_dynamic_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
import 'package:flutter/material.dart';
import 'package:ig_posts/features/1.flutter_frost_glass.view.dart';
import 'package:ig_posts/features/5.flutter_swiggy_button.dart';
import 'package:provider/provider.dart';
class DynamicSearchNotifier extends ChangeNotifier {
List<String> fakeData = [
"🍔 Burger",
"🍟 Fries",
"🍗 Chicken",
"🍅 Ketchup",
"🍗 Meat",
"💧 Water",
"🍕 Pizza",
"🍜 Noodles",
"🍨 Ice cream",
"🍊 Orange",
"🌭 Sausage",
"🍱 Meal"
];
List<String> queriedData = [];
String _query = "";
String get query => _query;
void setQuery({required String kValue}) {
_query = kValue;
notifyListeners();
}
void queryData() {
queriedData.clear();
for (String data in fakeData) {
if (data.contains(query)) {
queriedData.add(data);
notifyListeners();
}
}
}
void clearSearch() {
queriedData.clear();
notifyListeners();
}
}
class FlutterDyanmicSearchWidget extends StatelessWidget {
const FlutterDyanmicSearchWidget({super.key});
@override
Widget build(BuildContext context) {
DynamicSearchNotifier dynamicSearchNotifier({required bool renderUI}) =>
Provider.of<DynamicSearchNotifier>(context, listen: renderUI);
return Scaffold(
backgroundColor: bgColor,
body: Container(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 10),
SizedBox(
width: 400,
child: TextField(
style: regulerText,
onChanged: (val) {
if (val == "") {
dynamicSearchNotifier(renderUI: false).clearSearch();
} else {
dynamicSearchNotifier(renderUI: false)
.setQuery(kValue: val);
dynamicSearchNotifier(renderUI: false).queryData();
}
},
decoration: InputDecoration(
hintStyle: regulerText, hintText: "Enter any food item"),
),
),
// if (dynamicSearchNotifier(renderUI: true).queriedData.isEmpty)
// Center(
// child: Padding(
// padding: const EdgeInsets.only(top: 200),
// child: Text("No data found", style: boldText()),
// )),
// if (dynamicSearchNotifier(renderUI: true).queriedData.isNotEmpty)
ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount:
dynamicSearchNotifier(renderUI: true).queriedData.length,
itemBuilder: (BuildContext context, int index) {
String data =
dynamicSearchNotifier(renderUI: true).queriedData[index];
return Padding(
padding: const EdgeInsets.all(4.0),
child: ListTile(
tileColor: bgColorFaint,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25)),
title: Text(data, style: regulerText),
),
);
},
),
],
),
),
),
appBar: AppBar(
backgroundColor: bgColorFaint,
title: Text(
"Flutter static search",
style: boldText(),
)),
);
}
}