- Multi-cycle BLE scanning (5 cycles x 2 seconds) overcomes Android detection limits - RSSI averaging and variance calculation for confident beacon selection - Detects all 3 test beacons with 100% accuracy - Login flow optimized: beacon scan → browse menu → login on cart add - Anonymous users can browse full menu before authentication - Beacon scanning now occurs before login requirement Technical improvements: - Added API endpoints for beacon listing and business mapping - Updated AppState to handle business/service point selection - Implemented intelligent beacon scoring with proximity ranking - Added graceful fallbacks for no-beacon scenarios 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
195 lines
6.5 KiB
Dart
195 lines
6.5 KiB
Dart
import "package:flutter/material.dart";
|
|
import "package:provider/provider.dart";
|
|
|
|
import "../app/app_router.dart";
|
|
import "../app/app_state.dart";
|
|
import "../services/api.dart";
|
|
|
|
class LoginScreen extends StatefulWidget {
|
|
const LoginScreen({super.key});
|
|
|
|
@override
|
|
State<LoginScreen> createState() => _LoginScreenState();
|
|
}
|
|
|
|
class _LoginScreenState extends State<LoginScreen> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
final _usernameController = TextEditingController();
|
|
final _passwordController = TextEditingController();
|
|
|
|
bool _isLoading = false;
|
|
String? _errorMessage;
|
|
|
|
@override
|
|
void dispose() {
|
|
_usernameController.dispose();
|
|
_passwordController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _handleLogin() async {
|
|
if (!_formKey.currentState!.validate()) {
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
_isLoading = true;
|
|
_errorMessage = null;
|
|
});
|
|
|
|
try {
|
|
final result = await Api.login(
|
|
username: _usernameController.text.trim(),
|
|
password: _passwordController.text,
|
|
);
|
|
|
|
if (!mounted) return;
|
|
|
|
final appState = context.read<AppState>();
|
|
appState.setUserId(result.userId);
|
|
|
|
// Go back to previous screen (menu) or beacon scan if no previous route
|
|
if (Navigator.of(context).canPop()) {
|
|
Navigator.of(context).pop();
|
|
} else {
|
|
Navigator.of(context).pushReplacementNamed(AppRoutes.beaconScan);
|
|
}
|
|
} catch (e) {
|
|
if (!mounted) return;
|
|
|
|
setState(() {
|
|
_errorMessage = e.toString();
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text("Login"),
|
|
),
|
|
body: Center(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(24),
|
|
child: ConstrainedBox(
|
|
constraints: const BoxConstraints(maxWidth: 400),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
const Text(
|
|
"PAYFRIT",
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(
|
|
fontSize: 32,
|
|
fontWeight: FontWeight.bold,
|
|
letterSpacing: 2,
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
const Text(
|
|
"Sign in to order",
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
color: Colors.grey,
|
|
),
|
|
),
|
|
const SizedBox(height: 48),
|
|
TextFormField(
|
|
controller: _usernameController,
|
|
decoration: const InputDecoration(
|
|
labelText: "Email or Phone Number",
|
|
border: OutlineInputBorder(),
|
|
prefixIcon: Icon(Icons.person),
|
|
),
|
|
keyboardType: TextInputType.emailAddress,
|
|
textInputAction: TextInputAction.next,
|
|
enabled: !_isLoading,
|
|
validator: (value) {
|
|
if (value == null || value.trim().isEmpty) {
|
|
return "Please enter your email or phone number";
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextFormField(
|
|
controller: _passwordController,
|
|
decoration: const InputDecoration(
|
|
labelText: "Password",
|
|
border: OutlineInputBorder(),
|
|
prefixIcon: Icon(Icons.lock),
|
|
),
|
|
obscureText: true,
|
|
textInputAction: TextInputAction.done,
|
|
enabled: !_isLoading,
|
|
onFieldSubmitted: (_) => _handleLogin(),
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return "Please enter your password";
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 24),
|
|
if (_errorMessage != null)
|
|
Container(
|
|
padding: const EdgeInsets.all(12),
|
|
margin: const EdgeInsets.only(bottom: 16),
|
|
decoration: BoxDecoration(
|
|
color: Colors.red.shade50,
|
|
borderRadius: BorderRadius.circular(8),
|
|
border: Border.all(color: Colors.red.shade300),
|
|
),
|
|
child: Row(
|
|
children: [
|
|
Icon(Icons.error_outline, color: Colors.red.shade700),
|
|
const SizedBox(width: 12),
|
|
Expanded(
|
|
child: Text(
|
|
_errorMessage!,
|
|
style: TextStyle(color: Colors.red.shade900),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
FilledButton(
|
|
onPressed: _isLoading ? null : _handleLogin,
|
|
child: _isLoading
|
|
? const SizedBox(
|
|
height: 20,
|
|
width: 20,
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2,
|
|
valueColor:
|
|
AlwaysStoppedAnimation<Color>(Colors.white),
|
|
),
|
|
)
|
|
: const Text("Login"),
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextButton(
|
|
onPressed: _isLoading ? null : () {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text("Registration not yet implemented"),
|
|
),
|
|
);
|
|
},
|
|
child: const Text("Don't have an account? Register"),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|