Advertisement
alvinvin00

disaster_page.dart

Mar 18th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 6.47 KB | None | 0 0
  1. import 'dart:async';
  2.  
  3. import 'package:flutter/material.dart';
  4. import 'package:firebase_database/firebase_database.dart';
  5. import 'package:cached_network_image/cached_network_image.dart';
  6.  
  7. import 'package:bencana/disaster.dart';
  8. import 'package:bencana/utilities.dart';
  9. import 'package:bencana/disaster_thread.dart';
  10.  
  11. class DisasterPage extends StatefulWidget {
  12.   final postId;
  13.  
  14.   const DisasterPage({Key key, this.postId}) : super(key: key);
  15.  
  16.   @override
  17.   _DisasterPageState createState() => _DisasterPageState(postId);
  18. }
  19.  
  20. final disasterRef = FirebaseDatabase.instance.reference().child('disasters');
  21.  
  22. class _DisasterPageState extends State<DisasterPage> {
  23.   final postId;
  24.  
  25.   var title;
  26.   var content;
  27.   var photo;
  28.   var timestamp;
  29.   var userId;
  30.   var disaster;
  31.   List<Disaster> disasterList = new List();
  32.   Utilities util = new Utilities();
  33.  
  34.   // 3. Buat StreamSubscription untuk mendeteksi perubahan database
  35.   //    Ini perlu import dart:async
  36.   StreamSubscription<Event> _onDisasterAddedSubscription;
  37.   StreamSubscription<Event> _onDisasterChangedSubscription;
  38.  
  39.   _DisasterPageState(this.postId);
  40.  
  41.   void _onDisasterAdded(Event event) {
  42.     print('On disaster added...');
  43.     disaster = new Disaster.fromSnapshot(event.snapshot);
  44.     title = disaster.title;
  45.     content = disaster.content;
  46.     print('Disaster Length: $disasterList.length');
  47.     print("Title: $title, Content: $content");
  48.     setState(() {
  49.       disasterList.add(disaster);
  50.     });
  51.   }
  52.  
  53.   void _onDisasterChanged(Event event) {
  54.     print('On disaster changed...');
  55.     disaster = new Disaster.fromSnapshot(event.snapshot);
  56.     setState(() {
  57.       disasterList.clear();
  58.       title = disaster.title;
  59.       content = disaster.content;
  60.       print('Disaster Length: $disasterList.length');
  61.       print("Title: $title, Content: $content");
  62.       disasterList.add(disaster);
  63.     });
  64.   }
  65.  
  66.   @override
  67.   void initState() {
  68.     super.initState();
  69.     disasterList.clear();
  70.     _onDisasterAddedSubscription =
  71.         disasterRef.onChildAdded.listen(_onDisasterAdded);
  72.     _onDisasterChangedSubscription =
  73.         disasterRef.onChildChanged.listen(_onDisasterChanged);
  74.   }
  75.  
  76.   @override
  77.   void dispose() {
  78.     super.dispose();
  79.     _onDisasterAddedSubscription.cancel();
  80.     _onDisasterChangedSubscription.cancel();
  81.   }
  82.  
  83.   @override
  84.   Widget build(BuildContext context) {
  85.     return Center(
  86.       child: Container(
  87.         color: Colors.grey[300],
  88.         child: disasterList.length == 0
  89.             ? const Center(child: CircularProgressIndicator())
  90.             : ListView.builder(
  91.                 itemBuilder: (BuildContext context, int index) {
  92.                   return Card(
  93.                     child: Column(
  94.                       crossAxisAlignment: CrossAxisAlignment.start,
  95.                       children: <Widget>[
  96.                         Center(
  97.                           child: CachedNetworkImage(
  98.                             placeholder: (context, url) =>
  99.                                 CircularProgressIndicator(),
  100.                             errorWidget: (context, url, error) =>
  101.                                 Icon(Icons.error),
  102.                             imageUrl: disasterList[index].photo,
  103.                             height: 240.0,
  104.                             width: double.infinity,
  105.                             fit: BoxFit.fitWidth,
  106.                           ),
  107.                         ),
  108.                         Padding(
  109.                           padding: const EdgeInsets.only(
  110.                             left: 16.0,
  111.                             right: 16.0,
  112.                             bottom: 10.0,
  113.                             top: 16.0,
  114.                           ),
  115.                           child: Text(
  116.                             disasterList[index].title,
  117.                             style: TextStyle(
  118.                               fontSize: 18.0,
  119.                               fontWeight: FontWeight.bold,
  120.                             ),
  121.                           ),
  122.                         ),
  123.                         Padding(
  124.                           padding: const EdgeInsets.only(
  125.                             left: 16.0,
  126.                             right: 16.0,
  127.                             top: 5.0,
  128.                             bottom: 5.0,
  129.                           ),
  130.                           child: Text(
  131.                             util.convertTimestamp(
  132.                               disasterList[index].timestamp,
  133.                             ),
  134.                             style: TextStyle(
  135.                               fontSize: 14.0,
  136.                               color: Colors.grey,
  137.                             ),
  138.                           ),
  139.                         ),
  140.                         Padding(
  141.                           padding: const EdgeInsets.only(
  142.                             left: 16.0,
  143.                             right: 16.0,
  144.                             bottom: 10.0,
  145.                           ),
  146.                           child: Text(
  147.                             disasterList[index].content,
  148.                             style: TextStyle(
  149.                               fontSize: 14.0,
  150.                               color: Colors.black,
  151.                             ),
  152.                           ),
  153.                         ),
  154. //                        RaisedButton(onPressed: null),
  155.                         Container(
  156.                           padding: EdgeInsets.only(
  157.                             top: 5.0,
  158.                             left: 16.0,
  159.                             bottom: 10.0,
  160.                           ),
  161.                           child: RaisedButton(
  162.                             color: Colors.teal,
  163.                             textColor: Colors.white,
  164.                             onPressed: () {
  165.                               Navigator.push(
  166.                                 context,
  167.                                 MaterialPageRoute(
  168.                                     builder: (context) =>
  169.                                         DisasterThread(disasterList[index])),
  170.                               );
  171.                             },
  172.                             child: Text(
  173.                               'Comments',
  174.                             ),
  175.                           ),
  176.                         ),
  177.                       ],
  178.                     ),
  179.                   );
  180.                 },
  181.                 itemCount: disasterList == null ? 0 : disasterList.length,
  182.               ),
  183.       ),
  184.     );
  185.   }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement