-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy path13.flutter_filter_data.dart
142 lines (127 loc) · 4.56 KB
/
13.flutter_filter_data.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
import 'package:flutter/material.dart';
import 'package:ig_posts/features/1.flutter_frost_glass.view.dart';
import 'package:provider/provider.dart';
class FakeData {
final String title;
final String category;
const FakeData({required this.title, required this.category});
}
class FilterDataNotifier extends ChangeNotifier {
List<String> categories = ["animal", "bird", "fish"];
List<FakeData> data = const [
FakeData(title: "Lion", category: "animal"),
FakeData(title: "Elephant", category: "animal"),
FakeData(title: "Dolphin", category: "fish"),
FakeData(title: "Tiger", category: "animal"),
FakeData(title: "Eagle", category: "bird"),
FakeData(title: "Gold fish", category: "fish"),
FakeData(title: "Rabbit", category: "animal"),
FakeData(title: "Vulture", category: "bird"),
FakeData(title: "Swallow", category: "bird"),
FakeData(title: "Whale", category: "fish"),
FakeData(title: "Shark", category: "fish"),
FakeData(title: "Cat", category: "animal"),
FakeData(title: "Duck", category: "bird"),
FakeData(title: "Swallow", category: "bird"),
FakeData(title: "Pigeon", category: "bird"),
];
List<FakeData> queriedData = [];
bool isFilterMode = false;
String? _query;
String? get query => _query;
setQuery({required String kValue}) {
isFilterMode = true;
_query = kValue;
debugPrint(_query);
notifyListeners();
}
disableFilter() {
isFilterMode = false;
_query = null;
notifyListeners();
}
queryData() {
queriedData.clear();
for (FakeData kData in data) {
if (!queriedData.contains(kData)) {
if (_query != null) {
if (kData.category.contains(_query!)) {
queriedData.add(kData);
notifyListeners();
}
}
}
}
}
}
class FlutterFilterDataView extends StatelessWidget {
const FlutterFilterDataView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
FilterDataNotifier filterDataNotifier({required bool renderUI}) =>
Provider.of<FilterDataNotifier>(context, listen: renderUI);
List<FakeData> data = filterDataNotifier(renderUI: true).isFilterMode
? filterDataNotifier(renderUI: true).queriedData
: filterDataNotifier(renderUI: true).data;
List<String> categories = filterDataNotifier(renderUI: false).categories;
return Scaffold(
backgroundColor: bgColor,
appBar: AppBar(
backgroundColor: bgColorFaint,
title: const Text("Flutter Filter mode")),
floatingActionButton: filterDataNotifier(renderUI: true).isFilterMode
? FloatingActionButton(
backgroundColor: Colors.blue,
child: const Icon(Icons.clear),
onPressed: () {
filterDataNotifier(renderUI: false).disableFilter();
})
: const SizedBox.shrink(),
body: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 100,
width: 400,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: categories.length,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: ActionChip(
backgroundColor:
filterDataNotifier(renderUI: true).query ==
categories[index]
? Colors.blue
: bgColorFaint,
label: Text(categories[index]),
onPressed: () {
filterDataNotifier(renderUI: false)
.setQuery(kValue: categories[index]);
filterDataNotifier(renderUI: false).queryData();
}),
);
},
),
),
ListView.builder(
shrinkWrap: true,
itemCount: data.length,
itemBuilder: (BuildContext context, int index) {
FakeData fakeData = data[index];
return ListTile(
leading: const Icon(Icons.animation),
title: Text(fakeData.title),
subtitle: Text(fakeData.category),
);
},
),
],
),
),
);
}
}