summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/login.dart183
1 files changed, 148 insertions, 35 deletions
diff --git a/lib/login.dart b/lib/login.dart
index 7a4b0c8..b210650 100644
--- a/lib/login.dart
+++ b/lib/login.dart
@@ -1,48 +1,161 @@
import 'package:flutter/material.dart';
+import 'package:fluttertoast/fluttertoast.dart';
+import 'package:firebase_auth/firebase_auth.dart';
-class LoginPage extends StatelessWidget {
- final TextEditingController emailController = TextEditingController();
- final TextEditingController passwordController = TextEditingController();
+class LoginPage extends StatefulWidget {
+ const LoginPage({
+ Key? key,
+ }) : super(key: key);
+
+ @override
+ State<LoginPage> createState() => _LoginPageState();
+}
+
+class _LoginPageState extends State<LoginPage> {
+ final GlobalKey<FormState> _formKey = GlobalKey();
+
+ final FocusNode _focusNodePassword = FocusNode();
+ final TextEditingController _controllerUsername = TextEditingController();
+ final TextEditingController _controllerPassword = TextEditingController();
+
+ bool _obscurePassword = true;
@override
Widget build(BuildContext context) {
return Scaffold(
- appBar: AppBar(title: const Text('Login')),
- body: Padding(
- padding: const EdgeInsets.all(16.0),
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- TextField(
- controller: emailController,
- keyboardType: TextInputType.emailAddress,
- decoration: const InputDecoration(
- labelText: 'Email',
- border: OutlineInputBorder(),
+ backgroundColor: Colors.grey[300],
+ body: Form(
+ key: _formKey,
+ child: SingleChildScrollView(
+ padding: const EdgeInsets.all(30.0),
+ child: Column(
+ children: [
+ Image.asset(
+ "assets/logo.png",
+ height: 190,
),
- ),
- const SizedBox(height: 12.0),
- TextField(
- controller: passwordController,
- obscureText: true,
- decoration: const InputDecoration(
- labelText: 'Password',
- border: OutlineInputBorder(),
+ Text(
+ "Welcome back",
+ style: Theme.of(context).textTheme.headlineMedium,
),
- ),
- const SizedBox(height: 20.0),
- ElevatedButton(
- onPressed: () {
- // Perform the login action here
- // For now, let's just print the credentials
- print('Email: ${emailController.text.trim()}');
- print('Password: ${passwordController.text.trim()}');
- },
- child: const Text('Login'),
- ),
- ],
+ Text(
+ "Login to your account",
+ style: Theme.of(context).textTheme.bodySmall,
+ ),
+ const SizedBox(height: 40),
+ TextFormField(
+ controller: _controllerUsername,
+ keyboardType: TextInputType.name,
+ decoration: InputDecoration(
+ labelText: "Email",
+ prefixIcon: const Icon(Icons.email_outlined),
+ border: OutlineInputBorder(
+ borderRadius: BorderRadius.circular(10),
+ ),
+ enabledBorder: OutlineInputBorder(
+ borderRadius: BorderRadius.circular(10),
+ ),
+ ),
+ onEditingComplete: () => _focusNodePassword.requestFocus(),
+ validator: (String? value) {
+ if (value == null || value.isEmpty) {
+ return "Please enter username.";
+ }
+
+ return null;
+ },
+ ),
+ const SizedBox(height: 10),
+ TextFormField(
+ controller: _controllerPassword,
+ focusNode: _focusNodePassword,
+ obscureText: _obscurePassword,
+ keyboardType: TextInputType.visiblePassword,
+ decoration: InputDecoration(
+ labelText: "Password",
+ prefixIcon: const Icon(Icons.password_outlined),
+ suffixIcon: IconButton(
+ onPressed: () {
+ setState(() {
+ _obscurePassword = !_obscurePassword;
+ });
+ },
+ icon: _obscurePassword
+ ? const Icon(Icons.visibility_outlined)
+ : const Icon(Icons.visibility_off_outlined)),
+ border: OutlineInputBorder(
+ borderRadius: BorderRadius.circular(10),
+ ),
+ enabledBorder: OutlineInputBorder(
+ borderRadius: BorderRadius.circular(10),
+ ),
+ ),
+ validator: (String? value) {
+ if (value == null || value.isEmpty) {
+ return "Please enter password.";
+ }
+
+ return null;
+ },
+ ),
+ const SizedBox(height: 60),
+ Column(
+ children: [
+ ElevatedButton(
+ style: ElevatedButton.styleFrom(
+ minimumSize: const Size.fromHeight(50),
+ shape: RoundedRectangleBorder(
+ borderRadius: BorderRadius.circular(20),
+ ),
+ backgroundColor: Color(0xFF355291),
+ ),
+ onPressed: () async {
+ if (_formKey.currentState?.validate() ?? false) {
+ try {
+ await FirebaseAuth.instance
+ .signInWithEmailAndPassword(
+ email: _controllerUsername.text.trim(),
+ password: _controllerPassword.text.trim())
+ .then((value) =>
+ Navigator.pushNamedAndRemoveUntil(
+ context, '/', (route) => false));
+ } on FirebaseAuthException catch (e) {
+ Fluttertoast.showToast(
+ msg: e.message.toString(),
+ gravity: ToastGravity.SNACKBAR);
+ }
+ }
+ },
+ child: const Text("Login"),
+ ),
+ Row(
+ mainAxisAlignment: MainAxisAlignment.center,
+ children: [
+ const Text("Don't have an account?"),
+ TextButton(
+ onPressed: () {
+ _formKey.currentState?.reset();
+ Navigator.pushNamedAndRemoveUntil(
+ context, "/signup", (route) => false);
+ },
+ child: const Text("Signup"),
+ ),
+ ],
+ ),
+ ],
+ ),
+ ],
+ ),
),
),
);
}
+
+ @override
+ void dispose() {
+ _focusNodePassword.dispose();
+ _controllerUsername.dispose();
+ _controllerPassword.dispose();
+ super.dispose();
+ }
}