summaryrefslogtreecommitdiff
path: root/lib/screens/register_screen.dart
blob: 16875f2878cf5351283b689b42e140430e2e9660 (plain)
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
import 'package:email_validator/email_validator.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:pmsna1/firebase/auth.dart';
import 'package:pmsna1/widgets/avatar_picker.dart';
import 'package:social_login_buttons/social_login_buttons.dart';

import '../widgets/loading_modal_widget.dart';

class RegisterScreen extends StatefulWidget {
  const RegisterScreen({super.key});

  @override
  State<RegisterScreen> createState() => _RegisterScreenState();
}

class _RegisterScreenState extends State<RegisterScreen> {
  bool isLoading = false;

  final padding = 16.0;
  final spacer = const SizedBox(height: 16.0);

  XFile? _avatar;

  // TextField controllers
  late TextEditingController _nameController;
  late TextEditingController _emailController;
  late TextEditingController _passwordController;

  final _formKey = GlobalKey<FormState>();

  @override
  void initState() {
    super.initState();
    _nameController = TextEditingController();
    _emailController = TextEditingController();
    _passwordController = TextEditingController();
  }

  bool validateForm() {
    if (_formKey.currentState!.validate() && _avatar != null) {
      return true;
    }
    return false;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: [
          SliverAppBar.large(
            title: const Text('Crear cuenta'),
          ),
          SliverFillRemaining(
            child: Stack(
              children: [
                SingleChildScrollView(
                  child: Column(
                    children: [
                      spacer,
                      Card(
                        margin:
                            EdgeInsets.fromLTRB(padding, 0, padding, padding),
                        child: Padding(
                          padding: EdgeInsets.all(padding),
                          child: Form(
                            key: _formKey,
                            child: Column(
                              mainAxisSize: MainAxisSize.min,
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                AvatarPicker(
                                  avatar: _avatar,
                                  onAvatarPicked: (avatar) {
                                    setState(() {
                                      _avatar = avatar;
                                    });
                                  },
                                ),
                                spacer,
                                TextFormField(
                                  controller: _nameController,
                                  decoration: const InputDecoration(
                                    border: OutlineInputBorder(),
                                    labelText: 'Nombre',
                                    hintText: 'Juan Pérez',
                                  ),
                                  keyboardType: TextInputType.name,
                                  validator: (value) {
                                    if (value == null || value.isEmpty) {
                                      return 'El nombre no debe estar vacío';
                                    }
                                    return null;
                                  },
                                ),
                                spacer,
                                TextFormField(
                                  controller: _emailController,
                                  decoration: const InputDecoration(
                                    border: OutlineInputBorder(),
                                    labelText: 'Correo electrónico',
                                    hintText: 'test@example.com',
                                  ),
                                  keyboardType: TextInputType.emailAddress,
                                  validator: (value) {
                                    if (value == null || value.isEmpty) {
                                      return 'El correo no debe estar vacío';
                                    } else if (!EmailValidator.validate(
                                        value)) {
                                      return 'El formato del correo es inválido';
                                    }
                                    return null;
                                  },
                                ),
                                spacer,
                                TextFormField(
                                  controller: _passwordController,
                                  obscureText: true,
                                  decoration: const InputDecoration(
                                    border: OutlineInputBorder(),
                                    labelText: 'Contraseña',
                                  ),
                                  validator: (value) {
                                    if (value == null || value.isEmpty) {
                                      return 'La contraseña no debe estar vacía';
                                    }
                                    return null;
                                  },
                                ),
                                spacer,
                                SocialLoginButton(
                                  buttonType:
                                      SocialLoginButtonType.generalLogin,
                                  text: 'Crear cuenta',
                                  backgroundColor:
                                      Theme.of(context).colorScheme.primary,
                                  onPressed: () => onRegisterClicked(context),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                isLoading ? const LoadingModal() : const SizedBox.shrink(),
              ],
            ),
          )
        ],
      ),
    );
  }

  void onRegisterClicked(BuildContext context) {
    setState(() {
      isLoading = false;
      if (validateForm()) {
        Auth()
            .createUserWithEmailAndPassword(
          email: _emailController.text,
          password: _passwordController.text,
        )
            .then((success) {
          if (success) {
            Navigator.of(context).pushNamed('/dash');
          }
        });
      }
    });
  }
}