Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:firebase_auth/firebase_auth.dart';
- import 'package:flutter/material.dart';
- import 'package:plantual_1/ui/auth/signInScreen.dart';
- import 'package:plantual_1/widgets/round_button.dart';
- import 'package:plantual_1/utils/utils.dart';
- import '../../scenes/home.dart';
- class signUpScreen extends StatefulWidget {
- const signUpScreen({super.key});
- @override
- State<signUpScreen> createState() => _signUpScreenState();
- }
- class _signUpScreenState extends State<signUpScreen> {
- bool loading = false;
- final _formKey = GlobalKey<FormState>();
- final emailController = TextEditingController();
- final passwordController = TextEditingController();
- FirebaseAuth _auth = FirebaseAuth.instance;
- @override
- void dispose(){
- super.dispose();
- emailController.dispose();
- passwordController.dispose();
- }
- void signUp(){
- setState(() {
- loading = true;
- });
- _auth.createUserWithEmailAndPassword(
- email: emailController.text.toString(),
- password: passwordController.text.toString()).then((value){
- setState(() {
- loading = false;
- });
- utilsDone().toastMessage('Аккаунт успешно создан');
- Navigator.push(context, MaterialPageRoute(builder: (context) => Home()));
- }).onError((error, stackTrace){
- if(error.toString() == '[firebase_auth/invalid-email] The email address is badly formatted.') {
- utilsError().toastMessage('Почта введена неправильно');
- };
- setState(() {
- loading = false;
- });
- });
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- backgroundColor: Colors.lightGreen,
- foregroundColor: Colors.white,
- centerTitle: true,
- title: Text('Регистрация', style: TextStyle(color: Colors.white)),
- ),
- body: Padding(
- padding: const EdgeInsets.symmetric(horizontal: 20),
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- crossAxisAlignment: CrossAxisAlignment.center,
- children: [
- Form(
- key: _formKey,
- child: Column(
- children: [
- TextFormField(
- keyboardType: TextInputType.emailAddress,
- controller: emailController,
- decoration: const InputDecoration(
- hintText: 'Почта',
- helperText: 'Введите эл.почту',
- prefixIcon: Icon(Icons.alternate_email)
- ),
- validator: (value) {
- if(value!.isEmpty){
- return 'Введите почту';
- }
- return null;
- },
- ),
- const SizedBox(height: 10),
- TextFormField(
- keyboardType: TextInputType.text,
- obscureText: true,
- controller: passwordController,
- decoration: const InputDecoration(
- hintText: 'Пароль',
- helperText: 'Введите пароль',
- prefixIcon: Icon(Icons.lock_open)
- ),
- validator: (value) {
- if(value!.isEmpty){
- return 'Введите пароль';
- }
- return null;
- },
- ),
- ],
- ),
- ),
- const SizedBox(height: 30),
- roundButton(
- title: 'Зарегистрироваться',
- loading: loading,
- onTap: () {
- if(_formKey.currentState!.validate()){
- signUp();
- }
- },),
- const SizedBox(height: 10),
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- Text('Уже есть аккаунт?'),
- TextButton(
- onPressed: (){
- Navigator.push(context,
- MaterialPageRoute(builder: (context) => signInScreen())
- );
- },
- child: Text('Войти'))
- ],
- )
- ],
- ),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement