mirrored_repositories/firebase-chatapp/lib
import 'package:chatapp/constants.dart';
import 'package:chatapp/presentation/chat_screen.dart';
import 'package:chatapp/widgets/custom_button.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:modal_progress_hud_nsn/modal_progress_hud_nsn.dart';

class RegistrationScreen extends StatefulWidget {
  static const String id = 'registration_screen';
  @override
  _RegistrationScreenState createState() => _RegistrationScreenState();
}

class _RegistrationScreenState extends State<RegistrationScreen> {
  final _auth = FirebaseAuth.instance;
  late String email;
  late String password;
  bool showSpinner = false ;
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: ModalProgressHUD(
        inAsyncCall: showSpinner,
        child: Padding(
          padding: EdgeInsets.symmetric(horizontal: 24.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              Flexible(
                child: Hero(
                  tag: 'logo',
                  child: Container(
                    height: 200.0,
                    child: Image.asset('images/logo.png'),
                  ),
                ),
              ),
              SizedBox(
                height: 48.0,
              ),
              TextField(
                keyboardType: TextInputType.emailAddress,
                textAlign: TextAlign.center,
                  onChanged: (value) {
                    email = value;
                  },
                  decoration:
                      kInputDecoration.copyWith(hintText: 'Enter your email')),
              SizedBox(
                height: 8.0,
              ),
              TextField(
                
                textAlign: TextAlign.center,
                obscureText: true,
                  onChanged: (value) {
                    password = value;
                  },
                  decoration:
                      kInputDecoration.copyWith(hintText: 'Enter your password')),
              SizedBox(
                height: 24.0,
              ),
              CustomButton(
                  text: 'Register',
                  color: Colors.blue,
                  onPressed: () async{
                    setState(() {
                      showSpinner = true;
                    });
                  
                    try{
                    final newUser =  await _auth.createUserWithEmailAndPassword(email: email, password: password);
                    if(newUser != null){Navigator.pushReplacementNamed(context, ChatScreen.id);}}
                    catch(e){
                      print(e);
                    }})
            ],
          ),
        ),
      ),
    );
  }
}
