alvinvin00

disaster_thread_add.dart

Mar 18th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 5.29 KB | None | 0 0
  1. import 'dart:async';
  2.  
  3. import 'package:flutter/material.dart';
  4.  
  5. import 'package:firebase_database/firebase_database.dart';
  6. import 'package:cached_network_image/cached_network_image.dart';
  7.  
  8. import 'package:bencana/disaster_thread_add.dart';
  9. import 'package:bencana/disaster.dart';
  10. import 'package:bencana/comment.dart';
  11. import 'package:bencana/utilities.dart';
  12.  
  13. class DisasterThread extends StatefulWidget {
  14.   final Disaster disaster;
  15.  
  16.   DisasterThread(this.disaster);
  17.  
  18.   @override
  19.   _DisasterThreadState createState() => _DisasterThreadState();
  20. }
  21.  
  22. class _DisasterThreadState extends State<DisasterThread> {
  23.   var disasterId;
  24.  
  25.   // 1. Buat referensi buat comment
  26. //  final commentRef = FirebaseDatabase.instance.reference()
  27. //      .child('comments')
  28. //      .child(disasterId);
  29.   // 2. Siapkan variabel yang dibutuhkan
  30.   var fullName;
  31.   var comment;
  32.   var commentText;
  33.   var photo;
  34.   var timestamp;
  35.  
  36.   List<Comment> commentList = new List();
  37.   Utilities util = new Utilities();
  38.  
  39.   // 3. Buat StreamSubscription untuk mendeteksi perubahan database
  40.   //    Ini perlu import dart:async
  41.   StreamSubscription<Event> _onCommentAddedSubscription;
  42.   StreamSubscription<Event> _onCommentChangedSubscription;
  43.  
  44.   // 4. Siapkan event untuk add dan change dan buat file comment.dart
  45.   void _onCommentAdded(Event event) {
  46.     print('On comment added...');
  47.     comment = new Comment.fromSnapshot(event.snapshot);
  48.     commentText = comment.comment;
  49.     print('Comment text: ${commentText}');
  50.     setState(() {
  51.       commentList.add(comment);
  52.     });
  53.   }
  54.  
  55.   void _onCommentChanged(Event event) {
  56.     print('On comment changed...');
  57.     comment = new Comment.fromSnapshot(event.snapshot);
  58.     commentText = comment.comment;
  59.     print('Comment text: ${commentText}');
  60.     setState(() {
  61.       commentList.add(comment);
  62.     });
  63.   }
  64.  
  65.   // 5. Pasangkan subscription pada init state
  66.   @override
  67.   void initState() {
  68.     super.initState();
  69.     commentList.clear();
  70.     disasterId = widget.disaster.disasterId;
  71.     final commentRef = FirebaseDatabase.instance
  72.         .reference()
  73.         .child('comments')
  74.         .child(disasterId);
  75.     _onCommentAddedSubscription =
  76.         commentRef.onChildAdded.listen(_onCommentAdded);
  77.     _onCommentChangedSubscription =
  78.         commentRef.onChildChanged.listen(_onCommentChanged);
  79.   }
  80.  
  81.   @override
  82.   void dispose() {
  83.     super.dispose();
  84.     _onCommentAddedSubscription.cancel();
  85.     _onCommentChangedSubscription.cancel();
  86.   }
  87.  
  88.   Widget _buildContent() {
  89.     if (commentList.length == 0) {
  90.       return const Center(
  91.         child: Text('No comments'),
  92.       );
  93.     } else {
  94.       return Center(child: CircularProgressIndicator());
  95.     }
  96.   }
  97.  
  98.   @override
  99.   Widget build(BuildContext context) {
  100.     return Scaffold(
  101.       appBar: AppBar(
  102.         title: Text('Comment List'),
  103.         actions: <Widget>[
  104.           IconButton(
  105.             icon: Icon(Icons.add),
  106.             onPressed: () {
  107.               Navigator.push(
  108.                 context,
  109.                 MaterialPageRoute(
  110.                   builder: (context) =>
  111.                       DisasterThreadAdd(widget.disaster.disasterId),
  112.                 ),
  113.               );
  114.             },
  115.           ),
  116.         ],
  117.       ),
  118.       body: Container(
  119.         color: Colors.grey[300],
  120.         child: commentList.length == 0
  121.             ? _buildContent()
  122.             : ListView.builder(
  123.                 itemBuilder: (BuildContext context, int index) {
  124.                   return Card(
  125.                     child: Padding(
  126.                       padding: const EdgeInsets.all(8.0),
  127.                       child: Column(
  128.                         crossAxisAlignment: CrossAxisAlignment.start,
  129.                         children: <Widget>[
  130.                           Text(
  131.                             commentList[index].fullName,
  132.                             style: TextStyle(fontWeight: FontWeight.bold),
  133.                           ),
  134.                           Text(
  135.                             util.convertTimestamp(commentList[index].timestamp),
  136.                             style: TextStyle(color: Colors.grey),
  137.                           ),
  138.                           SizedBox(height: 10.0),
  139.                           Text(commentList[index].comment),
  140.                           SizedBox(height: 10.0),
  141.                           Center(
  142.                             child: commentList[index].photo != null
  143.                                 ? CachedNetworkImage(
  144.                                     placeholder: (context, url) =>
  145.                                         CircularProgressIndicator(),
  146.                                     errorWidget: (context, url, error) =>
  147.                                         Icon(Icons.error),
  148.                                     imageUrl: commentList[index].photo,
  149.                                     width: double.infinity,
  150.                                     fit: BoxFit.fitWidth,
  151.                                     height: 240.0,
  152.                                   )
  153.                                 : Container(),
  154.                           ),
  155.                         ],
  156.                       ),
  157.                     ),
  158.                   );
  159.                 },
  160.                 itemCount: commentList == null ? 0 : commentList.length,
  161.               ),
  162.       ),
  163.     );
  164.   }
  165. }
Add Comment
Please, Sign In to add comment