import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
class RegisterPage extends StatefulWidget {
const RegisterPage({super.key});
@override
State<RegisterPage> createState() => _RegisterPageState();
}
class _RegisterPageState extends State<RegisterPage> {
// text controller
final userNameController = TextEditingController();
final ageController = TextEditingController();
final emailController = TextEditingController();
final passwordController = TextEditingController();
final confirmPasswordController = TextEditingController();
bool _obscureText = true;
final GlobalKey<FormState> _key = GlobalKey<FormState>();
final userCollection = FirebaseFirestore.instance.collection("Users");
@override
void dispose() {
emailController.dispose();
passwordController.dispose();
confirmPasswordController.dispose();
ageController.dispose();
userNameController.dispose();
super.dispose();
}
Future signUp() async {
if (passwordConfirmed()) {
try {
// Create user
await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: emailController.text.trim(),
password: passwordController.text.trim(),
);
// Sign in the user after registration
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: emailController.text.trim(),
password: passwordController.text.trim(),
);
// Add user details to Firestore
await userCollection.add({
"Kullanıcı Adı": userNameController.text.trim(),
"Yaş": ageController.text.trim(),
"Email": emailController.text.trim(),
"Şifre": passwordController.text.trim(),
});
// You may not need to navigate to the login page since the user is already signed in
} catch (error) {
// Handle errors, e.g., duplicate email or weak password
print("Error: $error");
}
}
}
bool passwordConfirmed() {
if (passwordController.text.trim() ==
confirmPasswordController.text.trim()) {
return true;
} else {
return false;
}
}
// User create
Future<void> UserModel(
{required String userName,
required String age,
required String email,
required String password}) async {
await userCollection.doc().set({
"Kullanıcı Adı": userName,
"Yaş": age,
"Email": email,
"Şifre": password,
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: SafeArea(
child: SingleChildScrollView(
child: Form(
key: _key,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Hello There",
style: GoogleFonts.bebasNeue(
fontSize: 52,
),
),
const Text(
"Register below with your details!",
style: TextStyle(
fontSize: 20,
),
),
const SizedBox(height: 20),
// username textfield
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: TextFormField(
controller: userNameController,
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(color: Colors.white),
borderRadius: BorderRadius.circular(12),
),
hintText: "Kullanıcı Adı",
fillColor: Colors.grey[200],
filled: true,
),
),
),
const SizedBox(height: 10),
// age textfield
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: TextFormField(
controller: ageController,
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(color: Colors.white),
borderRadius: BorderRadius.circular(12),
),
hintText: "Yaş",
fillColor: Colors.grey[200],
filled: true,
),
),
),
const SizedBox(height: 10),
// email textfield
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: TextFormField(
controller: emailController,
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(color: Colors.white),
borderRadius: BorderRadius.circular(12),
),
hintText: "E-mail",
fillColor: Colors.grey[200],
filled: true,
),
),
),
const SizedBox(height: 10),
// password textfield
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: TextFormField(
controller: passwordController,
obscureText: _obscureText,
decoration: InputDecoration(
suffixIcon: GestureDetector(
onTap: () {
setState(() {
_obscureText = !_obscureText;
});
},
child: Icon(_obscureText
? Icons.visibility
: Icons.visibility_off),
),
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(color: Colors.white),
borderRadius: BorderRadius.circular(12),
),
hintText: "Şifre",
fillColor: Colors.grey[200],
filled: true,
),
),
),
const SizedBox(height: 10),
// corfirm password textfield
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: TextFormField(
controller: confirmPasswordController,
obscureText: _obscureText,
decoration: InputDecoration(
suffixIcon: GestureDetector(
onTap: () {
setState(() {
_obscureText = !_obscureText;
});
},
child: Icon(_obscureText
? Icons.visibility
: Icons.visibility_off),
),
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(color: Colors.white),
borderRadius: BorderRadius.circular(12),
),
hintText: "Tekrar şifre",
fillColor: Colors.grey[200],
filled: true,
),
),
),
const SizedBox(height: 10),
// sing in button
Container(
padding: const EdgeInsets.only(left: 30, right: 30),
width: MediaQuery.of(context).size.width,
height: 50,
child: ElevatedButton(
style: ButtonStyle(
shape:
MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(35),
),
),
),
onPressed: () async {
if (_key.currentState!.validate()) {}
signUp();
setState(() {});
},
child: const Text(
'Sign In',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 18,
),
)),
),
],
),
),
),
),
);
}
}
kayıt olduğumda aynı ekranda kalıyor ama firebase kayıt bilgileri düşüyor geri tuşuna bastıgımda giriş yapmış olarak gözüküyor ama ben kayıt ola tıkladığımda giriş yapsın aynı ekranda kalmasın istiyorum sebebi ne olabilir yeni başladım kodlamaya