-
Notifications
You must be signed in to change notification settings - Fork 318
Expand file tree
/
Copy path+page.markdoc
More file actions
249 lines (216 loc) · 6.77 KB
/
+page.markdoc
File metadata and controls
249 lines (216 loc) · 6.77 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
---
layout: tutorial
title: Add authentication
description: Add authentication to your Flutter application.
step: 4
---
## User context {% #user-context %}
In Flutter, you can use [provider](https://pub.dev/packages/provider) for state management.
Create a new file `lib/providers/user_provider.dart` and add the following code to it.
```dart
import 'package:appwrite/appwrite.dart';
import 'package:appwrite/models.dart' as models;
import 'package:flutter/material.dart';
import 'package:ideas_tracker/appwrite.dart';
class UserProvider extends ChangeNotifier {
models.User? _current;
models.User? get current => _current;
UserProvider() {
init();
}
Future<void> login(String email, String password) async {
try {
await account.createEmailPasswordSession(
email: email,
password: password,
);
_current = await account.get();
notifyListeners();
debugPrint('Welcome back. You are logged in');
} catch (e) {
rethrow;
}
}
Future<void> logout() async {
try {
await account.deleteSession(sessionId: 'current');
_current = null;
notifyListeners();
debugPrint("Logged out");
} catch(e) {
rethrow;
}
}
Future<void> register(String email, String password) async {
try {
await account.create(userId: ID.unique(), email: email, password: password);
await login(email, password);
notifyListeners();
debugPrint("Account created");
} catch (e) {
rethrow;
}
}
Future<void> init() async {
try {
_current = await account.get();
notifyListeners();
} catch (e) {
_current = null;
notifyListeners();
}
}
}
```
Add the `UserProvider` to `main.dart` to make it accessible throughout the App.
```dart
import 'package:flutter/material.dart';
import 'package:ideas_tracker/providers/user_provider.dart';
import 'package:ideas_tracker/screens/login.dart';
import 'package:provider/provider.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => UserProvider()),
],
child: const MyApp(),
)
);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Ideas Tracker",
debugShowCheckedModeBanner: false,
home: Login()
);
}
}
```
Now, you can use `UserProvider` to access the user's data inside any Widget.
## Styling {% #styling %}
To maintain DRY principles, we will move all styling constants to `lib/styles.dart`. Defining these as static class members allows for consistent, reusable widget styling across the entire app.
```dart
import 'package:flutter/material.dart';
class Styles {
static TextStyle heading = TextStyle(fontSize: 24, fontWeight: FontWeight.w600);
static InputDecoration input = InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey),
borderRadius: BorderRadius.circular(22),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.pink, width: 2),
borderRadius: BorderRadius.circular(22),
),
);
static ButtonStyle button = ElevatedButton.styleFrom(
backgroundColor: Colors.pinkAccent,
foregroundColor: Colors.white,
padding: EdgeInsets.symmetric(vertical: 12, horizontal: 24),
);
static ButtonStyle disabledButton = ElevatedButton.styleFrom(
backgroundColor: Colors.grey,
foregroundColor: Colors.white,
padding: EdgeInsets.symmetric(vertical: 12, horizontal: 24),
);
}
```
## Login page {% #login-page %}
Create a new file `lib/screens/login.dart` and add the following code to it.
this page contains a basic form to allow the user to login or register.
Notice how this page utilizes the `UserProvider` to perform login and register actions.
```dart
import 'package:flutter/material.dart';
import 'package:ideas_tracker/providers/user_provider.dart';
import 'package:ideas_tracker/styles.dart';
import 'package:provider/provider.dart';
class Login extends StatefulWidget {
const Login({super.key});
@override
State<Login> createState() => _LoginState();
}
class _LoginState extends State<Login> {
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
@override
void dispose() {
_emailController.dispose();
_passwordController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 40),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Login or Register", style: Styles.heading),
SizedBox(height: 40,),
TextField(
controller: _emailController,
decoration: Styles.input.copyWith(
hintText: "Email"
),
),
SizedBox(height: 25),
TextField (
controller: _passwordController,
obscureText: true,
decoration: Styles.input.copyWith(
hintText: "Password"
),
),
SizedBox(height: 25),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () async {
try {
await context.read<UserProvider>().login(
_emailController.text,
_passwordController.text,
);
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(e.toString())),
);
}
},
style: Styles.button,
child: Text("Login")
),
SizedBox(width: 24),
ElevatedButton(
onPressed: () async {
try {
await context.read<UserProvider>().register(
_emailController.text,
_passwordController.text,
);
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(e.toString())),
);
}
},
style: Styles.button,
child: Text("Register"),
),
],
)
]),
),
)
);
}
}
```