-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy path30.flutter_selectable_tile.dart
157 lines (146 loc) · 5.51 KB
/
30.flutter_selectable_tile.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
157
import 'package:flutter/material.dart';
import 'package:ig_posts/utils/colors.dart';
import 'package:provider/provider.dart';
class SelectableTileNotifier extends ChangeNotifier {
List<String> _selectedTilesId = [];
List<String> get selectedTilesId => _selectedTilesId;
void selectTiles({required String tileId}) {
if (_selectedTilesId.contains(tileId)) {
_selectedTilesId.remove(tileId);
notifyListeners();
} else {
_selectedTilesId.add(tileId);
notifyListeners();
}
}
void clearAllTiles() {
selectedTilesId.clear();
notifyListeners();
}
void selectAllTiles({required List<String> ids}) {
selectedTilesId.clear();
selectedTilesId.addAll(ids);
notifyListeners();
}
}
class FlutterSelectableTilesView extends StatelessWidget {
const FlutterSelectableTilesView({super.key});
@override
Widget build(BuildContext context) {
SelectableTileNotifier selectableTileNotifier({required bool renderUI}) =>
Provider.of<SelectableTileNotifier>(context, listen: renderUI);
List<Map> data = [
{"title": "Cricket", "iconData": Icons.star},
{"title": "Football", "iconData": Icons.star},
{"title": "Tennis", "iconData": Icons.star},
{"title": "Swimming", "iconData": Icons.star},
{"title": "Karate", "iconData": Icons.star},
{"title": "Boxing", "iconData": Icons.star},
{"title": "Chess", "iconData": Icons.star},
{"title": "Badminton", "iconData": Icons.star},
];
tileBlock({required IconData iconData, required String title}) {
bool isSelected = selectableTileNotifier(renderUI: true)
.selectedTilesId
.contains(title);
return GestureDetector(
onTap: () {
selectableTileNotifier(renderUI: false).selectTiles(tileId: title);
},
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
CircleAvatar(
child: Icon(iconData, color: KConstantColors.whiteColor),
backgroundColor: KConstantColors.bgColor,
),
SizedBox(height: 5),
Text(title, style: KConstantTextstyles.light(fontSize: 10)),
],
),
decoration: BoxDecoration(
color: isSelected
? KConstantColors.blueColor
: KConstantColors.bgColorFaint,
borderRadius: BorderRadius.circular(25)),
),
),
);
}
return Scaffold(
appBar: AppBar(
backgroundColor: KConstantColors.blueColor,
title: Text("Flutter Selectable Tiles",
style: KConstantTextstyles.bold(fontSize: 15))),
backgroundColor: KConstantColors.bgColor,
body: Container(
child: Center(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
GridView.builder(
shrinkWrap: true,
itemCount: data.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4),
itemBuilder: (context, index) {
return tileBlock(
iconData: data[index]['iconData'],
title: data[index]['title'],
);
}),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (selectableTileNotifier(renderUI: true)
.selectedTilesId
.isNotEmpty)
ActionChip(
backgroundColor: KConstantColors.redColor,
label: Text("Clear all",
style: KConstantTextstyles.light(fontSize: 12)),
onPressed: () {
selectableTileNotifier(renderUI: false)
.clearAllTiles();
}),
SizedBox(width: 20),
ActionChip(
backgroundColor: KConstantColors.blueColor,
label: Text("Select all",
style: KConstantTextstyles.light(fontSize: 12)),
onPressed: () {
List<String> d = [];
data.forEach((element) {
d.add(element['title']);
});
selectableTileNotifier(renderUI: false)
.selectAllTiles(ids: d);
})
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (selectableTileNotifier(renderUI: true)
.selectedTilesId
.isNotEmpty)
ActionChip(
backgroundColor: KConstantColors.bgColorFaint,
label: Text("Continue",
style: KConstantTextstyles.light(fontSize: 12)),
onPressed: () {}),
],
)
],
),
),
),
),
);
}
}