-
Notifications
You must be signed in to change notification settings - Fork 12
/
quick_links_view.dart
223 lines (205 loc) · 6.19 KB
/
quick_links_view.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Flutter imports:
import 'package:flutter/material.dart';
// Package imports:
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:reorderable_grid_view/reorderable_grid_view.dart';
import 'package:stacked/stacked.dart';
// Project imports:
import 'package:notredame/features/ets/quick-link/models/quick_link.dart';
import 'package:notredame/features/ets/quick-link/quick_links_viewmodel.dart';
import 'package:notredame/utils/app_theme.dart';
import 'package:notredame/features/app/widgets/base_scaffold.dart';
import 'package:notredame/features/app/widgets/web_link_card.dart';
class QuickLinksView extends StatefulWidget {
@override
_QuickLinksViewState createState() => _QuickLinksViewState();
}
class _QuickLinksViewState extends State<QuickLinksView>
with SingleTickerProviderStateMixin {
// Enable/Disable the edit state
bool _editMode = false;
// Animation Controller for Shake Animation
late AnimationController _controller;
late Animation<double> _animation;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(milliseconds: 250),
vsync: this,
);
_animation = Tween<double>(begin: -0.05, end: 0.05).animate(_controller);
}
@override
Widget build(BuildContext context) =>
ViewModelBuilder<QuickLinksViewModel>.reactive(
viewModelBuilder: () => QuickLinksViewModel(AppIntl.of(context)!),
builder: (context, model, child) => BaseScaffold(
isLoading: model.isBusy,
appBar: _buildAppBar(context, model),
body: _buildBody(context, model),
),
);
AppBar _buildAppBar(BuildContext context, QuickLinksViewModel model) {
return AppBar(
title: Text(AppIntl.of(context)!.title_ets),
automaticallyImplyLeading: false,
actions: const [],
);
}
Widget _buildBody(BuildContext context, QuickLinksViewModel model) {
return GestureDetector(
onTap: () {
if (_editMode) {
_controller.reset();
setState(() {
_editMode = false;
});
}
},
child: SafeArea(
child: Column(
children: [
Expanded(
flex: 2,
child: Padding(
padding: const EdgeInsets.only(top: 8.0, left: 8.0, right: 8.0),
child: _buildReorderableGridView(
model, model.quickLinkList, _buildDeleteButton),
),
),
if (_editMode && model.deletedQuickLinks.isNotEmpty) ...[
const Divider(
thickness: 2,
indent: 10,
endIndent: 10,
),
Expanded(
child: Padding(
padding:
const EdgeInsets.only(top: 8.0, left: 8.0, right: 8.0),
child: _buildReorderableGridView(
model, model.deletedQuickLinks, _buildAddButton),
),
),
],
],
),
),
);
}
ReorderableGridView _buildReorderableGridView(
QuickLinksViewModel model,
List<QuickLink> quickLinks,
Widget Function(QuickLinksViewModel, int) buildButtonFunction) {
final double screenWidth = MediaQuery.of(context).size.width;
int crossAxisCount;
if (screenWidth > 310 && screenWidth < 440) {
crossAxisCount = 3;
} else {
crossAxisCount =
(screenWidth / 110).floor().clamp(1, double.infinity).toInt();
}
return ReorderableGridView.count(
mainAxisSpacing: 2.0,
crossAxisSpacing: 2.0,
crossAxisCount: crossAxisCount,
children: List.generate(
quickLinks.length,
(index) {
return KeyedSubtree(
key: ValueKey(quickLinks[index].id),
child:
_buildGridChild(model, index, quickLinks, buildButtonFunction),
);
},
),
onReorder: (oldIndex, newIndex) {
setState(() {
model.reorderQuickLinks(oldIndex, newIndex);
});
},
);
}
Widget _buildGridChild(
QuickLinksViewModel model,
int index,
List<QuickLink> quickLinks,
Widget Function(QuickLinksViewModel, int) buildButtonFunction) {
return GestureDetector(
onLongPress: _editMode
? null
: () {
_controller.repeat(reverse: true);
setState(() {
_editMode = true;
});
},
child: AnimatedBuilder(
animation: _animation,
builder: (BuildContext context, Widget? child) {
return Transform.rotate(
angle: _editMode ? _animation.value : 0,
child: child,
);
},
child: Stack(
children: [
WebLinkCard(quickLinks[index]),
if (_editMode &&
quickLinks[index].id !=
1) // Don't show delete button for Security QuickLink
Positioned(
top: 0,
left: 0,
child: buildButtonFunction(model, index),
),
],
),
),
);
}
Container _buildDeleteButton(QuickLinksViewModel model, int index) {
return Container(
width: 32,
height: 32,
decoration: const BoxDecoration(
color: AppTheme.etsDarkGrey,
shape: BoxShape.circle,
),
child: IconButton(
padding: EdgeInsets.zero,
icon: const Icon(Icons.close, color: Colors.white, size: 16),
onPressed: () {
setState(() {
model.deleteQuickLink(index);
});
},
),
);
}
Container _buildAddButton(QuickLinksViewModel model, int index) {
return Container(
width: 32,
height: 32,
decoration: const BoxDecoration(
color: Colors.green,
shape: BoxShape.circle,
),
child: IconButton(
padding: EdgeInsets.zero,
icon: const Icon(Icons.add, color: Colors.white, size: 20),
onPressed: () {
setState(() {
model.restoreQuickLink(index);
});
},
),
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
}