AlertDialog在Flutter Web App上不可见

o2rvlv0m  于 2023-11-21  发布在  Flutter
关注(0)|答案(1)|浏览(144)

我在这里建立一个网络应用程序。我试图显示一个对话框的用户谁输入了错误的邮件/密码。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"),
            ),
          ],
        ),
      ),
    );
  }
}

字符串
如果你能帮忙的话,我将不胜感激。

6ovsh4lw

6ovsh4lw1#

问题是
在你的函数中,你调用showDialog

void signUserIn() async {
    showDialog(
      context: context,
      builder: (context) {
        return const Center(
          child: CircularProgressIndicator(),
        );
      },
    );

字符串
正如你所看到的,它需要一个context参数,这个参数需要一个BuildContext,这样它就应该知道在哪里显示对话框。
所以你想要的是传递一个BuildContext给singUserIn函数,然后在对话框中使用。

解决方案

首先在函数中接受BuildContext

void signUserIn(BuildContext context) async {
    showDialog(
      context: context,
      builder: (context) {
        return const Center(
          child: CircularProgressIndicator(),
        );
      },
    );


然后调用它:

onPressed: () => signUserIn(context),
              child: const Text("Giriş Yap"),

  • 对其他方法执行相同操作。*

请参见

相关问题