I have been trying to make my alert dialog show when user input incorrect email or password and it hasn't been coming up, just the CircularProgressIndicator spinning then stop. Here's by code. Any ideas please? Before, it was showing black screen but then I realised that i used Navigator.pop in multiple places.
import 'package:bet_corner/components/my_textfield.dart';
import 'package:bet_corner/constants/text_strings.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import '../components/my_buttons.dart';
import '../components/square_tile.dart';
class LoginPage extends StatefulWidget {
const LoginPage({Key? key}) : super(key: key);
@override
State<LoginPage> createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final emailController = TextEditingController();
final passwordController = TextEditingController();
void signUserIn() async {
showDialog(
context: context,
builder: (context) {
return const Center(
child: CircularProgressIndicator(),
);
},
);
try {
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: emailController.text,
password: passwordController.text,
);
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
wrongEmailMessage();
} else if (e.code == 'wrong-password') {
wrongPasswordMessage();
}
}
Navigator.pop(context);
}
void wrongEmailMessage() {
showDialog(
context: context,
builder: (context) {
return const AlertDialog(
title: Text('Incorrect Email'),
);
},
);
}
void wrongPasswordMessage() {
showDialog(
context: context,
builder: (context) {
return const AlertDialog(
title: Text('Incorrect password'),
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[300],
body: SafeArea(
child: Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(height: 100),
const Icon(
Icons.lock,
size: 100,
),
const SizedBox(height: 50),
Text(
jLoginTitle,
style: TextStyle(color: Colors.grey[700], fontSize: 17),
),
const SizedBox(height: 25),
MyTextField(
controller: emailController,
hintText: 'Email',
obscureText: false,
),
const SizedBox(height: 10),
MyTextField(
controller: passwordController,
hintText: 'Password',
obscureText: true,
),
const SizedBox(
height: 20,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text(
'Forgot Password?',
style: TextStyle(color: Colors.grey[600]),
),
],
),
),
const SizedBox(height: 10),
// sign in button
MyButton(
onTap: signUserIn,
),
const SizedBox(height: 50),
// or continue with
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: Row(
children: [
Expanded(
child: Divider(
thickness: 0.5,
color: Colors.grey[400],
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: Text(
'Or continue with',
style: TextStyle(color: Colors.grey[700]),
),
),
Expanded(
child: Divider(
thickness: 0.5,
color: Colors.grey[400],
),
),
],
),
),
const SizedBox(height: 50),
// google + apple sign in buttons
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
// google button
SquareTile(imagePath: 'assets/images/google.png'),
SizedBox(width: 25),
// apple button
SquareTile(imagePath: 'assets/images/apple.png')
],
),
const SizedBox(height: 50),
// not a member? register now
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Not a member?',
style: TextStyle(color: Colors.grey[700]),
),
const SizedBox(width: 4),
const Text(
'Register now',
style: TextStyle(
color: Colors.blue,
fontWeight: FontWeight.bold,
),
),
],
)
],
),
),
),
),
);
}
}
This is the TextField widget component I created for MyTextField.
import 'package:flutter/material.dart';
class MyTextField extends StatelessWidget {
final controller;
final String hintText;
final bool obscureText;
const MyTextField({
Key? key,
this.controller,
required this.hintText,
required this.obscureText,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: TextField(
controller: controller,
obscureText: obscureText,
decoration: InputDecoration(
enabledBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey.shade400),
),
fillColor: Colors.grey.shade200,
filled: true,
hintText: hintText,
hintStyle: TextStyle(color: Colors.grey[500])),
),
);
}
}
Try this format
void signUserIn() async {
try {
showDialog(
context: context,
builder: (context) {
return const Center(
child: CircularProgressIndicator(),
);
},
);
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: emailController.text,
password: passwordController.text,
);
Navigator.pop(context);// if no exception on top it will close the dialog
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
wrongEmailMessage(); // this will close by user
} else if (e.code == 'wrong-password') {
wrongPasswordMessage();// this will close by user
}
}
}