Advertisement
rachmadi

disaster.dart

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