-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.dart
145 lines (135 loc) · 4.43 KB
/
list.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_database/ui/firebase_animated_list.dart';
//import 'dart:developer';
import 'package:grablunch/auth.dart'
show ensureLoggedIn, googleSignIn, analytics;
import 'package:grablunch/filters.dart' show filterToday;
import 'package:grablunch/localization.dart' show AppLocalizations;
class ListScreen extends StatefulWidget {
@override
State createState() => new ListScreenState();
}
class ListScreenState extends State<ListScreen> {
final reference = FirebaseDatabase.instance.reference().child('lunchers');
String _luncherKey;
final Icon joinIcon = new Icon(Icons.local_dining);
final Icon cancelIcon = new Icon(Icons.close);
ListScreenState() {
reference.keepSynced(true);
_checkIfLuncher();
reference.onChildChanged.listen((Event event) => _checkIfLuncher());
}
Future<Null> _checkIfLuncher() async {
await ensureLoggedIn();
DataSnapshot snapshot = await filterToday(reference).once();
String _newKey;
snapshot.value?.forEach((key, value) {
if (value['name'] == googleSignIn.currentUser.displayName) {
_newKey = key;
}
});
if (_luncherKey != _newKey) {
setState(() {
_luncherKey = _newKey;
});
}
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(AppLocalizations.of(context).titleList),
elevation: Theme.of(context).platform == TargetPlatform.iOS ? 0.0 : 4.0,
actions: <Widget>[
new IconButton(
icon: new Icon(Icons.chat),
tooltip: AppLocalizations.of(context).chat,
onPressed: () => Navigator.of(context).pushNamed('/chat'),
),
],
),
body: new Container(
child: new Column(
children: <Widget>[
// List of Lunchers
new Flexible(
child: new FirebaseAnimatedList(
query: filterToday(reference),
sort: (a, b) => b.key.compareTo(a.key),
padding: new EdgeInsets.all(8.0),
itemBuilder:
(_, DataSnapshot snapshot, Animation<double> animation) {
return new ListItem(
snapshot: snapshot,
animation: animation,
);
},
),
),
],
),
decoration: Theme.of(context).platform == TargetPlatform.iOS
? new BoxDecoration(
border:
new Border(top: new BorderSide(color: Colors.grey[200])))
: null,
),
// Grab lunch button
floatingActionButton: new FloatingActionButton(
child: (_luncherKey != null) ? cancelIcon : joinIcon,
tooltip: (_luncherKey != null) ? 'Cancel' : 'Join',
onPressed: () => _handleSubmitted(),
),
);
}
Future<Null> _handleSubmitted() async {
await ensureLoggedIn();
(_luncherKey != null) ? _cancelLunch() : _joinLunch();
}
void _joinLunch() {
reference.push().set({
'name': googleSignIn.currentUser.displayName,
'photoUrl': googleSignIn.currentUser.photoUrl,
'date': new DateTime.now().millisecondsSinceEpoch,
});
_checkIfLuncher();
analytics.logEvent(name: 'join_lunch');
}
void _cancelLunch() {
reference.child(_luncherKey).remove();
_checkIfLuncher();
analytics.logEvent(name: 'cancel_lunch');
}
}
class ListItem extends StatelessWidget {
ListItem({this.snapshot, this.animation});
final DataSnapshot snapshot;
final Animation animation;
//@override
Widget build(BuildContext context) {
return new SizeTransition(
sizeFactor: new CurvedAnimation(parent: animation, curve: Curves.easeOut),
axisAlignment: 0.0,
child: new Container(
margin: const EdgeInsets.symmetric(vertical: 10.0),
child: new Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Container(
margin: const EdgeInsets.only(right: 16.0),
child: new CircleAvatar(
backgroundImage: new NetworkImage(snapshot.value['photoUrl']),
)),
new Column(
children: <Widget>[
new Text(snapshot.value['name']),
],
),
],
),
),
);
}
}