Fakultas Ilmu Komputer UI

detail_post_kegiatan.dart 27.2 KB
Newer Older
1
2
3
4
import 'dart:async';

import 'package:bisaGo/model/kegiatan.dart';
import 'package:bisaGo/model/lokasi.dart';
5
// import 'package:bisaGo/page/updateInformasi/update_informasi.dart';
6
import 'package:bisaGo/utils/validator.dart';
7
import 'package:carousel_slider/carousel_slider.dart';
8
9
10
11
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:intl/intl.dart';
12
import 'package:bisaGo/bloc/komentar_posting_kegiatan_bloc.dart';
13
14
import 'package:bisaGo/component/bisago_appbar.dart';
import 'package:bisaGo/config/styles.dart';
15
import 'package:bisaGo/model/komentar_posting_kegiatan.dart';
16
17
18
19
20
import 'package:bisaGo/network/data/network_model.dart';
import 'package:bisaGo/page/login/login.dart';
import 'package:share/share.dart';
import 'package:shared_preferences/shared_preferences.dart';

21
22
import '../fasilitas.dart';

23
24
25
26
class DetailPostKegiatanPage extends StatefulWidget {
  final Lokasi lokasi;
  final KegiatanModel kegiatan; // ganti model x // sudah
  const DetailPostKegiatanPage(
27
      {@required this.kegiatan, @required this.lokasi, Key key})
28
29
30
31
32
33
      : super(key: key);
  @override
  _DetailPostKegiatanPageState createState() => _DetailPostKegiatanPageState();
}

