Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:plantual_1/ui/auth/signUpScreen.dart';
- import 'package:plantual_1/utils/utils.dart';
- import 'package:plantual_1/widgets/round_button.dart';
- import 'package:firebase_auth/firebase_auth.dart';
- import '../../scenes/home.dart';
- class signInScreen extends StatefulWidget {
- const signInScreen({super.key});
- @override
- State<signInScreen> createState() => _signInScreenState();
- }
- class _signInScreenState extends State<signInScreen> {
- bool loading = false;
- final _formKey = GlobalKey<FormState>();
- final emailController = TextEditingController();
- final passwordController = TextEditingController();
- final _auth = FirebaseAuth.instance;
- @override
- void dispose(){
- super.dispose();
- emailController.dispose();
- passwordController.dispose();
- }
- void login(){
- setState(() {
- loading = true;
- });
- _auth.signInWithEmailAndPassword(
- email: emailController.text.toString(),
- password: passwordController.text.toString()).then((value){
- utilsDone().toastMessage(value.user!.email.toString());
- Navigator.push(context,
- MaterialPageRoute(builder: (context) => Home())
- );
- setState(() {
- loading = false;
- });
- }).onError((error, stackTrace){
- if(error.toString() == '[firebase_auth/invalid-email] The email address is badly formatted.') {
- utilsError().toastMessage('Почта введена неправильно');
- };
- if(error.toString() == '[firebase_auth/invalid-credential] The supplied auth credential is incorrect, malformed or has expired.') {
- utilsError().toastMessage('Неверные данные для входа');
- };
- debugPrint(error.toString());
- setState(() {
- loading = false;
- });
- });
- }
- @override
- Widget build(BuildContext context) {
- return WillPopScope(
- onWillPop: ()async{
- SystemNavigator.pop();
- return true;
- },
- child: Scaffold(
- appBar: AppBar(
- backgroundColor: Colors.lightGreen,
- automaticallyImplyLeading: false,
- 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()){
- login();
- }
- },),
- const SizedBox(height: 10),
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- Text('Нет аккаунта?'),
- TextButton(
- onPressed: (){
- Navigator.push(context,
- MaterialPageRoute(builder: (context) => signUpScreen())
- );
- },
- child: Text('Зарегистрироваться'))
- ],
- )
- ],
- ),
- ),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement