content: import 'package:firebase_auth/firebase_auth.dart';
  import 'package:flutter/material.dart';
  import 'package:flutterfire_samples/res/custom_colors.dart';
  
  import '../../../common/widgets/custom_form_field.dart';
  import '../screens/sign_in_screen.dart';
  import '../screens/user_info_screen.dart';
  import '../utils/authentication.dart';
  import '../utils/validator.dart';
  
  class RegisterForm extends StatefulWidget {
    final FocusNode nameFocusNode;
    final FocusNode emailFocusNode;
    final FocusNode passwordFocusNode;
  
    const RegisterForm({
      Key? key,
      required this.nameFocusNode,
      required this.emailFocusNode,
      required this.passwordFocusNode,
    }) : super(key: key);
    @override
    RegisterFormState createState() => RegisterFormState();
  }
  
  class RegisterFormState extends State<RegisterForm> {
    final TextEditingController _nameController = TextEditingController();
    final TextEditingController _emailController = TextEditingController();
    final TextEditingController _passwordController = TextEditingController();
  
    final _registerFormKey = GlobalKey<FormState>();
  
    bool _isSigningUp = false;
  
    Route _routeToSignInScreen() {
      return PageRouteBuilder(
        pageBuilder: (context, animation, secondaryAnimation) =>
            const SignInScreen(),
        transitionsBuilder: (context, animation, secondaryAnimation, child) {
          var begin = const Offset(-1.0, 0.0);
          var end = Offset.zero;
          var curve = Curves.ease;
  
          var tween =
              Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
  
          return SlideTransition(
            position: animation.drive(tween),
            child: child,
          );
        },
      );
    }
  
    @override
    Widget build(BuildContext context) {
      return Form(
        key: _registerFormKey,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Padding(
              padding: const EdgeInsets.only(
                left: 8.0,
                right: 8.0,
              ),
              child: Column(
                children: [
                  CustomFormField(
                    controller: _nameController,
                    focusNode: widget.nameFocusNode,
                    keyboardType: TextInputType.name,
                    inputAction: TextInputAction.next,
                    isCapitalized: true,
                    validator: (value) => Validator.validateName(
                      name: value,
                    ),
                    label: 'Name',
                    hint: 'Enter your name',
                  ),
                  const SizedBox(height: 16.0),
                  CustomFormField(
                    controller: _emailController,
                    focusNode: widget.emailFocusNode,
                    keyboardType: TextInputType.emailAddress,
                    inputAction: TextInputAction.next,
                    validator: (value) => Validator.validateEmail(
                      email: value,
                    ),
                    label: 'Email',
                    hint: 'Enter your email',
                  ),
                  const SizedBox(height: 16.0),
                  CustomFormField(
                    controller: _passwordController,
                    focusNode: widget.passwordFocusNode,
                    keyboardType: TextInputType.text,
                    inputAction: TextInputAction.done,
                    validator: (value) => Validator.validatePassword(
                      password: value,
                    ),
                    isObscure: true,
                    label: 'Password',
                    hint: 'Enter your password',
                  ),
                ],
              ),
            ),
            const SizedBox(height: 24.0),
            _isSigningUp
                ? const Padding(
                    padding: EdgeInsets.all(16.0),
                    child: CircularProgressIndicator(
                      valueColor: AlwaysStoppedAnimation<Color>(
                        Palette.firebaseOrange,
                      ),
                    ),
                  )
                : Padding(
                    padding: const EdgeInsets.only(left: 0.0, right: 0.0),
                    child: SizedBox(
                      width: double.maxFinite,
                      child: ElevatedButton(
                        style: ButtonStyle(
                          backgroundColor: MaterialStateProperty.all(
                            Palette.firebaseOrange,
                          ),
                          shape: MaterialStateProperty.all(
                            RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(10),
                            ),
                          ),
                        ),
                        onPressed: () async {
                          widget.emailFocusNode.unfocus();
                          widget.passwordFocusNode.unfocus();
  
                          setState(() {
                            _isSigningUp = true;
                          });
  
                          if (_registerFormKey.currentState!.validate()) {
                            User? user =
                                await Authentication.registerUsingEmailPassword(
                              name: _nameController.text.trim(),
                              email: _emailController.text.trim(),
                              password: _passwordController.text.trim(),
                              context: context,
                            );
  
                            if (user != null) {
                              Navigator.of(context).pushReplacement(
                                MaterialPageRoute(
                                  builder: (context) => UserInfoScreen(
                                    user: user,
                                  ),
                                ),
                              );
                            }
                          }
  
                          setState(() {
                            _isSigningUp = false;
                          });
                        },
                        child: const Padding(
                          padding: EdgeInsets.only(top: 16.0, bottom: 16.0),
                          child: Text(
                            'REGISTER',
                            style: TextStyle(
                              fontSize: 24,
                              fontWeight: FontWeight.bold,
                              color: Palette.firebaseGrey,
                              letterSpacing: 2,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
            const SizedBox(height: 16.0),
            InkWell(
              onTap: () {
                Navigator.of(context).pushReplacement(
                  _routeToSignInScreen(),
                );
              },
              child: const Text(
                'Already have an account? Sign in',
                style: TextStyle(
                  color: Palette.firebaseGrey,
                  letterSpacing: 0.5,
                ),
              ),
            )
          ],
        ),
      );
    }
  }
  