flutter alertDialog没有出现



我一直试图让我的警报对话框显示,当用户输入不正确的电子邮件或密码,它还没有出现,只是循环progressindicator旋转然后停止。下面是代码。有什么想法吗?之前,它显示黑屏,但后来我意识到,我使用了导航。在多个地方弹出

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,
),
),
],
)
],
),
),
),
),
);
}
}

这是我为MyTextField创建的TextField组件。

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])),
),
);
}
}

试试这个格式

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
}
}
}

最新更新