我对应用程序开发很陌生,对Firebase还是很陌生,而且在网上很难找到适合我专门编写的代码的资源,这很困难。我已经编辑了所需的页面(例如pubsepc文件,build.gradle文件等),我在编辑实际代码时遇到了麻烦。对于我的登录页面,我使用了多个TextField和Material按钮。这段代码是_HomePageState类的一部分,扩展了State部分的代码。
谁能帮助我找出一种技术或给我一个提示,以连接Firebase?我在网上看到了其他示例,但没有一个像我的代码那样专门使用TextField。许多都具有附加功能,我不确定是否需要它们。我更喜欢使用TextField,但甚至可以使用TextField吗?谢谢。
@override
Widget build(BuildContext context) {
final email = TextField(
obscureText: false,
style: style,
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
hintText: "Email",
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
);
final password = TextField(
obscureText: true,
style: style,
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
hintText: "Password",
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
);
final loginButon = Material(
elevation: 5.0,
borderRadius: BorderRadius.circular(30.0),
color: Color(0xff01A0C7),
child: MaterialButton(
minWidth: MediaQuery.of(context).size.width,
padding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
onPressed: () {},
child: Text("Login",
textAlign: TextAlign.center,
style: style.copyWith(
color: Colors.white, fontWeight: FontWeight.bold)),
),
);
最佳答案
您可以执行的操作,此代码用于登录按钮
userEmail和userPassword是您从两个文本字段中获取的字符串
onPressed: () async {
try {
AuthResult result = await FirebaseAuth.instance
.signInWithEmailAndPassword(
email: userEmail, password: userPassword);
print(result.user.email : userEmail);//#debug statement
setState(() {});
//navigator push here, to go to next page, if login is successfull
} catch (e) {
print('error during login is $e'); // #debug
}
},
在初始状态下,您可以使用此功能检查是否有人登录,这是用户下次启动应用程序时的自动登录。
@override
void initState() {
isUserLoggedIn();
// TODO: implement initState
super.initState();
}
isUserLoggedIn() async {
var x = await FirebaseAuth.instance.currentUser();
if (x != null) {
// code to be execute if someone is already logged in, take him to next page
}
}
关于firebase - 将Firebase连接到我的TextField/ Material 登录页面代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59549834/