簡體   English   中英

如何在等待來自 Firebase 的身份驗證時顯示圓形進度指示器

[英]How to display a circular progress indicator whilst waiting for Authentication from Firebase

我有一個由 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,
                                  ),
                                ],
                              ),
                            ],
                          ))
                    ],
                  ),
                ),
              ),
            ),
          );
  }
}

您可以在showDialog塊和Navigator.pop(context);中顯示帶有Center(child:CircularProgressIndicator())try 當您成功或拋出錯誤時

您可以在初始化為 false 的小部件中創建 bool isLoading 屬性。 然后在您的 function 調用中,您可以設置 state 並將您的 isLoading 更改為 true,然后再調用 firebase。 當對 firebase 的調用成功完成或引發錯誤時,您可以設置 state 並將 isLoading 再次更改為 false。

最后,在 UI 中,如果 isLoading 為真,您可以使用條件來顯示循環進度指示器。

我在 catch 塊的開頭添加了這個塊

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM