Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. import 'dart:async';
  2.  
  3. import 'package:flutter/material.dart';
  4. import 'package:firebase_auth/firebase_auth.dart';
  5. import 'package:firebase_database/firebase_database.dart';
  6. import 'package:firebase_database/ui/firebase_animated_list.dart';
  7.  
  8. class CarnivalCompassHome extends StatefulWidget {
  9. @override
  10. CarnivalCompassHomeState createState() => new CarnivalCompassHomeState();
  11. }
  12.  
  13. class CarnivalCompassHomeState extends State<CarnivalCompassHome> {
  14. final DatabaseReference _bandRef =
  15. FirebaseDatabase.instance.reference().child('bands');
  16. final DatabaseReference _messagesRef =
  17. FirebaseDatabase.instance.reference().child('messages');
  18. StreamSubscription<Event> _bandSubscription;
  19. StreamSubscription<Event> _messagesSubscription;
  20. bool _anchorToBottom = false;
  21.  
  22. String _kTestKey = 'Hello';
  23. String _kTestValue = 'world!';
  24.  
  25. @override
  26. void initState() {
  27. super.initState();
  28. FirebaseAuth.instance.currentUser().then((FirebaseUser firebaseUser) {
  29. if (firebaseUser == null) {
  30. FirebaseAuth.instance.signInAnonymously();
  31. print('Signing in anonymously');
  32. } else {
  33. print('Already signed in anonymously');
  34. }
  35. });
  36. FirebaseDatabase.instance.setPersistenceEnabled(true);
  37. FirebaseDatabase.instance.setPersistenceCacheSizeBytes(10000000);
  38. _bandRef.keepSynced(true);
  39. _bandSubscription = _bandRef.onValue.listen((Event event) {
  40. //setState(() {
  41. //_counter = event.snapshot.value ?? 0;
  42. //});
  43. });
  44. _messagesSubscription =
  45. _messagesRef.limitToLast(10).onChildAdded.listen((Event event) {
  46. print('Child added: ${event.snapshot.value}');
  47. });
  48. }
  49.  
  50. @override
  51. void dispose() {
  52. super.dispose();
  53. _messagesSubscription.cancel();
  54. _bandSubscription.cancel();
  55. }
  56.  
  57. @override
  58. Widget build(BuildContext context) {
  59. return new Scaffold(
  60. appBar: new AppBar(
  61. title: new Text('Carnival Compass'),
  62. ),
  63. body: new Column(
  64. children: <Widget>[
  65. new Text('i am working'),
  66. new Flexible(
  67. child: new FirebaseAnimatedList(
  68. key: new ValueKey<bool>(_anchorToBottom),
  69. query: _messagesRef,
  70. reverse: _anchorToBottom,
  71. sort: _anchorToBottom
  72. ? (DataSnapshot a, DataSnapshot b) => b.key.compareTo(a.key)
  73. : null,
  74. itemBuilder: (BuildContext context, DataSnapshot snapshot,
  75. Animation<double> animation, int index) {
  76. return new SizeTransition(
  77. sizeFactor: animation,
  78. child: new Text("Here: $index: ${snapshot.value.toString()}"),
  79. );
  80. },
  81. ),
  82. ),
  83. ],
  84. ),
  85. );
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement