1
- import 'dart:convert' ;
2
-
3
1
import 'package:flare_flutter/flare_actor.dart' ;
4
2
import 'package:flutter/cupertino.dart' ;
5
3
import 'package:flutter/material.dart' ;
4
+ import 'package:flutter_spinkit/flutter_spinkit.dart' ;
6
5
import 'package:get/get.dart' ;
6
+ import 'package:provider/provider.dart' ;
7
7
import 'package:search_page/search_page.dart' ;
8
- import 'package:shared_preferences/shared_preferences.dart' ;
9
8
import 'package:spo_balaesang/models/employee.dart' ;
10
9
import 'package:spo_balaesang/models/user.dart' ;
10
+ import 'package:spo_balaesang/repositories/data_repository.dart' ;
11
11
import 'package:spo_balaesang/screen/report_screen.dart' ;
12
12
import 'package:spo_balaesang/utils/app_const.dart' ;
13
13
@@ -19,25 +19,30 @@ class EmployeeAttendanceScreen extends StatefulWidget {
19
19
20
20
class _EmployeeAttendanceScreenState extends State <EmployeeAttendanceScreen > {
21
21
List <Employee > _employees;
22
+ bool _isLoading = false ;
22
23
23
24
@override
24
25
void setState (void Function () fn) {
25
26
if (mounted) super .setState (fn);
26
27
}
27
28
29
+ void _setLoading (bool isLoading) {
30
+ setState (() {
31
+ _isLoading = isLoading;
32
+ });
33
+ }
34
+
28
35
Future <void > loadData () async {
29
- final sp = await SharedPreferences .getInstance ();
30
- do {
31
- final _dataEmployees = sp.get (prefsEmployeeKey);
32
- final List <dynamic > _jsonEmployees =
33
- jsonDecode (_dataEmployees.toString ()) as List <dynamic >;
36
+ try {
37
+ _setLoading (true );
38
+ final dataRepo = Provider .of <DataRepository >(context, listen: false );
39
+ final List <Employee > _jsonEmployees = await dataRepo.getAllEmployee ();
34
40
setState (() {
35
- _employees = _jsonEmployees
36
- .map ((employee) =>
37
- Employee .fromJson (employee as Map <String , dynamic >))
38
- .toList ();
41
+ _employees = _jsonEmployees;
39
42
});
40
- } while (_employees.isEmpty);
43
+ } catch (e) {} finally {
44
+ _setLoading (false );
45
+ }
41
46
}
42
47
43
48
@override
@@ -177,6 +182,83 @@ class _EmployeeAttendanceScreenState extends State<EmployeeAttendanceScreen> {
177
182
);
178
183
}
179
184
185
+ Widget _buildContent () {
186
+ if (_isLoading) {
187
+ return const SizedBox (
188
+ child: Center (
189
+ child: SpinKitCircle (
190
+ size: 45 ,
191
+ color: Colors .blueAccent,
192
+ )),
193
+ );
194
+ }
195
+
196
+ if (_employees == null || _employees.isEmpty) {
197
+ return SizedBox (
198
+ child: Center (
199
+ child: Column (
200
+ mainAxisAlignment: MainAxisAlignment .center,
201
+ children: < Widget > [
202
+ SizedBox (
203
+ width: Get .width * 0.5 ,
204
+ height: Get .height * 0.3 ,
205
+ child: const FlareActor (
206
+ 'assets/flare/failure.flr' ,
207
+ animation: 'failure' ,
208
+ ),
209
+ ),
210
+ const Text ('Gagal memuat data pegawai!' ),
211
+ sizedBoxH20,
212
+ ElevatedButton (
213
+ style: ElevatedButton .styleFrom (
214
+ shape: RoundedRectangleBorder (
215
+ borderRadius: BorderRadius .circular (6 )),
216
+ primary: Colors .blueAccent,
217
+ onPrimary: Colors .white),
218
+ onPressed: loadData,
219
+ child: const Text ('Coba Lagi' ),
220
+ )
221
+ ]),
222
+ ),
223
+ );
224
+ }
225
+
226
+ return ListView .builder (
227
+ itemBuilder: (context, index) => _buildEmployeeCard (_employees[index]),
228
+ itemCount: _employees? .length ?? 0 ,
229
+ );
230
+ }
231
+
232
+ Future <Employee > _onSearchButtonPressed () async {
233
+ if (_employees == null || _employees.isEmpty) {
234
+ return null ;
235
+ }
236
+
237
+ return showSearch (
238
+ context: context,
239
+ delegate: SearchPage (
240
+ searchLabel: 'Cari Pegawai' ,
241
+ barTheme: ThemeData (
242
+ appBarTheme: const AppBarTheme (
243
+ brightness: Brightness .dark,
244
+ color: Colors .blueAccent,
245
+ ),
246
+ ),
247
+ showItemsOnEmpty: true ,
248
+ failure: _buildEmptyWidget (),
249
+ builder: (Employee employee) => Padding (
250
+ padding: const EdgeInsets .symmetric (horizontal: 8.0 ),
251
+ child: _buildEmployeeCard (employee),
252
+ ),
253
+ filter: (Employee employee) => [
254
+ employee.name,
255
+ employee.status,
256
+ employee.nip,
257
+ employee.department
258
+ ],
259
+ items: _employees));
260
+ }
261
+
180
262
@override
181
263
Widget build (BuildContext context) {
182
264
return Scaffold (
@@ -185,40 +267,13 @@ class _EmployeeAttendanceScreenState extends State<EmployeeAttendanceScreen> {
185
267
title: const Text ('Presensi Pegawai' ),
186
268
actions: < Widget > [
187
269
IconButton (
188
- onPressed: () => showSearch (
189
- context: context,
190
- delegate: SearchPage (
191
- searchLabel: 'Cari Pegawai' ,
192
- barTheme: ThemeData (
193
- appBarTheme: const AppBarTheme (
194
- brightness: Brightness .dark,
195
- color: Colors .blueAccent,
196
- ),
197
- ),
198
- showItemsOnEmpty: true ,
199
- failure: _buildEmptyWidget (),
200
- builder: (Employee employee) => Padding (
201
- padding:
202
- const EdgeInsets .symmetric (horizontal: 8.0 ),
203
- child: _buildEmployeeCard (employee),
204
- ),
205
- filter: (Employee employee) => [
206
- employee.name,
207
- employee.status,
208
- employee.nip,
209
- employee.department
210
- ],
211
- items: _employees)),
270
+ onPressed: _onSearchButtonPressed,
212
271
icon: const Icon (Icons .search_rounded))
213
272
],
214
273
),
215
274
body: Padding (
216
275
padding: const EdgeInsets .only (bottom: 8 , left: 8 , right: 8 ),
217
- child: ListView .builder (
218
- itemBuilder: (context, index) =>
219
- _buildEmployeeCard (_employees[index]),
220
- itemCount: _employees? .length ?? 0 ,
221
- ),
276
+ child: _buildContent (),
222
277
),
223
278
);
224
279
}
0 commit comments