alvinvin00

missing_list.dart

Mar 18th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.16 KB | None | 0 0
  1. import 'dart:async';
  2.  
  3. import 'package:bencana/disaster.dart';
  4. import 'package:bencana/disaster_page.dart';
  5. import 'package:bencana/missing_page.dart';
  6. import 'package:bencana/utilities.dart';
  7. import 'package:firebase_database/firebase_database.dart';
  8. import 'package:flutter/material.dart';
  9.  
  10. class MissingList extends StatefulWidget {
  11.   @override
  12.   _MissingListState createState() => _MissingListState();
  13. }
  14.  
  15. class _MissingListState extends State<MissingList> {
  16.   var title;
  17.   var timeStamp;
  18.   var disaster;
  19.   var disasterId;
  20.  
  21.   List<Disaster> disasterList = new List();
  22.  
  23.   Utilities util = new Utilities();
  24.  
  25.   final disasterRef = FirebaseDatabase.instance.reference().child('disasters');
  26.  
  27.   StreamSubscription<Event> _onDisasterAddedSubscription;
  28.   StreamSubscription<Event> _onDisasterChangedSubscription;
  29.  
  30.   void _onDisasterAdded(Event event) {
  31.     print('On disaster added...');
  32.     disaster = new Disaster.fromSnapshot(event.snapshot);
  33.     title = disaster.title;
  34.     disasterId = disaster.disasterId;
  35.     print('Disaster Length: $disasterList.length');
  36.     setState(() {
  37.       disasterList.add(disaster);
  38.     });
  39.   }
  40.  
  41.   void _onDisasterChanged(Event event) {
  42.     print('On disaster changed...');
  43.     disaster = new Disaster.fromSnapshot(event.snapshot);
  44.     setState(() {
  45.       disasterList.clear();
  46.       title = disaster.title;
  47.       disasterId = disaster.disasterId;
  48.       print('Disaster Length: $disasterList.length');
  49.       disasterList.add(disaster);
  50.     });
  51.   }
  52.  
  53.  
  54.   @override
  55.   void initState() {
  56.     super.initState();
  57.     disasterList.clear();
  58.     _onDisasterAddedSubscription =
  59.         disasterRef.onChildAdded.listen(_onDisasterAdded);
  60.     _onDisasterChangedSubscription =
  61.         disasterRef.onChildChanged.listen(_onDisasterChanged);
  62.   }
  63.  
  64.  
  65.   @override
  66.   void dispose() {
  67.     super.dispose();
  68.     _onDisasterAddedSubscription.cancel();
  69.     _onDisasterChangedSubscription.cancel();
  70.   }
  71.  
  72.   @override
  73.   Widget build(BuildContext context) {
  74.  
  75.     return Center(
  76.       child: Container(
  77.         color: Colors.grey[300],
  78.         child: disasterList.length == 0
  79.             ? const Center(child: CircularProgressIndicator())
  80.             : ListView.builder(
  81.           itemBuilder: (BuildContext context, int index) {
  82.             print('----------disasterId : $disasterId');
  83.             return GestureDetector(
  84.               onTap: (){
  85.                 Navigator.push(context, MaterialPageRoute(builder: (context) => MissingPage(disasterId: disasterList[index].disasterId, disasterName: disasterList[index].title,)));
  86.               },
  87.               child: Card(
  88.                 child: Column(
  89.                   crossAxisAlignment: CrossAxisAlignment.start,
  90.                   children: <Widget>[
  91.                     Padding(
  92.                       padding: const EdgeInsets.only(
  93.                         left: 16.0,
  94.                         right: 16.0,
  95.                         bottom: 10.0,
  96.                         top: 16.0,
  97.                       ),
  98.                       child: Text(
  99.                         disasterList[index].title,
  100.                         style: TextStyle(
  101.                           fontSize: 18.0,
  102.                           fontWeight: FontWeight.bold,
  103.                         ),
  104.                       ),
  105.                     ),
  106.                     Padding(
  107.                       padding: const EdgeInsets.only(
  108.                         left: 16.0,
  109.                         right: 16.0,
  110.                         top: 5.0,
  111.                         bottom: 5.0,
  112.                       ),
  113.                       child: Text(
  114.                         util.convertTimestamp(
  115.                           disasterList[index].timestamp,
  116.                         ),
  117.                         style: TextStyle(
  118.                           fontSize: 14.0,
  119.                           color: Colors.grey,
  120.                         ),
  121.                       ),
  122.                     ),
  123. //                        RaisedButton(onPressed: null),
  124.                   ],
  125.                 ),
  126.               ),
  127.             );
  128.           },
  129.           itemCount: disasterList == null ? 0 : disasterList.length,
  130.         ),
  131.       ),
  132.     );
  133.   }
  134. }
Add Comment
Please, Sign In to add comment