class _DetailPostKegiatanPageState extends State<DetailPostKegiatanPage> {
34
35
36
37
  Map<String, dynamic> newKomentarPostingKegiatanData = {};
  KomentarPostingKegiatanBloc _bloc;
  List<KomentarPostingKegiatanModel> allKomentarPositngKegiatanFromApi;
  TextEditingController komentarKegiatanController = TextEditingController();
38
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
39

40
41
  @override
  void initState() {
42
    allKomentarPositngKegiatanFromApi = [];
43
44
    _bloc =
        KomentarPostingKegiatanBloc(widget.lokasi.placeId, widget.kegiatan.id);
45
46
47
48
49
50
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
51
52
53
54
      onWillPop: () => Navigator.of(context).pushReplacement(MaterialPageRoute(
          builder: (BuildContext context) => Fasilitas(
                lokasi: widget.lokasi,
              ))),
55
56
      child: Scaffold(
        appBar: BisaGoAppBar(
57
          title: widget.lokasi.name,
jovi_013's avatar
jovi_013 committed
58
          key: Key('appbar-text-${widget.kegiatan.placeId}'),
59
60
          actions: <Widget>[
            InkWell(
61
62
63
64
65
66
67
68
              onTap: () => Share.share(
                  'Review kegiatan disabilitas di ${widget.lokasi.name}, '
                  'oleh: ${widget.kegiatan.creator}\n\"${widget.kegiatan.deskripsi}\"\n\nDapatkan info fasilitas ramah '
                  'disabilitas dari aplikasi bisaGo!\nDownload bisaGo '
                  'sekarang di: https://bit.ly/DownloadbisaGo'),
              child: const Icon(Icons.share),
              // ganti format share
            ),
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
          ],
        ),
        body: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              Container(
                key: const Key('Text Judul Kegiatan'),
                margin: const EdgeInsets.symmetric(
                    vertical: 10.0, horizontal: 15.0),
                alignment: Alignment.centerLeft,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Container(
                      width: MediaQuery.of(context).size.width * 0.6,
                      // ganti alias tambahan
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          SizedBox(height: regularSpace),
                          Text(
90
91
                            widget.kegiatan
                                .namaKegiatan, // ganti nama kegiatan // sudah
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
                            style: const TextStyle(
                              fontSize: 30,
                              fontWeight: FontWeight.w800,
                              letterSpacing: 0.3,
                              fontFamily: 'Muli',
                              // color: Colors.black,
                              // fontFamily: 'Comfortaa',
                            ),
                          ),
                          Text(
                            'oleh ${widget.kegiatan.penyelenggara}', // ganti format ${nama penyelenggara} // sudah
                            style: TextStyle(fontSize: 16),
                            textAlign: TextAlign.left,
                          )
                        ],
                      ),
                    ),
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
                    // PopupMenuButton(
                    //   key: const Key('Button Ubah Informasi'),
                    //   elevation: 4.0,
                    //   offset: const Offset(0.0, 40.0),
                    //   itemBuilder: (BuildContext context) {
                    //     final choices = ['Ubah Informasi'];
                    //     return choices.map((String choice) {
                    //       return PopupMenuItem(
                    //         key: Key(choice),
                    //         child: ElevatedButton(
                    //           style: ButtonStyle(
                    //               padding: MaterialStateProperty.all(
                    //                   EdgeInsets.symmetric(
                    //                       vertical: 0, horizontal: 0)),
                    //               backgroundColor:
                    //                   MaterialStateProperty.all(Colors.white),
                    //               foregroundColor:
                    //                   MaterialStateProperty.all(Colors.black),
                    //               elevation: MaterialStateProperty.all(0)),
                    //           onPressed: _updateInformasi,
                    //           child: SizedBox(
                    //             width: double.infinity,
                    //             child: Text(choice),
                    //           ),
                    //         ),
                    //       );
                    //     }).toList();
                    //   },
                    // ),
138
139
140
141
142
143
144
145
146
                  ],
                ),
              ),
              Container(
                margin: const EdgeInsets.all(doubleSpace),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    SizedBox(
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
                      width: MediaQuery.of(context).size.width,
                      height: 160,
                      child: CarouselSlider(
                        options: CarouselOptions(
                          aspectRatio: 1.0,
                          enlargeCenterPage: true,
                          enableInfiniteScroll: false,
                          initialPage: 0,
                          autoPlay: true,
                        ),
                        items: widget.kegiatan.image
                            .map((item) => Container(
                                  child: Container(
                                    child: ClipRRect(
                                        borderRadius: BorderRadius.all(
                                            Radius.circular(20)),
                                        child: Stack(
                                          children: <Widget>[
                                            Image.network(item,
                                                fit: BoxFit.cover,
                                                width: 1000.0),
                                            Positioned(
                                              bottom: 0.0,
                                              left: 0.0,
                                              right: 0.0,
                                              child: Container(
                                                decoration: BoxDecoration(
                                                  gradient: LinearGradient(
                                                    colors: [
                                                      Color.fromARGB(
                                                          200, 0, 0, 0),
                                                      Color.fromARGB(0, 0, 0, 0)
                                                    ],
                                                    begin:
                                                        Alignment.bottomCenter,
                                                    end: Alignment.topCenter,
                                                  ),
                                                ),
                                                padding: EdgeInsets.symmetric(
                                                    vertical: 10.0,
                                                    horizontal: 20.0),
                                                child: Text(
                                                  '#${widget.kegiatan.image.indexOf(item) + 1}',
                                                  style: TextStyle(
                                                    color: Colors.white,
                                                    fontSize: 20.0,
                                                    fontWeight: FontWeight.bold,
                                                  ),
                                                ),
                                              ),
                                            ),
                                          ],
                                        )),
                                  ),
                                ))
                            .toList(),
                      ),
                    ),
205
206
207
208
209
210
211
212
213
214
215
216
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
                    const SizedBox(
                      height: 10,
                    ),
                    Container(
                      key: const Key('desc'),
                      // decoration: BoxDecoration(
                      //     color: gray,
                      //     boxShadow: regularShadow,
                      //     borderRadius: regularBorderRadius),
                      padding: const EdgeInsets.all(doubleSpace),
                      margin: const EdgeInsets.symmetric(vertical: doubleSpace),
                      // child: Column(
                      //   crossAxisAlignment: CrossAxisAlignment.start,
                      //   children: <Widget>[
                      // Row(
                      //   children: <Widget>[
                      //     const Expanded(
                      //       child: Text(
                      //         'Cara menggunakan',
                      //         style: TextStyle(
                      //             fontSize: 20,
                      //             fontWeight: FontWeight.w800),
                      //       ),
                      //     ),
                      //   ],
                      // ),
                      // Container(
                      //   margin: const EdgeInsets.symmetric(
                      //     vertical: regularSpace
                      //   ),
                      // ganti alias tambahan
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Row(
                            // mainAxisAlignment:
                            // MainAxisAlignment.center,
                            children: [
                              const Icon(
                                Icons.access_time,
                                size: 20,
                                color: Colors.green,
                              ),
                              const SizedBox(width: 7.0),
                              const Text(
                                'Senin, 3 Mei 2021', // ganti format widget.x.tanggalpelaksanaan
                                style: TextStyle(fontSize: 16),
                                key: Key('Text Waktu Pelaksanaan'),
                              ),
                            ],
                          ),
                          SizedBox(height: 40),
                          Text(
258
259
                            widget.kegiatan
                                .deskripsi, // ganti format widget.x.deskripsi kegiatan // sudah
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
                            style: const TextStyle(fontSize: 16),
                            key: const Key('Text Deskripsi Kegiatan'),
                          ),
                          SizedBox(height: 40),
                          Row(
                            // mainAxisAlignment:
                            // MainAxisAlignment.center,
                            children: [
                              const Icon(
                                Icons.call,
                                size: 20,
                                color: Colors.green,
                              ),
                              const SizedBox(width: 7.0),
                              Text(
275
276
                                widget.kegiatan
                                    .narahubung, // ganti format narahubung // sudah
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
                                style: TextStyle(fontSize: 16),
                                key: Key('Text Narahubung'),
                              ),
                            ],
                          ),
                        ],
                      ),
                      // ),
                      // ],
                      // ),
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        const Text(
                          'informasi ditambahkan oleh ',
                          style: TextStyle(
                            fontSize: 12,
                            fontStyle: FontStyle.italic,
                            fontWeight: FontWeight.w200,
                          ),
                        ),
                        Container(
                          padding: EdgeInsets.zero,
                          constraints: BoxConstraints(
                              maxWidth:
                                  MediaQuery.of(context).size.width * 0.3),
                          child: Text(
305
306
307
308
                            widget.kegiatan
                                .creator, // ganti format ${widget.x.creator} // sudah
                            key: Key(
                                'Creator info kegiatan'), // ganti format key creator-${widget.x.creator}
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
                            overflow: TextOverflow.fade,
                            softWrap: false,
                            style: const TextStyle(
                              fontSize: 12,
                              fontStyle: FontStyle.italic,
                            ),
                          ),
                        ),
                        Text(
                          '22 April 2021',
                          // '(${DateFormat('dd MMM yyy').format(widget.kegiatan.dateTime)})', // ganti format (${DateFormat('dd MMM yyy').format(widget.x.dateTime)})
                          key: const Key('timestamp'),
                          style: const TextStyle(
                            fontSize: 12,
                            fontStyle: FontStyle.italic,
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(
                      height: regularSpace,
                    ),
                    const Divider(
                      color: grayPrimary,
                      thickness: 1.0,
                    ),
                    Container(
                      key: const Key('Komentar'),
337
338
                      padding:
                          const EdgeInsets.symmetric(vertical: regularSpace),
339
340
341
                      child: const Text(
                        'Komentar',
                        style: TextStyle(
342
                            fontSize: 20, fontWeight: FontWeight.w800),
343
344
345
                      ),
                    ),
                    StreamBuilder(
346
                        stream: _bloc.komentarPostingKegiatanListStream,
347
348
349
350
351
352
                        builder: (context, snapshot) {
                          if (snapshot.hasData) {
                            switch (snapshot.data.status) {
                              case Status.loading:
                                return const Center(
                                  child: CircularProgressIndicator(
353
354
                                    valueColor: AlwaysStoppedAnimation<Color>(
                                        greenPrimary),
355
356
357
358
                                  ),
                                );
                                break;
                              case Status.completed:
359
360
361
362
                                allKomentarPositngKegiatanFromApi = snapshot
                                    .data
                                    .data
                                    .allKomentarKegiatan; // kalo error, brarti allKegiatan
363
                                if (allKomentarPositngKegiatanFromApi.isEmpty) {
364
365
366
367
                                  return const Center(
                                      child: Text('Tidak ada komentar'));
                                } else {
                                  return Column(
368
369
370
371
372
373
374
375
                                      children:
                                          allKomentarPositngKegiatanFromApi
                                              .map<Widget>((k) =>
                                                  komentarKegiatanPlaceHolder(
                                                      k.creator,
                                                      k.created,
                                                      k.deskripsi))
                                              .toList());
376
377
378
379
380
381
382
383
384
385
                                }
                                break;
                              case Status.error:
                                return Center(
                                  child: Text(snapshot.data.data.toString()),
                                );
                                break;
                            }
                          }
                          return Container();
386
                        }),
387
388
389
390
391
392
393
394
395
396
397
                    const SizedBox(height: regularSpace),
                    Form(
                        key: _formKey,
                        child: Column(
                          children: <Widget>[
                            TextFormField(
                              key: const Key('Text Field Komentar'),
                              keyboardType: TextInputType.multiline,
                              maxLines: null,
                              minLines: 3,
                              validator: FieldValidator.validateInfo,
398
                              controller: komentarKegiatanController,
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
                              style: const TextStyle(
                                fontSize: 18,
                              ),
                              decoration: InputDecoration(
                                hintStyle: const TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 15),
                                hintText: 'Tulis komentar...',
                                contentPadding: const EdgeInsets.all(8.0),
                                enabledBorder: OutlineInputBorder(
                                  borderRadius: BorderRadius.circular(10),
                                  borderSide: BorderSide(
                                    color: Theme.of(context).primaryColor,
                                  ),
                                ),
                                border: OutlineInputBorder(
                                  borderRadius: BorderRadius.circular(10),
                                  borderSide: BorderSide(
                                    color: Theme.of(context).primaryColor,
                                  ),
                                ),
                              ),
                            ),
                            Container(
                                key: const Key('Tambah Komentar'),
                                padding:
                                    const EdgeInsets.only(top: doubleSpace),
                                alignment: Alignment.center,
                                child: ButtonTheme(
                                  minWidth: double.infinity,
                                  height: 40,
                                  child: ElevatedButton(
                                    key: const Key('Button Tambah Komentar'),
                                    style: ButtonStyle(
                                      padding: MaterialStateProperty.all(
                                          EdgeInsets.symmetric(vertical: 13)),
                                      elevation: MaterialStateProperty.all(0.0),
                                      backgroundColor:
                                          MaterialStateProperty.all(
                                              greenPrimary),
                                      shape: MaterialStateProperty.all(
                                          RoundedRectangleBorder(
                                              borderRadius: BorderRadius.all(
                                                  Radius.circular(10)))),
                                    ),
                                    onPressed: () {
                                      _checkLoginStatus();
                                    },
                                    child: Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.center,
                                      children: [
                                        const Icon(
                                          Icons.add,
                                          size: 30,
                                          color: Colors.white,
                                        ),
                                        const SizedBox(width: 5.0),
                                        const Text(
                                          'Tambah Komentar',
                                          style: TextStyle(
                                              fontSize: 20,
                                              color: Colors.white,
                                              fontWeight: FontWeight.bold),
                                        ),
                                      ],
                                    ),
                                  ),
                                )),
                          ],
                        )),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Future<void> _checkLoginStatus() async {
    final sharedPreferences = await SharedPreferences.getInstance();
    if (sharedPreferences.getString('token') == null) {
      await Navigator.of(context).pushAndRemoveUntil(
          MaterialPageRoute(builder: (BuildContext context) => const Login()),
          (Route<dynamic> route) => false);
    } else {
486
      await _validateKomentarKegiatanInput();
487
488
489
    }
  }

490
  Future<void> _validateKomentarKegiatanInput() async {
491
492
493
    final form = _formKey.currentState;
    if (_formKey.currentState.validate()) {
      form.save();
494
      await _submitKomentarKegiatan(komentarKegiatanController.text);
495
496
497
    }
  }

498
  Future<void> _submitKomentarKegiatan(String komentarKegiatan) async {
499
    final _namaLokasi = widget.lokasi.placeId;
500
501
    newKomentarPostingKegiatanData['deskripsi'] = komentarKegiatan;
    newKomentarPostingKegiatanData['namaLokasi'] = _namaLokasi;
502
503
    newKomentarPostingKegiatanData['id'] =
        widget.kegiatan.id; // ganti widget.x.id // sudah
504

505
506
    _bloc = KomentarPostingKegiatanBloc(
        _namaLokasi, widget.kegiatan.id); // ganti widget.x.id // sudah
507
    final response = await _bloc.addKomentarPostingKegiatan(
508
509
510
        newKomentarPostingKegiatanData,
        _namaLokasi,
        widget.kegiatan.id); // ganti widget.x.id // sudah
511
512
513
514
515
    if (response['response'] == 'komentar added') {
      successDialog(context);
      Timer(const Duration(seconds: 2), () {
        Navigator.pop(context);
      });
516
517
      await _bloc.fetchKomentarPostingKegiatanList(
          _namaLokasi, widget.kegiatan.id); // ganti widget.x.id // sudah
518
      komentarKegiatanController.clear();
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
    } else {
      failedDialog(context);
    }
  }

  void successDialog(BuildContext context) {
    const alertDialog = AlertDialog(
      title: Text('Tambah komentar berhasil'),
      content: Icon(FontAwesomeIcons.checkCircle),
    );
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return alertDialog;
        });
  }

  void failedDialog(BuildContext context) {
    const alertDialog = AlertDialog(
      title: Text('Gagal menambahkan komentar'),
      content: Icon(FontAwesomeIcons.exclamationCircle),
    );
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return alertDialog;
        });
  }

