Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'package:bencana/disaster.dart';
- import 'package:bencana/disaster_page.dart';
- import 'package:bencana/missing_page.dart';
- import 'package:bencana/utilities.dart';
- import 'package:firebase_database/firebase_database.dart';
- import 'package:flutter/material.dart';
- class MissingList extends StatefulWidget {
- @override
- _MissingListState createState() => _MissingListState();
- }
- class _MissingListState extends State<MissingList> {
- var title;
- var timeStamp;
- var disaster;
- var disasterId;
- List<Disaster> disasterList = new List();
- Utilities util = new Utilities();
- final disasterRef = FirebaseDatabase.instance.reference().child('disasters');
- StreamSubscription<Event> _onDisasterAddedSubscription;
- StreamSubscription<Event> _onDisasterChangedSubscription;
- void _onDisasterAdded(Event event) {
- print('On disaster added...');
- disaster = new Disaster.fromSnapshot(event.snapshot);
- title = disaster.title;
- disasterId = disaster.disasterId;
- print('Disaster Length: $disasterList.length');
- setState(() {
- disasterList.add(disaster);
- });
- }
- void _onDisasterChanged(Event event) {
- print('On disaster changed...');
- disaster = new Disaster.fromSnapshot(event.snapshot);
- setState(() {
- disasterList.clear();
- title = disaster.title;
- disasterId = disaster.disasterId;
- print('Disaster Length: $disasterList.length');
- disasterList.add(disaster);
- });
- }
- @override
- void initState() {
- super.initState();
- disasterList.clear();
- _onDisasterAddedSubscription =
- disasterRef.onChildAdded.listen(_onDisasterAdded);
- _onDisasterChangedSubscription =
- disasterRef.onChildChanged.listen(_onDisasterChanged);
- }
- @override
- void dispose() {
- super.dispose();
- _onDisasterAddedSubscription.cancel();
- _onDisasterChangedSubscription.cancel();
- }
- @override
- Widget build(BuildContext context) {
- return Center(
- child: Container(
- color: Colors.grey[300],
- child: disasterList.length == 0
- ? const Center(child: CircularProgressIndicator())
- : ListView.builder(
- itemBuilder: (BuildContext context, int index) {
- print('----------disasterId : $disasterId');
- return GestureDetector(
- onTap: (){
- Navigator.push(context, MaterialPageRoute(builder: (context) => MissingPage(disasterId: disasterList[index].disasterId, disasterName: disasterList[index].title,)));
- },
- child: Card(
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- Padding(
- padding: const EdgeInsets.only(
- left: 16.0,
- right: 16.0,
- bottom: 10.0,
- top: 16.0,
- ),
- child: Text(
- disasterList[index].title,
- style: TextStyle(
- fontSize: 18.0,
- fontWeight: FontWeight.bold,
- ),
- ),
- ),
- Padding(
- padding: const EdgeInsets.only(
- left: 16.0,
- right: 16.0,
- top: 5.0,
- bottom: 5.0,
- ),
- child: Text(
- util.convertTimestamp(
- disasterList[index].timestamp,
- ),
- style: TextStyle(
- fontSize: 14.0,
- color: Colors.grey,
- ),
- ),
- ),
- // RaisedButton(onPressed: null),
- ],
- ),
- ),
- );
- },
- itemCount: disasterList == null ? 0 : disasterList.length,
- ),
- ),
- );
- }
- }
Add Comment
Please, Sign In to add comment