Skip to content

Commit 8671590

Browse files
committed
Login Register y Tokens
1 parent 23b6929 commit 8671590

File tree

10 files changed

+429
-16
lines changed

10 files changed

+429
-16
lines changed

ios/Podfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@ PODS:
22
- Flutter (1.0.0)
33
- image_picker (0.0.1):
44
- Flutter
5+
- shared_preferences (0.0.1):
6+
- Flutter
57

68
DEPENDENCIES:
79
- Flutter (from `.symlinks/flutter/ios`)
810
- image_picker (from `.symlinks/plugins/image_picker/ios`)
11+
- shared_preferences (from `.symlinks/plugins/shared_preferences/ios`)
912

1013
EXTERNAL SOURCES:
1114
Flutter:
1215
:path: ".symlinks/flutter/ios"
1316
image_picker:
1417
:path: ".symlinks/plugins/image_picker/ios"
18+
shared_preferences:
19+
:path: ".symlinks/plugins/shared_preferences/ios"
1520

1621
SPEC CHECKSUMS:
1722
Flutter: 58dd7d1b27887414a370fcccb9e645c08ffd7a6a
1823
image_picker: 16e5fec1fbc87fd3b297c53e4048521eaf17cd06
24+
shared_preferences: 1feebfa37bb57264736e16865e7ffae7fc99b523
1925

2026
PODFILE CHECKSUM: aff02bfeed411c636180d6812254b2daeea14d09
2127

lib/main.dart

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,33 @@ import 'package:formvalidation/src/bloc/provider.dart';
55
import 'package:formvalidation/src/pages/home_page.dart';
66
import 'package:formvalidation/src/pages/login_page.dart';
77
import 'package:formvalidation/src/pages/producto_page.dart';
8+
import 'package:formvalidation/src/pages/registro_page.dart';
9+
import 'package:formvalidation/src/preferencias_usuario/preferencias_usuario.dart';
810

9-
void main() => runApp(MyApp());
11+
void main() async {
12+
13+
final prefs = new PreferenciasUsuario();
14+
await prefs.initPrefs();
15+
16+
runApp(MyApp());
17+
18+
}
1019

1120
class MyApp extends StatelessWidget {
1221
@override
1322
Widget build(BuildContext context) {
1423

24+
final prefs = new PreferenciasUsuario();
25+
print( prefs.token );
26+
1527
return Provider(
1628
child: MaterialApp(
1729
debugShowCheckedModeBanner: false,
1830
title: 'Material App',
19-
initialRoute: 'home',
31+
initialRoute: 'login',
2032
routes: {
2133
'login' : ( BuildContext context ) => LoginPage(),
34+
'registro' : ( BuildContext context ) => RegistroPage(),
2235
'home' : ( BuildContext context ) => HomePage(),
2336
'producto' : ( BuildContext context ) => ProductoPage(),
2437
},

lib/src/pages/login_page.dart

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import 'package:flutter/material.dart';
22
import 'package:formvalidation/src/bloc/provider.dart';
3+
import 'package:formvalidation/src/providers/usuario_provider.dart';
4+
5+
import 'package:formvalidation/src/utils/utils.dart';
36

47
class LoginPage extends StatelessWidget {
8+
9+
final usuarioProvider = new UsuarioProvider();
10+
11+
512
@override
613
Widget build(BuildContext context) {
714
return Scaffold(
@@ -58,7 +65,10 @@ class LoginPage extends StatelessWidget {
5865
),
5966
),
6067

61-
Text('¿Olvido la contraseña?'),
68+
FlatButton(
69+
child: Text('Crear una nueva cuenta'),
70+
onPressed: ()=> Navigator.pushReplacementNamed(context, 'registro'),
71+
),
6272
SizedBox( height: 100.0 )
6373
],
6474
),
@@ -152,15 +162,16 @@ class LoginPage extends StatelessWidget {
152162
);
153163
}
154164

155-
_login(LoginBloc bloc, BuildContext context) {
156-
157-
print('================');
158-
print('Email: ${ bloc.email }');
159-
print('Password: ${ bloc.password }');
160-
print('================');
165+
_login(LoginBloc bloc, BuildContext context) async {
161166

162-
Navigator.pushReplacementNamed(context, 'home');
167+
Map info = await usuarioProvider.login(bloc.email, bloc.password);
163168

169+
if ( info['ok'] ) {
170+
Navigator.pushReplacementNamed(context, 'home');
171+
} else {
172+
mostrarAlerta( context, info['mensaje'] );
173+
}
174+
164175
}
165176

166177

lib/src/pages/registro_page.dart

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:formvalidation/src/bloc/provider.dart';
3+
import 'package:formvalidation/src/providers/usuario_provider.dart';
4+
import 'package:formvalidation/src/utils/utils.dart';
5+
6+
class RegistroPage extends StatelessWidget {
7+
8+
final usuarioProvider = new UsuarioProvider();
9+
10+
@override
11+
Widget build(BuildContext context) {
12+
return Scaffold(
13+
body: Stack(
14+
children: <Widget>[
15+
_crearFondo( context ),
16+
_loginForm( context ),
17+
],
18+
)
19+
);
20+
}
21+
22+
Widget _loginForm(BuildContext context) {
23+
24+
final bloc = Provider.of(context);
25+
final size = MediaQuery.of(context).size;
26+
27+
return SingleChildScrollView(
28+
child: Column(
29+
children: <Widget>[
30+
31+
SafeArea(
32+
child: Container(
33+
height: 180.0,
34+
),
35+
),
36+
37+
Container(
38+
width: size.width * 0.85,
39+
margin: EdgeInsets.symmetric(vertical: 30.0),
40+
padding: EdgeInsets.symmetric( vertical: 50.0 ),
41+
decoration: BoxDecoration(
42+
color: Colors.white,
43+
borderRadius: BorderRadius.circular(5.0),
44+
boxShadow: <BoxShadow>[
45+
BoxShadow(
46+
color: Colors.black26,
47+
blurRadius: 3.0,
48+
offset: Offset(0.0, 5.0),
49+
spreadRadius: 3.0
50+
)
51+
]
52+
),
53+
child: Column(
54+
children: <Widget>[
55+
Text('Crear cuenta', style: TextStyle(fontSize: 20.0)),
56+
SizedBox( height: 60.0 ),
57+
_crearEmail( bloc ),
58+
SizedBox( height: 30.0 ),
59+
_crearPassword( bloc ),
60+
SizedBox( height: 30.0 ),
61+
_crearBoton( bloc )
62+
],
63+
),
64+
),
65+
66+
FlatButton(
67+
child: Text('¿Ya tienes cuenta? Login'),
68+
onPressed: ()=> Navigator.pushReplacementNamed(context, 'login'),
69+
),
70+
SizedBox( height: 100.0 )
71+
],
72+
),
73+
);
74+
75+
76+
}
77+
78+
Widget _crearEmail(LoginBloc bloc) {
79+
80+
return StreamBuilder(
81+
stream: bloc.emailStream,
82+
builder: (BuildContext context, AsyncSnapshot snapshot){
83+
84+
return Container(
85+
padding: EdgeInsets.symmetric(horizontal: 20.0),
86+
87+
child: TextField(
88+
keyboardType: TextInputType.emailAddress,
89+
decoration: InputDecoration(
90+
icon: Icon( Icons.alternate_email, color: Colors.deepPurple ),
91+
hintText: '[email protected]',
92+
labelText: 'Correo electrónico',
93+
counterText: snapshot.data,
94+
errorText: snapshot.error
95+
),
96+
onChanged: bloc.changeEmail,
97+
),
98+
99+
);
100+
101+
102+
},
103+
);
104+
105+
106+
}
107+
108+
Widget _crearPassword(LoginBloc bloc) {
109+
110+
return StreamBuilder(
111+
stream: bloc.passwordStream,
112+
builder: (BuildContext context, AsyncSnapshot snapshot){
113+
114+
return Container(
115+
padding: EdgeInsets.symmetric(horizontal: 20.0),
116+
117+
child: TextField(
118+
obscureText: true,
119+
decoration: InputDecoration(
120+
icon: Icon( Icons.lock_outline, color: Colors.deepPurple ),
121+
labelText: 'Contraseña',
122+
counterText: snapshot.data,
123+
errorText: snapshot.error
124+
),
125+
onChanged: bloc.changePassword,
126+
),
127+
128+
);
129+
130+
},
131+
);
132+
133+
134+
}
135+
136+
Widget _crearBoton( LoginBloc bloc) {
137+
138+
// formValidStream
139+
// snapshot.hasData
140+
// true ? algo si true : algo si false
141+
142+
return StreamBuilder(
143+
stream: bloc.formValidStream,
144+
builder: (BuildContext context, AsyncSnapshot snapshot){
145+
146+
return RaisedButton(
147+
child: Container(
148+
padding: EdgeInsets.symmetric( horizontal: 80.0, vertical: 15.0),
149+
child: Text('Ingresar'),
150+
),
151+
shape: RoundedRectangleBorder(
152+
borderRadius: BorderRadius.circular(5.0)
153+
),
154+
elevation: 0.0,
155+
color: Colors.deepPurple,
156+
textColor: Colors.white,
157+
onPressed: snapshot.hasData ? ()=> _register(bloc, context) : null
158+
);
159+
},
160+
);
161+
}
162+
163+
_register(LoginBloc bloc, BuildContext context) async {
164+
165+
final info = await usuarioProvider.nuevoUsuario(bloc.email, bloc.password);
166+
167+
if ( info['ok'] ) {
168+
Navigator.pushReplacementNamed(context, 'home');
169+
} else {
170+
mostrarAlerta( context, info['mensaje'] );
171+
}
172+
173+
174+
// Navigator.pushReplacementNamed(context, 'home');
175+
176+
}
177+
178+
179+
Widget _crearFondo(BuildContext context) {
180+
181+
final size = MediaQuery.of(context).size;
182+
183+
final fondoModaro = Container(
184+
height: size.height * 0.4,
185+
width: double.infinity,
186+
decoration: BoxDecoration(
187+
gradient: LinearGradient(
188+
colors: <Color> [
189+
Color.fromRGBO(63, 63, 156, 1.0),
190+
Color.fromRGBO(90, 70, 178, 1.0)
191+
]
192+
)
193+
),
194+
);
195+
196+
final circulo = Container(
197+
width: 100.0,
198+
height: 100.0,
199+
decoration: BoxDecoration(
200+
borderRadius: BorderRadius.circular(100.0),
201+
color: Color.fromRGBO(255, 255, 255, 0.05)
202+
),
203+
);
204+
205+
206+
return Stack(
207+
children: <Widget>[
208+
fondoModaro,
209+
Positioned( top: 90.0, left: 30.0, child: circulo ),
210+
Positioned( top: -40.0, right: -30.0, child: circulo ),
211+
Positioned( bottom: -50.0, right: -10.0, child: circulo ),
212+
Positioned( bottom: 120.0, right: 20.0, child: circulo ),
213+
Positioned( bottom: -50.0, left: -20.0, child: circulo ),
214+
215+
Container(
216+
padding: EdgeInsets.only(top: 80.0),
217+
child: Column(
218+
children: <Widget>[
219+
Icon( Icons.person_pin_circle, color: Colors.white, size: 100.0 ),
220+
SizedBox( height: 10.0, width: double.infinity ),
221+
Text('Fernando Herrera', style: TextStyle( color: Colors.white, fontSize: 25.0 ))
222+
],
223+
),
224+
)
225+
226+
],
227+
);
228+
229+
}
230+
231+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import 'package:shared_preferences/shared_preferences.dart';
2+
3+
/*
4+
Recordar instalar el paquete de:
5+
shared_preferences:
6+
7+
Inicializar en el main
8+
final prefs = new PreferenciasUsuario();
9+
prefs.initPrefs();
10+
11+
*/
12+
13+
class PreferenciasUsuario {
14+
15+
static final PreferenciasUsuario _instancia = new PreferenciasUsuario._internal();
16+
17+
factory PreferenciasUsuario() {
18+
return _instancia;
19+
}
20+
21+
PreferenciasUsuario._internal();
22+
23+
SharedPreferences _prefs;
24+
25+
initPrefs() async {
26+
this._prefs = await SharedPreferences.getInstance();
27+
}
28+
29+
// GET y SET de la última página
30+
get token {
31+
return _prefs.getString('token') ?? '';
32+
}
33+
34+
set token( String value ) {
35+
_prefs.setString('token', value);
36+
}
37+
38+
39+
// GET y SET de la última página
40+
get ultimaPagina {
41+
return _prefs.getString('ultimaPagina') ?? 'login';
42+
}
43+
44+
set ultimaPagina( String value ) {
45+
_prefs.setString('ultimaPagina', value);
46+
}
47+
48+
}
49+

0 commit comments

Comments
 (0)