如何在Flutter过渡之间显示进度轮/进度条?

时间:2019-02-16 15:29:08

标签: flutter navigation progress-bar alert

我创建了一个自定义登录页面,该页面的外观和工作方式符合我的预期。在确定Firebase用户身份并提取用户名,照片或占位符时,我无法弄清楚代码中显示进度指示器的方式和位置。另外,如果身份验证失败,则在何处以及如何显示警报。这是我当前的代码...

Future<Null> _loginButton() async {
    _email = _emailController.text.toString().replaceAll(" ", "");
    _password = _passController.text.toString().replaceAll(" ", "");
    //_username = _nameController.text.toString().replaceAll(" ", "");
    if (_email != null && _password != null) {
      try {
        await FirebaseAuth.instance
            .signInWithEmailAndPassword(email: _email, password: _password);

        final FirebaseUser currentUser = await _auth.currentUser();
        final userid = currentUser.uid;
        currentUserId = userid;


        FirebaseDatabase.instance
            .reference()
            .child('users/$userid')
            .onValue
            .listen((Event event) {
          if (event.snapshot.value == null) {
            imageString = "placeholder";
            name = _username;
          } else if (event.snapshot.value != null) {
            imageString = event.snapshot.value['image'];
            name = event.snapshot.value['displayName'];
          }

          fb.child('users/$userid').set({
            'displayName': name,
            'image': imageString,
            'uid' : userid.toString()
          });
        }).onDone(() {
          Navigator.pushNamed(context, '/menu');
          Main.uid = userid;
        });
      } catch (error) {

      }
    } else {}
  } 

1 个答案:

答案 0 :(得分:1)

在我的项目中查看此功能示例,以在抖动过渡之间实现CircularProgressIndicator

class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  bool isLoading = false;

  @override
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
            child: isLoading
                ? Column(
                    children: <Widget>[
                      CircularProgressIndicator(),
                      Divider(
                        height: 20,
                        color: Colors.transparent,
                      ),
                      Text("Please wait..."),
                    ],
                    mainAxisSize: MainAxisSize.min,
                  )
                : MaterialButton(
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        Image.asset(
                          'assets/google.png',
                          width: 25.0,
                        ),
                        Padding(
                          child: Text(
                            "Sign in with Google",
                            style: TextStyle(
                              fontFamily: 'Roboto',
                              color: Color.fromRGBO(68, 68, 76, .8),
                            ),
                          ),
                          padding: new EdgeInsets.only(left: 15.0),
                        ),
                      ],
                    ),
                    onPressed: () {
                      googleUserSignIn().then((user) {
                        this.setState(() {
                          //isLoading = false;
                          // Your task....
                        });
                      });
                    },
                    color: Colors.white,
                    elevation: 5,
                    highlightElevation: 2,
                  )));
  }

  Future<FirebaseUser> googleUserSignIn() async {
    this.setState(() {
      isLoading = true;
    });

    GoogleSignInAccount googleUser = await Firebase().firebaseGSI.signIn();

    GoogleSignInAuthentication googleAuth = await googleUser.authentication;

    final AuthCredential credential = GoogleAuthProvider.getCredential(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );

    FirebaseUser user =
        await Firebase().firebaseAuth.signInWithCredential(credential);

    return user;
  }
}