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
|
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:beam_messenger/auth.dart';
import 'package:beam_messenger/data/database_helper.dart';
import 'package:beam_messenger/models/user.dart';
import 'package:beam_messenger/screens/login/login_screen_presenter.dart';
class LoginScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new LoginScreenState();
}
}
class LoginScreenState extends State<LoginScreen>
implements LoginScreenContract, AuthStateListener {
BuildContext _ctx;
bool _isLoading = false;
final formKey = new GlobalKey<FormState>();
final scaffoldKey = new GlobalKey<ScaffoldState>();
String _password, _email;
LoginScreenPresenter _presenter;
LoginScreenState() {
_presenter = new LoginScreenPresenter(this);
var authStateProvider = new AuthStateProvider();
authStateProvider.subscribe(this);
}
void _submit() {
final form = formKey.currentState;
if (form.validate()) {
setState(() => _isLoading = true);
form.save();
_presenter.doLogin(_email, _password);
}
}
void _showSnackBar(String text) {
scaffoldKey.currentState
.showSnackBar(new SnackBar(content: new Text(text)));
}
@override
onAuthStateChanged(AuthState state) {
if (state == AuthState.LOGGED_IN)
Navigator.of(_ctx).pushReplacementNamed("/home");
}
@override
Widget build(BuildContext context) {
_ctx = context;
var loginBtn = new RaisedButton(
onPressed: _submit,
child: new Text("LOGIN"),
color: Colors.primaries[0],
);
var loginForm = new Column(
children: <Widget>[
new Text(
"Login",
textScaleFactor: 2.0,
),
new Form(
key: formKey,
child: new Column(
children: <Widget>[
new Padding(
padding: const EdgeInsets.all(8.0),
child: new TextFormField(
onSaved: (val) => _email = val,
validator: (val) {
return val.length < 10
? "email must have atleast 10 chars"
: null;
},
decoration: new InputDecoration(labelText: "email"),
),
),
new Padding(
padding: const EdgeInsets.all(8.0),
child: new TextFormField(
onSaved: (val) => _password = val,
decoration: new InputDecoration(labelText: "Password"),
),
),
],
),
),
_isLoading ? new CircularProgressIndicator() : loginBtn
],
crossAxisAlignment: CrossAxisAlignment.center,
);
return new Scaffold(
appBar: null,
key: scaffoldKey,
body: new Container(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new AssetImage("assets/login_background.jpg"),
fit: BoxFit.cover),
),
child: new Center(
child: new ClipRect(
child: new BackdropFilter(
filter: new ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
child: new Container(
child: loginForm,
height: 300.0,
width: 300.0,
decoration: new BoxDecoration(
color: Colors.grey.shade200.withOpacity(0.5)),
),
),
),
),
),
);
}
@override
void onLoginError(String errorTxt) {
_showSnackBar(errorTxt);
setState(() => _isLoading = false);
}
@override
void onLoginSuccess(User user) async {
_showSnackBar(user.toString());
setState(() => _isLoading = false);
var db = new DatabaseHelper();
await db.saveUser(user);
var authStateProvider = new AuthStateProvider();
authStateProvider.notify(AuthState.LOGGED_IN);
}
}
|