diff options
author | Iván Ávalos <avalos@disroot.org> | 2023-05-20 16:43:21 -0600 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2023-05-20 16:43:21 -0600 |
commit | cd8f22959e62c0b7b191e64f4ecfec53cba9f364 (patch) | |
tree | 9e40717d4e7b5c18694ab65558acda65875f817b /lib/screens/register_screen.dart | |
parent | 3a3af285a7460a7e2f6850b9a8dd7465492163f6 (diff) | |
download | linkchat-cd8f22959e62c0b7b191e64f4ecfec53cba9f364.tar.gz linkchat-cd8f22959e62c0b7b191e64f4ecfec53cba9f364.tar.bz2 linkchat-cd8f22959e62c0b7b191e64f4ecfec53cba9f364.zip |
Se añade autenticación y onboarding
Diffstat (limited to 'lib/screens/register_screen.dart')
-rw-r--r-- | lib/screens/register_screen.dart | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/lib/screens/register_screen.dart b/lib/screens/register_screen.dart new file mode 100644 index 0000000..349cac0 --- /dev/null +++ b/lib/screens/register_screen.dart @@ -0,0 +1,178 @@ +import 'dart:io'; + +import 'package:email_validator/email_validator.dart'; +import 'package:flutter/material.dart'; +import 'package:image_picker/image_picker.dart'; +import 'package:social_login_buttons/social_login_buttons.dart'; + +import '../firebase/auth.dart'; +import '../widgets/avatar_picker.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, + displayName: _nameController.text, + avatar: File(_avatar!.path), + ) + .then((success) { + if (success) { + Navigator.of(context).pushNamed('/onboard'); + } + }); + } + }); + } +} |