Fakultas Ilmu Komputer UI

program_page.dart 15.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:home_industry/Component/custom_circular.dart';
import 'package:home_industry/Component/date_time_formatter.dart';
import 'package:home_industry/Component/product_image.dart';
import 'package:home_industry/Component/router.dart';
import 'package:home_industry/Pages/Program/bloc_program/bloc.dart';
import 'package:home_industry/Pages/Program/bloc_program/program_bloc.dart';
import 'package:home_industry/Pages/Program/bloc_search_program/bloc.dart';
import 'package:home_industry/Pages/Program/model/program.dart';
import 'package:home_industry/Pages/Program/program_search_delegate.dart';
import 'package:home_industry/Pages/Program/repositories/program_repository.dart';
import 'package:home_industry/State/Auth/repositories/depedencies_repositories.dart';

17
class Programs extends StatefulWidget {
18
19
  const Programs({Key key}) : super(key: key);

20
21
22
23
24
25
26
27
  @override
  _ProgramsState createState() => _ProgramsState();
}

class _ProgramsState extends State<Programs> {
  Widget programList = const _ListProgram();
  String sort = 'Status';

28
29
30
31
32
33
34
35
36
37
38
39
40
41
  Future<void> onSearchPressed(BuildContext context) async {
    final searchProgram =
        SearchProgramBloc(RepositoryProvider.of<ProgramRepository>(context));
    await showSearch(
        context: context, delegate: ProgramSearchDelegate(searchProgram));
    await searchProgram.close();
  }

  @override
  Widget build(BuildContext context) {
    return RepositoryProvider<ProgramRepository>(
      create: (context) => ProgramRepository(
          dio: RepositoryProvider.of<DependenciesRepositories>(context).dio),
      child: Builder(
42
43
44
          builder: (BuildContext context) {
            return Scaffold(
              appBar: AppBar(
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
45
                title: const Text(
46
47
48
49
50
51
52
53
54
55
56
57
58
                  'Program',
                  style: TextStyle(color: Colors.black),
                ),
                centerTitle: true,
                backgroundColor: Colors.white,
                elevation: 0.5,
                actions: <Widget>[
                  IconButton(
                      icon: const Icon(Icons.search),
                      onPressed: () async {
                        await onSearchPressed(context);
                      })
                ],
59
              ),
60
61
62
63
              body: SingleChildScrollView(
                child: BlocProvider(
                  create: (context) => ProgramBloc(
                      programRepository:
64
                      RepositoryProvider.of<ProgramRepository>(context))
65
66
                    ..add(const FetchProgram()),
                  child: Column(children: <Widget>[
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
67
                    programList,
68
69
70
71
                  ]),
                ),
              ),
              bottomNavigationBar: Container(
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
72
                margin: const EdgeInsets.symmetric(horizontal: 25.0),
73
                child: Wrap(children: [
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
74
75
                  const Padding(
                    padding: EdgeInsets.only(top: 20),
76
                    child: Text('Urutkan Berdasarkan:',
77
                      style: TextStyle(fontSize: 14.0),),
78
79
80
81
                  ),
                  GestureDetector(
                      onTap: () {
                        setState(() {
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
82
                          programList = const _ListProgramSortStatus();
83
84
                        });
                      },
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
85
                      child: const FilterButton(sort: 'Status')
86
87
88
89
                  ),
                  GestureDetector(
                      onTap: () {
                        setState(() {
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
90
                          programList = const _ListProgram();
91
92
                        });
                      },
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
93
                      child: const FilterButton(sort: 'Tanggal')
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
  }
}

class _ListProgram extends StatefulWidget {
  const _ListProgram({Key key}) : super(key: key);
  @override
  __ListProgramState createState() => __ListProgramState();
}

class __ListProgramState extends State<_ListProgram> {
  ScrollController _scrollController;
  static const _scrollThreshold = 300;

  void onScroll() {
    final maxScroll = _scrollController.position.maxScrollExtent;
    final currentScroll = _scrollController.position.pixels;
    if (maxScroll - currentScroll <= _scrollThreshold) {
      BlocProvider.of<ProgramBloc>(context).add(const FetchProgram());
    }
  }

  @override
  void initState() {
    _scrollController = ScrollController()..addListener(onScroll);
    super.initState();
  }

129
130
131
132
133
134
  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

135
136
137
  @override
  Widget build(BuildContext context) {
    return BlocBuilder<ProgramBloc, ProgramState>(
138
139
140
141
      bloc: ProgramBloc(
          programRepository:
          RepositoryProvider.of<ProgramRepository>(context))
        ..add(const FetchProgram()),
142
143
144
145
146
147
148
149
150
151
152
153
154
        builder: (BuildContext context, ProgramState state) {
      if (state is InitialListProgramState) {
        return const Center(
          child: CustomCircularIndicator(),
        );
      } else if (state is ListProgramsError) {
        return Center(
          child: Text(state.error),
        );
      } else if (state is ListProgramsLoaded) {
        if (state.programs.isEmpty) {
          return const Center(
            child: Text(
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
155
              'Maaf program sementara kosong',
156
157
158
159
              style: TextStyle(fontSize: 20),
            ),
          );
        } else {
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
          return Column(
            children: [
              ListView.builder(
                  itemCount: state.hasReachedMax
                      ? state.programs.length
                      : state.programs.length + 1,
                  controller: _scrollController,
                  padding: const EdgeInsets.symmetric(horizontal: 10),
                  physics: const BouncingScrollPhysics(),
                  shrinkWrap: true,
                  itemBuilder: (BuildContext context, int index) {
                    return index >= state.programs.length
                        ?  Container()
                        : ProgramCard(
                            program: state.programs[index],
                          );
                  }),
              Container(height: 10)
            ],
179
180
          );
        }
181
182
183
184
185
186
187
188
189
      } else if (state is ListProgramsSortedLoaded) {
        if (state.programs.isEmpty) {
          return const Center(
            child: Text(
              'Maaf program sementara kosong',
              style: TextStyle(fontSize: 20),
            ),
          );
        } else {
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
          return Column(
            children: [
              ListView.builder(
                  itemCount: state.hasReachedMax
                      ? state.programs.length
                      : state.programs.length + 1,
                  controller: _scrollController,
                  padding: const EdgeInsets.symmetric(horizontal: 10),
                  physics: const BouncingScrollPhysics(),
                  shrinkWrap: true,
                  itemBuilder: (BuildContext context, int index) {
                    return index >= state.programs.length
                        ?  Container()
                        : ProgramCard(
                      program: state.programs[index],
                    );
                  }),
              Container(height: 10)
            ],
209
210
          );
        }
211
212
213
214
215
216
      }
      return null;
    });
  }
}

217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
class _ListProgramSortStatus extends StatefulWidget {
  const _ListProgramSortStatus({Key key}) : super(key: key);
  @override
  __ListProgramSortStatusState createState() => __ListProgramSortStatusState();
}

class __ListProgramSortStatusState extends State<_ListProgramSortStatus> {
  ScrollController _scrollController;
  static const _scrollThreshold = 300;

  void onScroll() {
    final maxScroll = _scrollController.position.maxScrollExtent;
    final currentScroll = _scrollController.position.pixels;
    if (maxScroll - currentScroll <= _scrollThreshold) {
      BlocProvider.of<ProgramBloc>(context).add(const FetchProgramByStatus());
    }
  }

  @override
  void initState() {
    _scrollController = ScrollController()..addListener(onScroll);
    super.initState();
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<ProgramBloc, ProgramState>(
        bloc: ProgramBloc(
            programRepository:
                RepositoryProvider.of<ProgramRepository>(context))
          ..add(const FetchProgramByStatus()),
        builder: (BuildContext context, ProgramState state) {
          if (state is InitialListProgramState || state is ListProgramsLoaded) {
            return const Center(
              child: CustomCircularIndicator(),
            );
          } else if (state is ListProgramsSortedError) {
            return Center(
              child: Text(state.error),
            );
          } else if (state is ListProgramsSortedLoaded) {
            if (state.programs.isEmpty) {
              return const Center(
                child: Text(
                  'Maaf program sementara kosong',
                  style: TextStyle(fontSize: 20),
                ),
              );
            } else {
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
              return Column(
                children: [
                  ListView.builder(
                      itemCount: state.hasReachedMax
                          ? state.programs.length
                          : state.programs.length + 1,
                      controller: _scrollController,
                      padding: const EdgeInsets.symmetric(horizontal: 10),
                      physics: const BouncingScrollPhysics(),
                      shrinkWrap: true,
                      itemBuilder: (BuildContext context, int index) {
                        return index >= state.programs.length
                            ?  Container()
                            : ProgramCard(
                          program: state.programs[index],
                        );
                      }),
                  Container(height: 10)
                ],
291
292
293
294
295
296
297
298
              );
            }
          }
          return Container();
        });
  }
}

299
300
301
302
303
304
305
306
307
class ProgramCard extends StatelessWidget {
  final Program program;
  const ProgramCard({Key key, @required this.program})
      : assert(program != null),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
308
      height: 230,
309
310
311
      child: Card(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(15),
312
          side: BorderSide(color: Theme.of(context).primaryColor, width: 1.5),
313
314
        ),
        child: Column(
315
            crossAxisAlignment: CrossAxisAlignment.center,
316
            children: <Widget>[
317
318
              Expanded(
                flex: 4,
319
320
321
                child: Hero(
                  tag: program.id,
                  child: CachedImage(
322
                    url: program.posterImage,
323
324
325
                  ),
                ),
              ),
326
327
328
329
330
              Flexible(
                  flex: 3,
                  child: _ProgramDescription(
                    program: program,
                  ))
331
332
333
334
335
336
337
338
            ]),
      ),
    );
  }
}

class _ProgramDescription extends StatelessWidget {
  final Program program;
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
339
  const _ProgramDescription({Key key, @required this.program})
340
341
342
343
344
345
      : assert(program != null),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return Padding(
346
        padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 5),
347
348
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
349
          mainAxisAlignment: MainAxisAlignment.spaceAround,
350
351
352
353
354
          children: <Widget>[
            Text(
              program.name,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
355
              style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
356
357
358
359
360
            ),
            Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
361
362
363
364
365
                  Expanded(
                    flex: 2,
                    child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
366
367
368
369
370
371
372
373
                          if (program.startDateTime != null)
                            Text(
                              '${formatDate(program.startDateTime)}: '
                              '${formatTime(program.startDateTime)}',
                              overflow: TextOverflow.ellipsis,
                              style: const TextStyle(
                                fontSize: 16,
                              ),
374
                            ),
375
376
377
378
379
380
381
382
                          if (program.endDateTime != null)
                            Text(
                              '${formatDate(program.endDateTime)}: '
                              '${formatTime(program.endDateTime)}',
                              overflow: TextOverflow.ellipsis,
                              style: const TextStyle(
                                fontSize: 16,
                              ),
383
                            ),
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
384
                            statusText()
385
386
387
388
389
390
                        ]),
                  ),
                  Expanded(
                    child: _DonasiButton(
                      program: program,
                    ),
391
392
393
394
                  )
                ])
          ],
        ));
395
  }
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417

  Widget statusText() {
    if (program.startDateTime != null &&
        CustomizableDateTime.current.compareTo(program.startDateTime) < 1) {
      return const Text(
        'Belum Mulai',
        style: TextStyle(fontWeight: FontWeight.bold),
      );
    }
    else if ( program.endDateTime != null &&
        program.endDateTime.compareTo(CustomizableDateTime.current) < 1) {
      return const Text(
        'Selesai',
        style: TextStyle(fontWeight: FontWeight.bold),
      );
    } else {
      return const Text(
        'Dalam Progress',
        style: TextStyle(fontWeight: FontWeight.bold),
      );
    }
  }
418
419
420
421
422
423
424
425
426
427
428
429
}

class _DonasiButton extends StatelessWidget {
  final Program program;

  const _DonasiButton({
    Key key,
    @required this.program,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
430
431
432
433
434
435
436
437
    return SizedBox(
      width: 100,
      height: 40,
      child: OutlineButton(
        onPressed: () {
          Navigator.of(context).pushNamed(Router.detailProgramPage,
              arguments: {'program': program});
        },
Aditya Pratama's avatar
Aditya Pratama committed
438
        child: const Text('DETAIL'),
439
440
441
        textColor: Theme.of(context).primaryColor,
        borderSide: BorderSide(color: Theme.of(context).primaryColor, width: 2),
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
442
443
444
445
      ),
    );
  }
}
446
447
448

class FilterButton extends StatelessWidget {
  final String sort;
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
449
  const FilterButton({Key key, @required this.sort})
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
450
451
      : assert(sort != null),
        super(key: key);
452
453
454
455

  @override
  Widget build(BuildContext context) {
    return Container(
456
      margin: const EdgeInsets.symmetric(vertical: 12.0, horizontal: 5.0),
457
458
459
460
461
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10),
        color: Theme.of(context).primaryColor,
      ),
      child: Padding(
462
        padding: const EdgeInsets.symmetric(vertical: 7.0, horizontal: 10.0),
463
        child: Text(sort,
464
          style: const TextStyle(color: Colors.white, fontSize: 12),
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
        ),
      ),
    );
  }
}


extension CustomizableDateTime on DateTime {
  static DateTime customTime;
  static DateTime get current {
    if (customTime != null) {
      return customTime;
    }
    else {
      return DateTime.now();
    }
  }
}