forked from vandadnp/flutter-tips-and-tricks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filterchip-in-flutter.dart
156 lines (138 loc) · 3.49 KB
/
filterchip-in-flutter.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
154
155
156
// 🐦 Twitter https://twitter.com/vandadnp
// 🔵 LinkedIn https://linkedin.com/in/vandadnp
// 🎥 YouTube https://youtube.com/c/vandadnp
// 💙 Free Flutter Course https://linktr.ee/vandadnp
// 📦 11+ Hours Bloc Course https://youtu.be/Mn254cnduOY
// 🤝 Want to support my work? https://buymeacoffee.com/vandad
import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
),
);
}
enum TypeOfThing { animal, person, object }
abstract class Thing {
final TypeOfThing type;
final String name;
const Thing({
required this.type,
required this.name,
});
}
class Person extends Thing {
const Person({
required String name,
}) : super(
name: name,
type: TypeOfThing.person,
);
}
class Animal extends Thing {
const Animal({
required String name,
}) : super(
name: name,
type: TypeOfThing.animal,
);
}
class Object extends Thing {
const Object({
required String name,
}) : super(
name: name,
type: TypeOfThing.object,
);
}
const things = [
Person(name: 'Foo'),
Person(name: 'Bar'),
Animal(name: 'Goof ball'),
Animal(name: 'Fluffers'),
Object(name: 'Chair'),
Object(name: 'Table'),
];
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('FilterChip in Flutter'),
),
body: const Padding(
padding: EdgeInsets.all(8.0),
child: ListOfThings(),
),
);
}
}
class ListOfThings extends StatefulWidget {
const ListOfThings({Key? key}) : super(key: key);
@override
State<ListOfThings> createState() => _ListOfThingsState();
}
class _ListOfThingsState extends State<ListOfThings> {
TypeOfThing? filter;
Iterable<Thing> get filteredThings => filter != null
? things.where(
(thing) => thing.type == filter,
)
: things;
@override
Widget build(BuildContext context) {
return Column(
children: [
Wrap(
spacing: 10,
children: TypeOfThing.values.map(
(typeOfThing) {
return FilterChip(
selectedColor: Colors.blueAccent[100],
checkmarkColor: Colors.white,
onSelected: (value) {
setState(
() => filter = (value ? typeOfThing : null),
);
},
label: Text(typeOfThing.name),
selected: filter == typeOfThing,
);
},
).toList(),
),
const SizedBox(height: 10),
ThingsList(
filteredThings: filteredThings,
)
],
);
}
}
class ThingsList extends StatelessWidget {
const ThingsList({
Key? key,
required this.filteredThings,
}) : super(key: key);
final Iterable<Thing> filteredThings;
@override
Widget build(BuildContext context) {
return Expanded(
child: ListView.builder(
itemCount: filteredThings.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(
filteredThings.elementAt(index).name,
),
subtitle: Text(
filteredThings.elementAt(index).type.name,
),
);
},
),
);
}
}