Skip to content

Instantly share code, notes, and snippets.

@chummypixels
Created December 26, 2021 09:51
Show Gist options
  • Save chummypixels/efb67ebafbd31d020f05cace1cd6b07a to your computer and use it in GitHub Desktop.
Save chummypixels/efb67ebafbd31d020f05cace1cd6b07a to your computer and use it in GitHub Desktop.
import 'package:firebase_setup/screens/register_screen.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../profile/profile_screen.dart';
import '../services/auth.dart';
class LoginScreen extends StatefulWidget {
const LoginScreen({Key? key}) : super(key: key);
@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
final _formKey = GlobalKey<FormState>();
@override
void dispose() {
_emailController.dispose();
_passwordController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final auth = Provider.of<Auth>(context);
return Scaffold(
appBar: AppBar(
title: const Text('Login'),
),
body: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: _emailController,
decoration:
const InputDecoration(labelText: 'Enter your email'),
keyboardType: TextInputType.emailAddress,
validator: (String? value) {
if (value == null || value.isEmpty) {
return 'Email is required';
}
return null;
},
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
obscureText: true,
controller: _passwordController,
decoration:
const InputDecoration(labelText: 'Enter your password'),
validator: (String? value) {
if (value == null || value.isEmpty) {
return 'Password is required';
} else if (value.length < 6) {
return 'Password should be at least 6 characters';
}
return null;
},
),
),
ElevatedButton(
onPressed: () async {
final isValid = _formKey.currentState!.validate();
await auth
.handleSignInEmail(
_emailController.text, _passwordController.text)
.then((value) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const ProfileScreen()));
}).catchError((e) => print(e));
},
child: const Text('Login')),
TextButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => const RegisterScreen()));
},
child: const Text('Don\'t have an account?'))
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment