【问题标题】:How to display a circular progress indicator whilst waiting for Authentication from Firebase如何在等待来自 Firebase 的身份验证时显示圆形进度指示器
【发布时间】:2021-05-18 12:51:59
【问题描述】:

我有一个通过 Firebase 身份验证的登录页面,我想在仍在进行身份验证的同时显示一个圆形进度指示器。 我在登录按钮的 catch 块中插入了 if 语句,用于处理登录过程中可能出现的错误

如果 firebase 服务器没有抛出错误,我想显示圆形指示器 我是编码新手,请帮助我。

下面是登录页面的代码

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:user_details/fromJP/Scan_QR.dart';
import 'package:user_details/fromJP/load_widget.dart';
import 'Register.dart';

class Login extends StatefulWidget {
  @override
  _LoginState createState() => _LoginState();
}

class _LoginState extends State<Login> {
  final formKey = GlobalKey<FormState>();
  bool loading = false;

  TextEditingController _emailController = TextEditingController();
  TextEditingController _passwordController = TextEditingController();

  String errorMessage;

  @override
  Widget build(BuildContext context) {
    final GlobalKey<ScaffoldState> _scafoldKey = GlobalKey<ScaffoldState>();
    return loading
        ? Loading()
        : Scaffold(
            key: _scafoldKey,
            appBar: AppBar(
              title: Text("Jinjer Pay"),
            ),
            body: Container(
              alignment: Alignment.center,
              decoration: BoxDecoration(
                  image: DecorationImage(
                      image: AssetImage("lib/bg.jpg"), fit: BoxFit.cover)),
              child: Form(
                key: formKey,
                child: SingleChildScrollView(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Container(
                          margin: EdgeInsets.fromLTRB(15, 0, 15, 0),
                          padding: EdgeInsets.all(35),
                          decoration: BoxDecoration(
                              shape: BoxShape.rectangle,
                              color: Colors.white,
                              borderRadius:
                                  BorderRadius.all(Radius.circular(20))),
                          child: Column(
                            children: <Widget>[
                              SizedBox(
                                height: 20,
                              ),
                              TextFormField(
                                decoration:
                                    new InputDecoration(hintText: "Email"),
                                keyboardType: TextInputType.emailAddress,
                                controller: _emailController,
                              ),
                              SizedBox(
                                height: 20,
                              ),
                              TextFormField(
                                decoration:
                                    new InputDecoration(hintText: "Password"),
                                obscureText: true,
                                enableSuggestions: false,
                                controller: _passwordController,
                              ),
                              SizedBox(
                                height: 20,
                              ),
                              RaisedButton(
                                child: Text(
                                  "Log In",
                                  style: TextStyle(fontSize: 19),
                                ),
                                color: Colors.amberAccent,
                                elevation: 10,
                                shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(15)),
                                onPressed: () async {
                                  if (formKey.currentState.validate()) {
                                    setState(() => loading = true);

                                    try {
                                      UserCredential res;
                                      String userID;

                                      res = (await FirebaseAuth.instance
                                          .signInWithEmailAndPassword(
                                              email: _emailController.text,
                                              password:
                                                  _passwordController.text));
                                      userID = res.user.uid;

                                      if (res == null) {
                                        setState(() {
                                          loading = false;
                                        });
                                      }
                                      if (res != null) {
                                        Navigator.pop(context);
                                        Navigator.of(context).push(
                                          MaterialPageRoute(
                                              builder: (context) => Scan_QR()),
                                        );
                                      }
                                    } catch (e) {
                                      if (e.code == "unknown") {
                                        Fluttertoast.showToast(
                                            msg:
                                                "Email/Password Field cannot be Empty",
                                            toastLength: Toast.LENGTH_SHORT,
                                            gravity: ToastGravity.CENTER,
                                            timeInSecForIosWeb: 1,
                                            backgroundColor: Colors.redAccent,
                                            textColor: Colors.white,
                                            fontSize: 16.0);
                                      }
                                      if (e.code == "wrong-password") {
                                        Fluttertoast.showToast(
                                            msg: "Your password is incorrect",
                                            toastLength: Toast.LENGTH_SHORT,
                                            gravity: ToastGravity.CENTER,
                                            timeInSecForIosWeb: 1,
                                            backgroundColor: Colors.redAccent,
                                            textColor: Colors.white,
                                            fontSize: 16.0);
                                      }
                                      if (e.code == "invalid-email") {
                                        Fluttertoast.showToast(
                                            msg:
                                                "Email/Password Field cannot be Empty",
                                            toastLength: Toast.LENGTH_SHORT,
                                            gravity: ToastGravity.CENTER,
                                            timeInSecForIosWeb: 1,
                                            backgroundColor: Colors.redAccent,
                                            textColor: Colors.white,
                                            fontSize: 16.0);
                                      }
                                      if (e.code == "user-not-found") {
                                        Fluttertoast.showToast(
                                            msg:
                                                "No User found with this email",
                                            toastLength: Toast.LENGTH_SHORT,
                                            gravity: ToastGravity.CENTER,
                                            timeInSecForIosWeb: 1,
                                            backgroundColor: Colors.redAccent,
                                            textColor: Colors.white,
                                            fontSize: 16.0);
                                      }
                                      if (e.code == "user-disabled") {
                                        Fluttertoast.showToast(
                                            msg:
                                                "user with this email has been disabled",
                                            toastLength: Toast.LENGTH_SHORT,
                                            gravity: ToastGravity.CENTER,
                                            timeInSecForIosWeb: 1,
                                            backgroundColor: Colors.redAccent,
                                            textColor: Colors.white,
                                            fontSize: 16.0);
                                      }
                                      if (e.code == "too-many-requests") {
                                        Fluttertoast.showToast(
                                            msg:
                                                "Too many requests, try again later",
                                            toastLength: Toast.LENGTH_SHORT,
                                            gravity: ToastGravity.CENTER,
                                            timeInSecForIosWeb: 1,
                                            backgroundColor: Colors.redAccent,
                                            textColor: Colors.white,
                                            fontSize: 16.0);
                                      }
                                      print(e);
                                      //_emailController.text = "";
                                      _passwordController.text = "";
                                    }
                                  }
                                },
                              ),
                              SizedBox(
                                height: 20,
                              ),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: <Widget>[
                                  Text(
                                    "Not Registered? ",
                                    style: TextStyle(fontSize: 19),
                                  ),
                                  SizedBox(width: 5),
                                  InkWell(
                                    onTap: () {
                                      Navigator.of(context).push(
                                        MaterialPageRoute(
                                            builder: (context) => Register()),
                                      );
                                    },
                                    child: Text(
                                      "Register",
                                      style: TextStyle(
                                          color: Colors.blue, fontSize: 19),
                                    ),
                                  ),
                                  SizedBox(
                                    height: 20,
                                  ),
                                ],
                              ),
                            ],
                          ))
                    ],
                  ),
                ),
              ),
            ),
          );
  }
}

【问题讨论】:

    标签: firebase flutter dart firebase-authentication


    【解决方案1】:

    当你成功或抛出错误时,你可以在 try 块和 Navigator.pop(context); 中使用 Center(child:CircularProgressIndicator()) showDialogNavigator.pop(context);

    【讨论】:

    • 我不明白你的回答
    • 我已经编辑了关于您的回答的问题
    【解决方案2】:

    您可以在您的小部件中创建一个 bool isLoading 属性,该属性初始化为 false。然后在您的函数调用中,您可以在调用firebase之前设置状态并将isLoading更改为true。当对 firebase 的调用成功完成或引发错误时,您可以设置状态并将 isLoading 再次更改为 false。

    最后,如果 isLoading 为真,您可以在 UI 中使用条件来显示一个循环进度指示器。

    【讨论】:

    • 我已根据您的回答编辑了我的问题
    • 现在是否显示加载指示器?另外,我不会像您现在那样使用脚手架的加载条件。这将导致当加载为真时,您将移除脚手架,这对用户来说可能看起来很糟糕。此外,当您再次将 is loading 设置为 false 时,您将不得不重新渲染整个页面。更好的选择是将条件检查放在需要它的小部件树的更下方。
    • 我在 TextFormFields 中添加了验证语句,在加载进度循环指示器之前进行验证,然后如果服务器出现错误,它会在 toast 中返回该错误。
    【解决方案3】:

    我在 catch 块的开头添加了这个块

    if(FirebaseAuth.instance.currentUser.uid != e.code)
                                            {
                                              setState(() {
                                                loading = false;
                                              });
                                            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-17
      • 2020-03-06
      • 2019-09-29
      • 1970-01-01
      • 2020-08-10
      • 1970-01-01
      • 1970-01-01
      • 2016-10-04
      相关资源
      最近更新 更多