Advertisement
rachmadi

Home.dart

Mar 11th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 5.12 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/post.dart';
  8. import 'utilities.dart';
  9.  
  10. class HomePage extends StatefulWidget {
  11.   @override
  12.   _HomePageState createState() => _HomePageState();
  13. }
  14.  
  15. // 1. Buat reference untuk post
  16. final postRef = FirebaseDatabase.instance.reference().child('posts');
  17.  
  18. class _HomePageState extends State<HomePage> {
  19.   // 2. Deklarasikan variabel yang dibutuhkan
  20.   var title;
  21.   var content;
  22.   var photo;
  23.   var timestamp;
  24.   var userId;
  25.   var post;
  26.   List<Post> postList = new List();
  27.  
  28.   Utilities util = new Utilities();
  29.  
  30.   // 3. Buat StreamSubscription untuk mendeteksi perubahan database
  31.   //    Ini perlu import dart:async
  32.   StreamSubscription<Event> _onPostAddedSubscription;
  33.   StreamSubscription<Event> _onPostChangedSubscription;
  34.  
  35.   // 4. Siapkan event untuk add dan change dan buat file post.dart
  36.   void _onPostAdded(Event event) {
  37.     print('On post added...');
  38.     post = new Post.fromSnapshot(event.snapshot);
  39.     setState(() {
  40.       postList.add(post);
  41.     });
  42.   }
  43.  
  44.   void _onPostChanged(Event event) {
  45.     print('On post changed...');
  46.     post = new Post.fromSnapshot(event.snapshot);
  47.     setState(() {
  48.       postList.add(post);
  49.     });
  50.   }
  51.  
  52.   // 5. Pasangkan subscription pada init state
  53.   @override
  54.   void initState() {
  55.     super.initState();
  56.     postList.clear();
  57.     _onPostAddedSubscription = postRef.onChildAdded.listen(_onPostAdded);
  58.     _onPostChangedSubscription = postRef.onChildChanged.listen(_onPostChanged);
  59.   }
  60.  
  61.   @override
  62.   void dispose() {
  63.     super.dispose();
  64.     _onPostAddedSubscription.cancel();
  65.     _onPostChangedSubscription.cancel();
  66.   }
  67.  
  68.   @override
  69.   Widget build(BuildContext context) {
  70.     print('Post list: $postList');
  71.     return Container(
  72.       child: Container(
  73.         color: Colors.grey[300],
  74.         child: postList.length == 0
  75.             ? const Center(
  76.                 child: const CircularProgressIndicator(),
  77.               )
  78.             : ListView.builder(
  79.                 itemBuilder: (BuildContext context, int index) {
  80.                   return Card(
  81.                     child: Column(
  82.                       crossAxisAlignment: CrossAxisAlignment.start,
  83.                       children: <Widget>[
  84.                         Padding(
  85.                           padding: const EdgeInsets.only(
  86.                             left: 16.0,
  87.                             right: 16.0,
  88.                             top: 10.0,
  89.                             bottom: 10.0,
  90.                           ),
  91.                           child: Row(
  92.                             children: <Widget>[
  93.                               Text(
  94.                                 postList[index].title,
  95.                                 style: TextStyle(
  96.                                   fontWeight: FontWeight.bold,
  97.                                   fontSize: 18.0,
  98.                                 ),
  99.                               ),
  100.                               Expanded(
  101.                                 child: SizedBox(),
  102.                               ),
  103.                               Text(
  104.                                 util.convertTimestamp(
  105.                                   postList[index].timestamp,
  106.                                 ),
  107.                                 style: TextStyle(
  108.                                   fontSize: 14.0,
  109.                                   color: Colors.grey,
  110.                                 ),
  111.                               ),
  112.                             ],
  113.                           ),
  114.                         ),
  115.                         Padding(
  116.                           padding: const EdgeInsets.only(
  117.                             left: 16.0,
  118.                             right: 16.0,
  119.                             bottom: 10.0,
  120.                           ),
  121.                           child: Text(
  122.                             postList[index].content,
  123.                             style: TextStyle(
  124.                               fontSize: 14.0,
  125.                               color: Colors.grey,
  126.                             ),
  127.                           ),
  128.                         ),
  129.                         Center(
  130.                           child: CachedNetworkImage(
  131.                             placeholder: (context, url) =>
  132.                                 CircularProgressIndicator(),
  133.                             errorWidget: (context, url, error) =>
  134.                                 Icon(Icons.error),
  135.                             imageUrl: postList[index].photo,
  136.                             height: 240.0,
  137.                             width: double.infinity,
  138.                             fit: BoxFit.fitWidth,
  139.                           ),
  140.                         ),
  141.                         SizedBox(
  142.                           height: 20,
  143.                         ),
  144.                       ],
  145.                     ),
  146.                   );
  147.                 },
  148.                 itemCount: postList == null ? 0 : postList.length,
  149.               ),
  150.       ),
  151.     );
  152.   }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement