-
Notifications
You must be signed in to change notification settings - Fork 1
/
firebase.dart
198 lines (183 loc) · 6.03 KB
/
firebase.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/classes/loading.dart';
import 'package:flutter_demo/classes/validators.dart';
import 'package:flutter_demo/providers/auth.dart';
import 'package:flutter_demo/services/firestore.dart';
import 'package:flutter_demo/types/firestore_user.dart';
import 'package:flutter_translate/flutter_translate.dart';
import 'package:provider/provider.dart';
class Firebase extends StatefulWidget {
const Firebase({super.key});
@override
State<Firebase> createState() => _FirebaseState();
}
class _FirebaseState extends State<Firebase> {
final _formKey = GlobalKey<FormState>();
final TextEditingController _nameController = TextEditingController();
final TextEditingController _photoController = TextEditingController();
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(16.0),
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Visibility(
visible: Provider.of<Auth>(context).auth.currentUser == null,
child: loggedOutUI(),
),
Visibility(
visible: Provider.of<Auth>(context).auth.currentUser != null,
child: loggedInUI(),
),
Visibility(
visible: Provider.of<Auth>(context).auth.currentUser != null,
child: ElevatedButton(
onPressed: () {
Provider.of<Auth>(context, listen: false).logOut(context);
},
child: Text(translate('firebase.log_out')),
),
),
],
),
),
);
}
Widget loggedOutUI() {
return Column(
children: [
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Text(translate('firebase.need_log_in')),
),
ElevatedButton(
onPressed: () async {
await Provider.of<Auth>(context, listen: false).logIn(context);
},
child: Text(translate('firebase.log_in_google')),
),
],
);
}
Widget loggedInUI() {
return StreamBuilder<DocumentSnapshot>(
stream: FirebaseFirestore.instance
.collection('users')
.doc(Provider.of<Auth>(context).auth.currentUser?.uid)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
}
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
}
// If the document exists and there is no error, build the UI
if (snapshot.hasData && snapshot.data!.exists) {
Map<String, dynamic> data =
snapshot.data!.data() as Map<String, dynamic>;
_nameController.text = data['displayName'];
_photoController.text = data['photoURL'];
return Column(
children: [
Text(
translate('firebase.welcome'),
style: const TextStyle(fontSize: 20),
),
Text(translate('firebase.welcome_desc')),
form(),
],
);
} else {
// If the document does not exist
// NOTE: in our case, this is not possible
// because we just created the user after a new signup,
// but this is good to have anyway
return Text(translate('firebase.user_error'));
}
},
);
}
Widget form() {
return Form(
key: _formKey,
child: Column(
children: [
nameTextField(),
photoTextField(),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Padding(
padding: const EdgeInsets.only(right: 8),
child: ElevatedButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
// example of failing API and how to catch errors
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(translate('firebase.api_error')),
duration: const Duration(seconds: 3),
));
}
},
child: Text(translate('firebase.submit_error')),
),
),
ElevatedButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
submitForm();
}
},
child: Text(translate('firebase.submit')),
),
],
),
),
],
),
);
}
Widget nameTextField() {
return TextFormField(
controller: _nameController,
validator: (value) {
return Validators.checkInput(value!);
},
decoration: InputDecoration(
labelText: translate('firebase.name'),
floatingLabelBehavior: FloatingLabelBehavior.auto,
),
);
}
Widget photoTextField() {
return TextFormField(
controller: _photoController,
validator: (value) {
return Validators.checkUrl(value!);
},
decoration: InputDecoration(
labelText: translate('firebase.photo'),
floatingLabelBehavior: FloatingLabelBehavior.auto,
),
);
}
Future<void> submitForm() async {
final loading = Loading();
loading.load(
context,
translate('loads.updating_user'),
);
final FirestoreUser user = FirestoreUser(
uid: Provider.of<Auth>(context, listen: false).auth.currentUser!.uid,
displayName: _nameController.text,
photoURL: _photoController.text,
);
await Firestore().updateUser(user).then((value) => loading.cancel(context));
}
}