我在这里建立一个网络应用程序。我试图显示一个对话框的用户谁输入了错误的邮件/密码。Vs代码表示没有错误的代码,但我看不到警告框无论如何。在FlutterDevtools我只是得到这个消息:对话框路线
但只显示进度指示器,而不显示对话框。
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
class DesktopLoginPage extends StatefulWidget {
const DesktopLoginPage({
super.key,
required this.emailController,
required this.passwordController,
});
final TextEditingController emailController;
final TextEditingController passwordController;
@override
State<DesktopLoginPage> createState() => _DesktopLoginPageState();
}
class _DesktopLoginPageState extends State<DesktopLoginPage> {
void signUserIn() async {
showDialog(
context: context,
builder: (context) {
return const Center(
child: CircularProgressIndicator(),
);
},
);
try {
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: widget.emailController.text,
password: widget.passwordController.text);
Navigator.pop(context);
} on FirebaseAuthException catch (e) {
Navigator.pop(context);
if (e.code == "user-not-found") {
wrongEmailMessage();
} else if (e.code == "wrong-password") {
wrongPasswordMessage();
}
}
}
void wrongEmailMessage() {
showDialog(
context: context,
builder: (BuildContext context) {
return const AlertDialog(
title: Text("Hatalı mail adresi girdiniz"),
);
},
);
}
void wrongPasswordMessage() {
showDialog(
context: context,
builder: (BuildContext context) {
return const AlertDialog(
title: Text("Hatalı parola girdiniz"),
);
},
);
}
@override
Widget build(BuildContext context) {
return Container(
color: Colors.blue.shade100,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 300.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text("Giriş Yapın"),
const SizedBox(height: 10),
TextField(
controller: widget.emailController,
decoration: InputDecoration(
fillColor: Colors.white,
filled: true,
hintText: "E-posta Adresiniz",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5),
),
),
),
const SizedBox(height: 10),
TextField(
obscureText: true,
controller: widget.passwordController,
decoration: InputDecoration(
fillColor: Colors.white,
filled: true,
hintText: "Parolanız",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5),
),
),
),
const SizedBox(height: 10),
TextButton(
style: TextButton.styleFrom(
padding: const EdgeInsets.all(10),
backgroundColor: Colors.pink.shade100,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(5),
),
),
),
onPressed: signUserIn,
child: const Text("Giriş Yap"),
),
],
),
),
);
}
}
字符串
如果你能帮忙的话,我将不胜感激。
1条答案
按热度按时间6ovsh4lw1#
问题是
在你的函数中,你调用
showDialog
:字符串
正如你所看到的,它需要一个
context
参数,这个参数需要一个BuildContext
,这样它就应该知道在哪里显示对话框。所以你想要的是传递一个BuildContext给
singUserIn
函数,然后在对话框中使用。解决方案
首先在函数中接受
BuildContext
:型
然后调用它:
型
请参见