245 lines
8.9 KiB
Dart
245 lines
8.9 KiB
Dart
import 'dart:developer' as developer;
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
import '../../../app/app_services.dart';
|
|
import '../../../app/router/app_router.dart';
|
|
import '../../../core/widgets/app_button.dart';
|
|
import '../../../domain/repositories/auth_repository.dart';
|
|
|
|
class LoginScreen extends StatefulWidget {
|
|
const LoginScreen({required this.services, super.key});
|
|
|
|
final AppServices services;
|
|
|
|
@override
|
|
State<LoginScreen> createState() => _LoginScreenState();
|
|
}
|
|
|
|
class _LoginScreenState extends State<LoginScreen> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
final _usernameController = TextEditingController();
|
|
final _passwordController = TextEditingController();
|
|
bool _obscurePassword = true;
|
|
bool _submitting = false;
|
|
late final Future<String> _appVersion;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_appVersion = widget.services.appVersionProvider.getVersion();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_usernameController.dispose();
|
|
_passwordController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _submit() async {
|
|
if (_submitting || !_formKey.currentState!.validate()) return;
|
|
setState(() => _submitting = true);
|
|
|
|
try {
|
|
final settings = await widget.services.settingsRepository.load();
|
|
if (settings?.isConfigured != true) {
|
|
_showMessage('Configure o servidor pela engrenagem antes de entrar.');
|
|
return;
|
|
}
|
|
|
|
final device = await widget.services.posDeviceProvider.getDevice();
|
|
final appVersion = await widget.services.appVersionProvider.getVersion();
|
|
await widget.services.authRepository.authenticate(
|
|
username: _usernameController.text.trim(),
|
|
password: _passwordController.text,
|
|
settings: settings!,
|
|
device: device,
|
|
appVersion: appVersion,
|
|
);
|
|
if (mounted) {
|
|
Navigator.of(context).pushNamedAndRemoveUntil(
|
|
AppRoutes.menu,
|
|
(route) => false,
|
|
);
|
|
}
|
|
} on UnsupportedError catch (error) {
|
|
developer.log(
|
|
'Login bloqueado pela plataforma do dispositivo.',
|
|
name: 'pdv.auth',
|
|
);
|
|
_showMessage(error.message.toString());
|
|
} on AuthUnavailableException catch (error) {
|
|
_showMessage(error.message);
|
|
} catch (error, stackTrace) {
|
|
developer.log(
|
|
'Falha ao preparar login do PDV: ${error.runtimeType}.',
|
|
name: 'pdv.auth',
|
|
stackTrace: stackTrace,
|
|
);
|
|
_showMessage(
|
|
'Não foi possível autenticar. Verifique os dados e tente novamente.');
|
|
} finally {
|
|
if (mounted) setState(() => _submitting = false);
|
|
}
|
|
}
|
|
|
|
Future<void> _exit() async {
|
|
await widget.services.authRepository.logout();
|
|
await SystemNavigator.pop();
|
|
}
|
|
|
|
void _openSettings() {
|
|
Navigator.of(context).pushNamed(AppRoutes.settings);
|
|
}
|
|
|
|
void _showMessage(String message) {
|
|
if (!mounted) return;
|
|
ScaffoldMessenger.of(context)
|
|
..hideCurrentSnackBar()
|
|
..showSnackBar(SnackBar(content: Text(message)));
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
automaticallyImplyLeading: false,
|
|
title: const Text('PDV'),
|
|
),
|
|
body: SafeArea(
|
|
child: Column(
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.only(left: 16, right: 8),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
FutureBuilder<String>(
|
|
future: _appVersion,
|
|
builder: (context, snapshot) {
|
|
if (!snapshot.hasData || snapshot.hasError) {
|
|
return const SizedBox.shrink();
|
|
}
|
|
return Text(
|
|
'Versão: ${snapshot.data}',
|
|
key: const Key('app-version'),
|
|
style: const TextStyle(
|
|
fontWeight: FontWeight.bold,
|
|
fontSize: 11,
|
|
),
|
|
);
|
|
},
|
|
),
|
|
IconButton(
|
|
key: const Key('settings-button'),
|
|
tooltip: 'Configuração',
|
|
onPressed: _submitting ? null : _openSettings,
|
|
icon: const Icon(Icons.settings_outlined),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Expanded(
|
|
child: Center(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(24),
|
|
child: ConstrainedBox(
|
|
constraints: const BoxConstraints(maxWidth: 440),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Icon(
|
|
Icons.point_of_sale_rounded,
|
|
size: 64,
|
|
color: Theme.of(context).colorScheme.primary,
|
|
),
|
|
const SizedBox(height: 20),
|
|
Text(
|
|
'Acesso ao PDV',
|
|
textAlign: TextAlign.center,
|
|
style: Theme.of(context)
|
|
.textTheme
|
|
.headlineSmall
|
|
?.copyWith(
|
|
fontWeight: FontWeight.w700,
|
|
),
|
|
),
|
|
const SizedBox(height: 28),
|
|
TextFormField(
|
|
key: const Key('login-username'),
|
|
controller: _usernameController,
|
|
enabled: !_submitting,
|
|
textInputAction: TextInputAction.next,
|
|
autocorrect: false,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Usuário',
|
|
prefixIcon: Icon(Icons.person_outline),
|
|
),
|
|
validator: (value) =>
|
|
value == null || value.trim().isEmpty
|
|
? 'Informe o usuário.'
|
|
: null,
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextFormField(
|
|
key: const Key('login-password'),
|
|
controller: _passwordController,
|
|
enabled: !_submitting,
|
|
obscureText: _obscurePassword,
|
|
textInputAction: TextInputAction.done,
|
|
onFieldSubmitted: (_) => _submit(),
|
|
decoration: InputDecoration(
|
|
labelText: 'Senha',
|
|
prefixIcon: const Icon(Icons.lock_outline),
|
|
suffixIcon: IconButton(
|
|
tooltip: _obscurePassword
|
|
? 'Mostrar senha'
|
|
: 'Ocultar senha',
|
|
onPressed: () => setState(
|
|
() => _obscurePassword = !_obscurePassword,
|
|
),
|
|
icon: Icon(
|
|
_obscurePassword
|
|
? Icons.visibility_outlined
|
|
: Icons.visibility_off_outlined,
|
|
),
|
|
),
|
|
),
|
|
validator: (value) => value == null || value.isEmpty
|
|
? 'Informe a senha.'
|
|
: null,
|
|
),
|
|
const SizedBox(height: 24),
|
|
AppButton(
|
|
label: 'Entrar',
|
|
icon: Icons.login,
|
|
onPressed: _submitting ? null : _submit,
|
|
),
|
|
if (_submitting) ...[
|
|
const SizedBox(height: 16),
|
|
const Center(child: CircularProgressIndicator()),
|
|
],
|
|
const SizedBox(height: 12),
|
|
TextButton.icon(
|
|
onPressed: _submitting ? null : _exit,
|
|
icon: const Icon(Icons.exit_to_app),
|
|
label: const Text('Sair'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|