Flutter: после FirebaseAuth.instance.createUserWithEmailAndPassword Как я могу перенаправить на домашнюю страницу или страницу OTPScreen?
Я хочу, чтобы мой пользователь перенаправлял на экран OTP, а оттуда на домашнюю страницу, когда они подписываются или регистрируют адрес электронной почты. Но я не могу перенаправить другую страницу, когда пользователь регистрирует адрес электронной почты.
Пожалуйста, проверьте приведенный ниже код и предложите, как сделать это и перенаправить на нужную страницу. пожалуйста, исправьте этот код, когда я обращаюсь за помощью:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:myapp/Screens/Login/login_screen.dart';
import 'package:myapp/Screens/Signup/Otp.dart';
import 'package:myapp/Screens/Signup/components/background.dart';
import 'package:myapp/Screens/Signup/components/or_divider.dart';
import 'package:myapp/Screens/Signup/components/social_icon.dart';
import 'package:myapp/components/already_have_an_account_acheck.dart';
import 'package:myapp/components/rounded_button.dart';
import 'package:myapp/components/rounded_input_field.dart';
import 'package:myapp/components/rounded_password_field.dart';
import 'package:email_auth/email_auth.dart';
class SignUpScreen extends StatefulWidget {
@override
_SignUpScreenState createState() => _SignUpScreenState();
}
class _SignUpScreenState extends State<SignUpScreen> {
GlobalKey<ScaffoldState> globalKey = GlobalKey<ScaffoldState>();
TextEditingController email = TextEditingController();
TextEditingController password = TextEditingController();
TextEditingController otpController = TextEditingController();
bool loading = false;
void sendOTP()async{
EmailAuth.sessionName = "Test Session";
var res = await EmailAuth.sendOtp(receiverMail: email.text);
if(res){
globalKey.currentState.showSnackBar(SnackBar(
content: Text("send otp Successfull"),
),
);
}
}
void verifyOTP()async{
//EmailAuth.sessionName = "Test Session";
var res = await EmailAuth.validate(receiverMail: email.text,userOTP: otpController.text);
if(res){
globalKey.currentState.showSnackBar(SnackBar(
content: Text("Validation Successfull"),
));
}
else{
globalKey.currentState.showSnackBar(SnackBar(
content: Text("Validation Un-Successfull"),
));
}
}
Future sendData() async {
try {
UserCredential userCredential =
await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: email.text,
password: password.text,
);
await FirebaseFirestore.instance
.collection('userData')
.doc(userCredential.user.uid)
.set({
'email': email.text.trim(),
'userid': userCredential.user.uid,
'password': password.text.trim()
});
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
globalKey.currentState.showSnackBar(SnackBar(
content: Text("Password is Weak"),
));
} else if (e.code == 'email-already-in-use') {
globalKey.currentState.showSnackBar(SnackBar(
content: Text("Email is already in use"),
));
}
} catch (e) {
globalKey.currentState.showSnackBar(SnackBar(
content: Text(e),
));
setState(() {
loading = false;
});
}
setState(() {
loading = false;
});
}
void validation() {
if (email.text.trim().isEmpty ||
email.text.trim() == null && password.text.trim().isEmpty ||
password.text.trim() == null) {
globalKey.currentState.showSnackBar(SnackBar(
content: Text("Both Fields are Empty"),
));
} else if (email.text.trim().isEmpty || email.text.trim() == null) {
globalKey.currentState.showSnackBar(SnackBar(
content: Text("Email is Empty"),
));
return;
} else if (password.text.trim().isEmpty || password.text.trim() == null) {
globalKey.currentState.showSnackBar(SnackBar(
content: Text("Password is Empty"),
));
return;
} else {
setState(() {
loading = true;
});
sendData();
}
}
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
key: globalKey,
body: Background(
child: Form(child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"SIGNUP",
style: TextStyle(fontWeight: FontWeight.bold),
),
SizedBox(height: size.height * 0.03),
SvgPicture.asset(
"assets/icons/signup.svg",
height: size.height * 0.35,
),
RoundedInputField(
hintText: "Your Email",
onChanged: email,
),
RoundedPasswordField(
onChanged: password,
),
// TextFieldContainer(
// child: TextField(
// obscureText: obsecureText,
// // onChanged: onChanged,
// controller: password,
// cursorColor: kPrimaryColor,
// decoration: InputDecoration(
// hintText: "Password",
// icon: Icon(
// Icons.lock,
// color: kPrimaryColor,
// ),
// suffixIcon:
// InkWell(
// onTap: togglePAsswordView,
// child: Icon(
// Icons.visibility,
// color: kPrimaryColor,
// ),
// ),
// border: InputBorder.none,
// ),
// ),
// ),
RoundedButton(
text: "SIGNUP",
press: () {
validation();
},
),
SizedBox(height: size.height * 0.03),
AlreadyHaveAnAccountCheck(
login: false,
press: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return LoginScreen();
},
),
);
},
),
OrDivider(),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SocalIcon(
iconSrc: "assets/icons/facebook.svg",
press: () {},
),
SocalIcon(
iconSrc: "assets/icons/twitter.svg",
press: () {},
),
SocalIcon(
iconSrc: "assets/icons/google-plus.svg",
press: () {},
),
],
)
],
),
),
),
),
);
}
}