我使用Flutter创建了一个登录和注册应用程序,其中显示了两个按钮(登录或注册), 在点击注册没有任何反应。调试控制台还显示viewpostime指针0或1。我是android开发的初学者,任何帮助将不胜感激。谢谢。
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
class LoginPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => new _LoginPageState();
}
enum FormType {
login,
register
}
class _LoginPageState extends State<LoginPage> {
final formKey = new GlobalKey<FormState>();
String _email;
String _password;
FormType _formType = FormType.login;
bool validateAndSave() {
final form = formKey.currentState;
if (form.validate()){
form.save();
return true;
} return false;
}
void validateAndSubmit() async {
if (validateAndSave()){
try{
if (_formType == FormType.login){
AuthResult user = await FirebaseAuth.instance.signInWithEmailAndPassword(email: _email,
password: _password);
print('Signed in : ${user.user.uid}');}
else{
AuthResult user = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: _email,
password: _password);
print('Registered User: ${user.user.uid}');
}
} catch (e){ print('Error: $e');}
}
}
void moveToRegister () {
formKey.currentState.reset();
setState(() {
_formType = FormType.register;
});
}
void moveToLogin(){
setState(() {
_formType = FormType.login;
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Flutter login demo'),
),
body: new Container(
padding: EdgeInsets.all(16.0),
child: Form(
key: formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: buildInputs() + buildSubmitButtons() ))));
}
List<Widget> buildInputs(){
return [
new TextFormField(
decoration: new InputDecoration(labelText: 'Email'),
validator: (value) => value.isEmpty ? 'Email can\'t be empty' : null,
onSaved: (value) => _email=value),
new TextFormField(
decoration: new InputDecoration(labelText: 'Password'),
obscureText: true,
validator: (value) => value.isEmpty ? 'Password can\'t be empty' : null,
onSaved: (value) => _password=value,
),
];
}
List<Widget> buildSubmitButtons(){
if (_formType == FormType.login){
return [
new RaisedButton(
child:
new Text('Login', style: new TextStyle(fontSize: 20.0)),
onPressed: validateAndSave,),
new FlatButton(
child: new Text('Create an Account', style: TextStyle(fontSize: 20.0)),
onPressed: moveToRegister,
)];
}else{
return [
new RaisedButton(
child:
new Text('Create an Account', style: new TextStyle(fontSize: 20.0)),
onPressed: validateAndSave,),
new FlatButton(
child: new Text('Have an Account? Login', style: TextStyle(fontSize: 20.0)),
onPressed: moveToLogin,
)
];
}
}}
///////////////////////////////////////////////// ///////////////////////////