added login

This commit is contained in:
2026-08-07 17:07:49 +05:30
parent ad44402232
commit 4a2474ee6c
8 changed files with 908 additions and 383 deletions

View File

@@ -1,5 +1,3 @@
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
@@ -10,12 +8,11 @@ import '../../../core/constants/asset_paths.dart';
import '../../../core/router/app_router.dart';
import '../../../core/theme/app_colors.dart';
import '../../../core/theme/app_dimens.dart';
import '../../../core/utils/validators.dart';
import '../../../core/widgets/brand_mark.dart';
import '../../../core/widgets/primary_button.dart';
import '../providers/auth_controller.dart';
/// Store sign-in. The terminal shows this until a valid account is entered.
/// Store sign-in. The terminal shows this until the back office issues a
/// session.
///
/// One centred card on a plain background, at every width. The split-screen
/// version put a marketing panel beside the form, which meant the thing the
@@ -23,6 +20,11 @@ import '../providers/auth_controller.dart';
/// different width on every monitor, and collapsed into a different layout
/// below 1000px. A till is signed into at the start of a shift by someone who
/// already bought the product; the pitch was costing the form its position.
///
/// There is no role picker. The role comes back in the login response and is
/// the server's to decide — a tab on this screen would only ever have been a
/// hint, and a hint that disagreed with the response would be a bug someone
/// spends an afternoon on.
class LoginScreen extends ConsumerStatefulWidget {
const LoginScreen({super.key});
@@ -33,30 +35,15 @@ class LoginScreen extends ConsumerStatefulWidget {
class _LoginScreenState extends ConsumerState<LoginScreen> {
final _formKey = GlobalKey<FormState>();
/// Which of the two default accounts the tabs are pointing at. Only a
/// convenience for filling the fields — [AuthController.signIn] decides the
/// role from the email that is actually submitted, so typing a different
/// address over the top still signs in as that account.
TerminalLogin _login = TerminalLogin.admin;
late final _email = TextEditingController(text: _login.email);
late final _password = TextEditingController(text: _login.password);
final _authname = TextEditingController();
final _password = TextEditingController();
bool _obscure = true;
bool _rememberTerminal = true;
void _selectLogin(TerminalLogin login) {
setState(() {
_login = login;
_email.text = login.email;
_password.text = login.password;
});
ref.read(authControllerProvider.notifier).clearError();
}
@override
void dispose() {
_email.dispose();
_authname.dispose();
_password.dispose();
super.dispose();
}
@@ -66,11 +53,15 @@ class _LoginScreenState extends ConsumerState<LoginScreen> {
if (!(_formKey.currentState?.validate() ?? false)) return;
final ok = await ref.read(authControllerProvider.notifier).signIn(
email: _email.text,
authname: _authname.text,
password: _password.text,
);
if (ok && mounted) context.go(AppRoutes.pos);
if (!ok || !mounted) return;
// Admin accounts land on the full shell, cashiers on the till. Read from
// the session that was just opened rather than from anything typed here.
context.go(AppRoutes.homeFor(ref.read(terminalLoginProvider)));
}
@override
@@ -109,21 +100,18 @@ class _LoginScreenState extends ConsumerState<LoginScreen> {
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// _Masthead(tight: tight),
SizedBox(
height: tight ? AppSpacing.lg : AppSpacing.xl,
),
_Card(
formKey: _formKey,
email: _email,
authname: _authname,
password: _password,
obscure: _obscure,
rememberTerminal: _rememberTerminal,
login: _login,
busy: busy,
failure:
auth is AuthFailure ? auth.message : null,
onSelectLogin: _selectLogin,
onToggleObscure: () =>
setState(() => _obscure = !_obscure),
onToggleRemember: (v) =>
@@ -157,11 +145,10 @@ class _LoginScreenState extends ConsumerState<LoginScreen> {
/// The shopfront behind the form.
///
/// Blurred hard and darkened, because it is atmosphere rather than something
/// to read: a sharp photograph under a sign-in card competes with the two
/// fields the person came here to fill in. Scaled up slightly before the blur
/// so the softened edges are pushed off-screen instead of showing as a pale
/// border.
/// Darkened, because it is atmosphere rather than something to read: a sharp
/// photograph under a sign-in card competes with the two fields the person came
/// here to fill in. Scaled up slightly so the edges are pushed off-screen
/// instead of showing as a pale border.
///
/// Falls back to the plain background colour if the asset is missing, so an
/// undeclared file costs the login screen its atmosphere and not its function.
@@ -174,24 +161,21 @@ class _Backdrop extends StatelessWidget {
fit: StackFit.expand,
children: [
ClipRect(
child: ImageFiltered(
imageFilter: ui.ImageFilter.blur(sigmaX: 0, sigmaY: 0),
child: Transform.scale(
scale: 1.12,
child: Image.asset(
AssetPaths.loginBackground,
fit: BoxFit.cover,
filterQuality: FilterQuality.medium,
errorBuilder: (context, _, __) =>
const ColoredBox(color: AppColors.background),
),
child: Transform.scale(
scale: 1.12,
child: Image.asset(
AssetPaths.loginBackground,
fit: BoxFit.cover,
filterQuality: FilterQuality.medium,
errorBuilder: (context, _, __) =>
const ColoredBox(color: AppColors.background),
),
),
),
// Two layers, not one: the flat wash guarantees contrast for the white
// masthead wherever the photograph happens to be pale, and the gradient
// puts the darkest part behind the text rather than spreading it evenly
// and flattening the image out.
// Two layers, not one: the flat wash guarantees contrast wherever the
// photograph happens to be pale, and the gradient puts the darkest part
// behind the card rather than spreading it evenly and flattening the
// image out.
const ColoredBox(color: Color(0x8A1A0B22)),
const DecoratedBox(
decoration: BoxDecoration(
@@ -207,74 +191,27 @@ class _Backdrop extends StatelessWidget {
}
}
/// Mark and product name, directly above the card.
class _Masthead extends StatelessWidget {
const _Masthead({required this.tight});
final bool tight;
@override
Widget build(BuildContext context) {
return Column(
children: [
BrandMark(size: tight ? 48 : 60),
const SizedBox(height: AppSpacing.md),
const Text(
'Nearle POS',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.w700,
letterSpacing: -0.4,
color: Colors.white,
height: 1.2,
shadows: [
Shadow(color: Color(0x66000000), blurRadius: 12),
],
),
),
if (!tight) ...[
const SizedBox(height: 2),
Text(
'Scanner-first billing for Indian retail',
style: TextStyle(
fontSize: 13,
color: Colors.white.withValues(alpha: 0.82),
shadows: const [
Shadow(color: Color(0x55000000), blurRadius: 10),
],
),
),
],
],
).animate().fadeIn(duration: 260.ms);
}
}
class _Card extends StatelessWidget {
const _Card({
required this.formKey,
required this.email,
required this.authname,
required this.password,
required this.obscure,
required this.rememberTerminal,
required this.login,
required this.busy,
required this.failure,
required this.onSelectLogin,
required this.onToggleObscure,
required this.onToggleRemember,
required this.onSubmit,
});
final GlobalKey<FormState> formKey;
final TextEditingController email;
final TextEditingController authname;
final TextEditingController password;
final bool obscure;
final bool rememberTerminal;
final TerminalLogin login;
final bool busy;
final String? failure;
final ValueChanged<TerminalLogin> onSelectLogin;
final VoidCallback onToggleObscure;
final ValueChanged<bool?> onToggleRemember;
final VoidCallback onSubmit;
@@ -313,7 +250,8 @@ class _Card extends StatelessWidget {
),
const SizedBox(height: 2),
const Text(
'Use the credentials issued when your outlet was registered.',
'Use the terminal account issued when your outlet was '
'registered.',
style: TextStyle(
fontSize: 13,
color: AppColors.textSecondary,
@@ -322,33 +260,21 @@ class _Card extends StatelessWidget {
),
const SizedBox(height: AppSpacing.xl),
_RoleSwitch(
selected: login,
enabled: !busy,
onSelect: onSelectLogin,
),
const SizedBox(height: AppSpacing.sm),
Text(
login.blurb,
style: const TextStyle(
fontSize: 12.5,
color: AppColors.textTertiary,
height: 1.45,
),
),
const SizedBox(height: AppSpacing.xl),
const _Label('Store email'),
const _Label('Terminal account'),
TextFormField(
controller: email,
controller: authname,
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.next,
enabled: !busy,
autocorrect: false,
// Deliberately not validated as an email address. It looks like
// one, but it is an account name the back office issues and its
// shape is theirs to change.
validator: (v) => (v ?? '').trim().isEmpty
? 'Store email is required'
: Validators.emailOptional(v),
? 'The terminal account is required'
: null,
decoration: const InputDecoration(
hintText: 'store@example.in',
hintText: 'supervisor.1135@pos.nearle.in',
prefixIcon: Icon(Icons.storefront_outlined),
),
),
@@ -361,9 +287,11 @@ class _Card extends StatelessWidget {
enabled: !busy,
textInputAction: TextInputAction.done,
onFieldSubmitted: (_) => onSubmit(),
validator: (v) => (v ?? '').isEmpty
? 'Password is required'
: ((v ?? '').length < 6 ? 'Password looks too short' : null),
// Length is the server's rule to enforce. Refusing to *send* a
// short password only produces a second, different error message
// for the same wrong credential.
validator: (v) =>
(v ?? '').isEmpty ? 'Password is required' : null,
decoration: InputDecoration(
hintText: 'Enter your password',
prefixIcon: const Icon(Icons.lock_outline_rounded),
@@ -432,8 +360,11 @@ class _Card extends StatelessWidget {
),
child: Row(
children: [
const Icon(Icons.error_outline_rounded,
color: AppColors.danger, size: 18,),
const Icon(
Icons.error_outline_rounded,
color: AppColors.danger,
size: 18,
),
const SizedBox(width: AppSpacing.sm),
Expanded(
child: Text(
@@ -457,12 +388,6 @@ class _Card extends StatelessWidget {
busy: busy,
onPressed: onSubmit,
),
const SizedBox(height: AppSpacing.lg),
_DemoHint(
login: login,
onFill: busy ? null : () => onSelectLogin(login),
),
],
),
),
@@ -490,168 +415,3 @@ class _Label extends StatelessWidget {
);
}
}
/// Which of the two default accounts is being signed into.
///
/// The roles are not cosmetic — they decide whether the terminal opens the
/// full shell or the billing screen alone, and whether signing out leaves the
/// products behind — so the choice is made before the credentials rather than
/// inferred from them afterwards.
class _RoleSwitch extends StatelessWidget {
const _RoleSwitch({
required this.selected,
required this.enabled,
required this.onSelect,
});
final TerminalLogin selected;
final bool enabled;
final ValueChanged<TerminalLogin> onSelect;
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(4),
decoration: BoxDecoration(
color: AppColors.surfaceAlt,
borderRadius: AppRadius.brSm,
border: Border.all(color: AppColors.border),
),
child: Row(
children: [
for (final login in TerminalLogin.values)
Expanded(
child: _RoleTab(
login: login,
selected: login == selected,
enabled: enabled,
onTap: () => onSelect(login),
),
),
],
),
);
}
}
class _RoleTab extends StatelessWidget {
const _RoleTab({
required this.login,
required this.selected,
required this.enabled,
required this.onTap,
});
final TerminalLogin login;
final bool selected;
final bool enabled;
final VoidCallback onTap;
@override
Widget build(BuildContext context) {
final icon = login == TerminalLogin.admin
? Icons.admin_panel_settings_outlined
: Icons.point_of_sale_rounded;
return Material(
color: Colors.transparent,
child: InkWell(
onTap: enabled ? onTap : null,
borderRadius: AppRadius.brXs,
child: AnimatedContainer(
duration: const Duration(milliseconds: 160),
height: 40,
alignment: Alignment.center,
decoration: BoxDecoration(
color: selected ? AppColors.surface : Colors.transparent,
borderRadius: AppRadius.brXs,
border: Border.all(
color: selected ? AppColors.primaryBorder : Colors.transparent,
),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
icon,
size: 17,
color: selected ? AppColors.primary : AppColors.textSecondary,
),
const SizedBox(width: AppSpacing.sm),
Flexible(
child: Text(
login.label,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 13.5,
fontWeight: selected ? FontWeight.w700 : FontWeight.w500,
color:
selected ? AppColors.primary : AppColors.textSecondary,
),
),
),
],
),
),
),
);
}
}
class _DemoHint extends StatelessWidget {
const _DemoHint({required this.login, this.onFill});
final TerminalLogin login;
final VoidCallback? onFill;
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(AppSpacing.md),
decoration: BoxDecoration(
color: AppColors.primarySurface,
borderRadius: AppRadius.brSm,
border: Border.all(color: AppColors.primaryBorder),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Icon(Icons.info_outline_rounded,
size: 17, color: AppColors.primary,),
const SizedBox(width: AppSpacing.sm),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Default ${login.label.toLowerCase()} account',
style: const TextStyle(
fontSize: 12.5,
fontWeight: FontWeight.w600,
color: AppColors.primary,
),
),
const SizedBox(height: 2),
SelectableText(
'${login.email} \u00b7 ${login.password}',
style: const TextStyle(
fontSize: 12,
color: AppColors.textSecondary,
),
),
],
),
),
TextButton(
onPressed: onFill,
style: TextButton.styleFrom(
minimumSize: const Size(0, 32),
padding: const EdgeInsets.symmetric(horizontal: AppSpacing.sm),
),
child: const Text('Fill', style: TextStyle(fontSize: 12.5)),
),
],
),
);
}
}