pcastlivetv/lib/pages/login_page.dart

161 lines
5.6 KiB
Dart

import 'package:flutter/material.dart';
import 'package:pcastlivetv/components/nc_form_field.dart';
import 'package:pcastlivetv/mixins/toast_mixin.dart';
import 'package:pcastlivetv/routes.dart';
import 'package:pcastlivetv/stores/login_store.dart';
import 'package:validatorless/validatorless.dart';
import 'package:velocity_x/velocity_x.dart';
class LoginPage extends StatefulWidget with ToastMixin {
const LoginPage({super.key});
@override
State<LoginPage> createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
@override
Widget build(BuildContext context) {
LoginStore store = VxState.store as LoginStore;
final formKey = GlobalKey<FormState>();
TextEditingController email = TextEditingController();
TextEditingController password = TextEditingController();
return Container(
color: const Color.fromRGBO(166, 0, 249, 1),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Card(
elevation: 20,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
side: const BorderSide(
width: 1,
color: Colors.black,
),
),
child: Column(
children: [
const SizedBox(
height: 32.0,
),
"Login".text.xl5.bold.make(),
const SizedBox(
height: 32.0,
),
SizedBox(
width: MediaQuery.of(context).size.width / 3,
child: Form(
key: formKey,
child: Padding(
padding: const EdgeInsets.only(
left: 64.0,
right: 64,
bottom: 32,
),
child: Column(
children: [
NCFormField(
label: 'Email',
controller: email,
validator: Validatorless.multiple(
[
Validatorless.email('Email inválido'),
Validatorless.required('Email é obrigatório'),
],
),
),
const SizedBox(
height: 16.0,
),
NCFormField(
label: 'Senha',
controller: password,
obscureText: true,
validator: Validatorless.required('Senha é obrigatória'),
),
const SizedBox(
height: 32.0,
),
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: const Color.fromRGBO(166, 0, 249, 1),
foregroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
onPressed: () async {
var result = await processAuth(formKey, store, email.text, password.text);
if (result) {
switch (store.userType) {
case 'A':
router.go('/admin');
break;
case 'R':
router.go('/reseller');
break;
case 'U':
router.go('/user');
break;
default:
}
} else {
widget.showToast(context, store.message!);
}
},
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Text('Entrar', style: TextStyle(fontSize: 24)),
),
),
],
),
),
),
),
],
),
),
],
),
),
);
}
Future<bool> processAuth(GlobalKey<FormState> formKey, LoginStore store, String email, String password) async {
Function? close;
if (formKey.currentState!.validate()) {
formKey.currentState!.save();
store.email = email;
store.password = password;
try {
close = context.showLoading(
msg: '',
bgColor: Colors.transparent,
textSize: 20,
);
await store.login();
close();
if (store.isLogged) {
return true;
}
if (store.message != "") {
return false;
}
} catch (e) {
close!();
widget.showToast(context, store.message!);
}
return false;
}
return false;
}
}