548
549
  Widget komentarKegiatanPlaceHolder(
      String name, DateTime created, String description) {
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Row(
          children: <Widget>[
            CircleAvatar(
              backgroundColor: greenPrimary,
              child: Text(
                _creatorInitials(name),
                style: const TextStyle(
                  color: Colors.white,
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(regularSpace),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    name,
                    style: const TextStyle(fontSize: 18),
                  ),
573
                  Text('${DateFormat('dd MMMM yyy hh:mm').format(created)}',
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
                      style: const TextStyle(color: grayPrimary, fontSize: 14))
                ],
              ),
            )
          ],
        ),
        Container(
            margin: const EdgeInsets.only(bottom: regularSpace, left: 13.0),
            child: Text(
              description,
              style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
            )),
        const Divider(
          color: grayPrimary,
          thickness: 1.0,
        )
      ],
    );
  }

  String _creatorInitials(String name) {
    var initials = '';
    for (final i in name.split(' ')) {
      initials += '${i[0].toUpperCase()}';
    }
    return initials;
  }

602
603
604
605
606
607
608
609
610
611
612
  // Future<void> _updateInformasi() async {
  //   final sharedPreferences = await SharedPreferences.getInstance();
  //   if (sharedPreferences.getString('token') == null) {
  //     await Navigator.of(context).pushAndRemoveUntil(
  //         MaterialPageRoute(builder: (BuildContext context) => const Login()),
  //         (Route<dynamic> route) => false);
  //   } else {
  //     await Navigator.of(context).push(MaterialPageRoute(
  //         builder: (BuildContext context) => UpdateInformasi(
  //             komentar: widget.komentar, lokasi: widget.lokasi) // ganti modelnya di update_informasi.dart
  //     ));
613
614
615
616
617
618
619
620
621
  //   }
  // }

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