155 lines
5.0 KiB
Dart
155 lines
5.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
import 'package:fotodocumentation/controller/login_controller.dart';
|
|
import 'package:fotodocumentation/dto/jwt_token_pair_dto.dart';
|
|
import 'package:fotodocumentation/l10n/app_localizations.dart';
|
|
import 'package:fotodocumentation/pages/ui_utils/component/general_submit_widget.dart';
|
|
import 'package:fotodocumentation/pages/ui_utils/header_utils.dart';
|
|
import 'package:fotodocumentation/pages/ui_utils/modern_app_bar.dart';
|
|
import 'package:fotodocumentation/utils/di_container.dart';
|
|
import 'package:fotodocumentation/utils/login_credentials.dart';
|
|
|
|
class LoginWidget extends StatefulWidget {
|
|
const LoginWidget({super.key});
|
|
|
|
@override
|
|
State<LoginWidget> createState() => _LoginWidgetState();
|
|
}
|
|
|
|
class _LoginWidgetState extends State<LoginWidget> {
|
|
HeaderUtils get _headerUtils => DiContainer.get();
|
|
LoginController get _loginController => DiContainer.get();
|
|
LoginCredentials get _loginCredentials => DiContainer.get();
|
|
|
|
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
|
|
|
final _usernameController = TextEditingController();
|
|
final _passwordController = TextEditingController();
|
|
|
|
String? _error;
|
|
final FocusNode _focusNode = FocusNode();
|
|
|
|
@override
|
|
void dispose() {
|
|
_focusNode.dispose();
|
|
_usernameController.dispose();
|
|
_passwordController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: ModernAppBar(
|
|
title: _headerUtils.titleWidget("Login title"),
|
|
actions: [],
|
|
),
|
|
body: _body(context),
|
|
);
|
|
}
|
|
|
|
Widget _body(BuildContext context) {
|
|
return Container(
|
|
decoration: BoxDecoration(
|
|
gradient: LinearGradient(
|
|
begin: Alignment.topCenter,
|
|
end: Alignment.bottomCenter,
|
|
colors: [
|
|
Colors.grey[50]!,
|
|
Colors.white,
|
|
],
|
|
),
|
|
),
|
|
child: _content(context),
|
|
);
|
|
}
|
|
|
|
Widget _content(BuildContext context) {
|
|
return Padding(
|
|
padding: const EdgeInsets.all(20.0),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: KeyboardListener(
|
|
focusNode: _focusNode,
|
|
onKeyEvent: (event) {
|
|
if (event is KeyDownEvent && event.logicalKey == LogicalKeyboardKey.enter) {
|
|
_actionSubmit(context);
|
|
}
|
|
},
|
|
child: ListView(
|
|
children: [
|
|
Card(
|
|
elevation: 4,
|
|
margin: EdgeInsets.zero,
|
|
clipBehavior: Clip.antiAlias,
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(30.0),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
TextFormField(
|
|
key: Key("username"),
|
|
controller: _usernameController,
|
|
decoration: InputDecoration(
|
|
border: UnderlineInputBorder(),
|
|
labelText: AppLocalizations.of(context)!.loginUsernameTFLabel,
|
|
),
|
|
),
|
|
const SizedBox(height: 10),
|
|
TextFormField(
|
|
key: Key("password"),
|
|
controller: _passwordController,
|
|
obscureText: true,
|
|
decoration: InputDecoration(
|
|
border: UnderlineInputBorder(),
|
|
labelText: AppLocalizations.of(context)!.loginPasswordTFLabel,
|
|
),
|
|
),
|
|
const SizedBox(height: 10),
|
|
if (_error != null) ...[
|
|
Text(
|
|
_error!,
|
|
style: const TextStyle(color: Colors.red),
|
|
),
|
|
const SizedBox(height: 10),
|
|
],
|
|
GeneralSubmitWidget(
|
|
key: const Key("submit"),
|
|
onSelect: () async => await _actionSubmit(context),
|
|
title: AppLocalizations.of(context)!.loginLoginButtonLabel,
|
|
),
|
|
const SizedBox(height: 30),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _actionSubmit(BuildContext context) async {
|
|
String username = _usernameController.text;
|
|
String password = _passwordController.text;
|
|
|
|
AuthenticateReply authenticateReply = await _loginController.authenticate(username, password);
|
|
|
|
JwtTokenPairDto? jwtTokenPairDto = authenticateReply.jwtTokenPairDto;
|
|
if (jwtTokenPairDto == null) {
|
|
setState(() => _error = "Error message");
|
|
return;
|
|
}
|
|
|
|
_loginCredentials.setLoggedIn(true);
|
|
if (context.mounted) {
|
|
context.go("/");
|
|
}
|
|
}
|
|
}
|