import 'package:cloud_firestore/cloud_firestore.
dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/[Link]';
import 'package:flutter_music1/core/models/[Link]';
import 'package:flutter_music1/core/routes/[Link]';
import
'package:flutter_music1/core/sevices/firebase_messaging/firebase_messaging.dart';
import 'package:flutter_music1/core/utility/[Link]';
import 'package:google_fonts/google_fonts.dart';
import '../../../widgets/shared/custum_colors.dart';
import '../../../widgets/shared/form_auth.dart';
import '../../../widgets/shared/[Link]';
class Signup extends StatefulWidget {
const Signup({[Link]});
@override
State<Signup> createState() => _SignupState();
}
class _SignupState extends State<Signup> {
final formKey = GlobalKey<FormState>();
final FirebaseAuth _auth = [Link];
final FirebaseFirestore _fireStore = [Link];
final FirebaseMessagingService firebaseMessagingService =
FirebaseMessagingService();
final bool _isProcessing = false;
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
final _nameController = TextEditingController();
final _confirmPasswordController = TextEditingController();
String mToken = '';
@override
void dispose() {
_emailController.dispose();
_passwordController.dispose();
_nameController.dispose();
_confirmPasswordController.dispose();
[Link]();
}
void _trySubmitForm(BuildContext context) {
final bool? isValid = [Link]?.validate();
if (isValid == true) {
debugPrint('Everything looks good!');
_createUserWithEmailAndPassword(context);
}
}
void _createUserWithEmailAndPassword(BuildContext context) async {
try {
final String email = _emailController.[Link]();
final String password = _passwordController.[Link]();
final String fullName = _nameController.[Link]();
final String? token = await [Link]();
// Create the user using Firebase Authentication
UserCredential userCredential = await
_auth.createUserWithEmailAndPassword(email: email, password: password,);
// I can save the email and the password with Firebase Authentication.
// I also save this information with Cloud Firestore.
// But how can I add and save the displayName after registering
await [Link]?.updateDisplayName(fullName);
// Store additional user data in Cloud FireStore
await _fireStore
.collection('users')
.doc([Link]!.uid)
.set(UserModel(
id: [Link]!.uid,
displayName: fullName,
email: email,
token: token,
isAdmin: false,
).toJson())
.whenComplete(() {
debugPrint('User created successfully!');
snackBarInfo(context: context, message: "User created successfully!");
[Link](context, [Link]); //??
});/*.onError((error, stackTrace) {
snackBarInfo(
context: context,
message: "Error creating user: $error \n stacktrace : $stackTrace");
});*/
} catch (e) {
// Handle any errors that occurred during registration
snackBarInfo(context: context, message: "Error creating user: $e");
}
}
bool _isSecurePassword = true;
bool _isSecureconfirmPassword = true;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: [Link][200],
body: Center(
child: SingleChildScrollView(
child: Column(
children: [
Padding(
padding: const [Link](vertical: 25),
child: Icon(
color: [Link].shade800,
Icons.verified_user_rounded,
size: 100,
),
),
Text(
'Register',
style: [Link](fontSize: 40,color:
[Link].shade800,),
),
const SizedBox(
height: 20,
),
Form(
key: formKey,
child: Column(
mainAxisAlignment: [Link],
children: [
CustomAuthFormField(
icon: const Icon([Link]),
controller: _nameController,
hint: "full name",
validate: "other",
validator: (String) {}, /* obscureText: false*/
),
CustomAuthFormField(
icon: const Icon([Link]),
controller: _emailController,
hint: "email",
validate: "email",
validator: (String) {},
),
Padding(
padding: const [Link](
horizontal: 20, vertical: 10),
child: TextFormField(
cursorColor: [Link],
controller: _passwordController,
validator: "password" != null
? "password" == "email"
? (value) => [Link](
value: _confirmPasswordController.text)
: "password" == "password"
? (value) => [Link](
_confirmPasswordController.text)
: "password" == "matchPassword"
? (value) =>
[Link](
_confirmPasswordController
.text,
_passwordController.text)
: (value) => [Link](
value: _confirmPasswordController
.text)
: null,
decoration: InputDecoration(
errorStyle: const TextStyle(
color: [Link],
),
prefixIcon: const Icon(
[Link],
),
suffixIcon: togglePassword(),
hintText: "password",
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(color: [Link]),
borderRadius: [Link](10),
),
focusedBorder: OutlineInputBorder(
borderSide:
const BorderSide(color: [Link]),
borderRadius: [Link](12),
),
prefixIconColor: [Link](
(states) =>
[Link]([Link])
? [Link]
: Colors.black54),
suffixIconColor: [Link](
(states) =>
[Link]([Link])
? [Link]
: Colors.black54),
filled: true,
errorBorder: OutlineInputBorder(
borderRadius: [Link](8.0),
borderSide: const BorderSide(
color: [Link],
width: 1,
),
),
focusedErrorBorder: OutlineInputBorder(
borderRadius: [Link](8.0),
borderSide: const BorderSide(
color: [Link],
width: 1,
),
),
),
obscureText: _isSecurePassword,
),
),
Padding(
padding: const [Link](horizontal: 20,
vertical: 10),
child: TextFormField(
controller: _confirmPasswordController,
validator: "matchPassword" != null
? "matchPassword" == "email"
? (value) => [Link](
value: _confirmPasswordController.text)
: "matchPassword" == "password"
? (value) => [Link](
_confirmPasswordController.text)
: "matchPassword" == "matchPassword"
? (value) =>
[Link](
_confirmPasswordController
.text,
_passwordController.text)
: (value) => [Link](
value: _confirmPasswordController
.text)
: null,
decoration: InputDecoration(
errorStyle: const TextStyle(
color: [Link],
),
prefixIcon: const Icon(
[Link],
),
suffixIcon: toggleconfirmPassword(),
hintText: "confirm password",
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(color: [Link]),
borderRadius: [Link](10),
),
focusedBorder: OutlineInputBorder(
borderSide:
const BorderSide(color: [Link]),
borderRadius: [Link](12),
),
prefixIconColor: [Link](
(states) =>
[Link]([Link])
? [Link]
: Colors.black54),
suffixIconColor: [Link](
(states) =>
[Link]([Link])
? [Link]
: Colors.black54),
filled: true,
errorBorder: OutlineInputBorder(
borderRadius: [Link](8.0),
borderSide: const BorderSide(
color: [Link],
width: 1,
),
),
focusedErrorBorder: OutlineInputBorder(
borderRadius: [Link](8.0),
borderSide: const BorderSide(
color: [Link],
width: 1,
),
),
),
obscureText: _isSecureconfirmPassword,
),
)
]),
),
// dropDownMenuProfileType(),
_isProcessing
? Padding(
padding: const [Link](8.0),
child: Center(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(
[Link],
),
),
),
)
: Padding(
padding:const [Link](horizontal: 20, vertical: 20),
child: GestureDetector(
onTap: () => _trySubmitForm(context),
child: Container(
padding: const [Link](20),
decoration: BoxDecoration(
color: [Link].shade400,
borderRadius: [Link](12)),
child: const Center(
child: Text(
'Sign Up',
style: TextStyle(
color: [Link],
fontWeight: [Link],
fontSize: 18,
),
),
),
),
),
),
],
),
),
),
);
}
Widget toggleconfirmPassword() {
return IconButton(
onPressed: () {
setState(() {
_isSecureconfirmPassword = !_isSecureconfirmPassword;
});
},
icon: _isSecureconfirmPassword
? const Icon(Icons.visibility_off)
: const Icon([Link])
);
}
Widget togglePassword() {
return IconButton(
onPressed: () {
setState(() {
_isSecurePassword = !_isSecurePassword;
});
},
icon: _isSecurePassword
? const Icon(Icons.visibility_off)
: const Icon([Link])
);
}