194 lines
7.6 KiB
Dart

import 'package:baseproject/core/common/index.dart';
import 'package:baseproject/core/components/index.dart';
import 'package:baseproject/features/presentation/account/bloc/login_bloc.dart';
import 'package:baseproject/features/repositories/hra_repository.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class LoginScreen extends StatefulWidget {
const LoginScreen({Key? key}) : super(key: key);
@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final GlobalKey<FormBuilderState> _formKey = GlobalKey<FormBuilderState>();
bool _rememberMe = false;
final LoginBloc _loginBloc = getItSuper<LoginBloc>();
Future<void> _onSubmit() async {
final formState = _formKey.currentState;
if (formState == null) return;
if (!formState.saveAndValidate()) {
return;
}
final value = formState.value;
final dto = LoginDto(
userName: value['userName'] as String?,
password: value['password'] as String?,
rememberMe: _rememberMe,
);
final captcha = _loginBloc.state.model.captcha;
dto.captchaText = "999999"; //captcha?.dntCaptchaTextValue;
dto.captchaToken = captcha?.dntCaptchaTokenValue;
dto.captchaInputText = "999999";
_loginBloc.login(dto);
}
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => _loginBloc,
child: Scaffold(
appBar: AppBar(
title: const Text('Đăng nhập'),
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(16),
child: FormBuilder(
key: _formKey,
autovalidateMode: AutovalidateMode.onUserInteraction,
child: BlocBuilder<LoginBloc, BaseStateBloc<LoginViewModel>>(
builder: (context, state) {
final vm = state.model;
final isLoading = state is LoadingState<LoginViewModel> || vm.isLoading;
final error = vm.errorMessage;
final captcha = vm.captcha;
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
if (error != null)
Padding(
padding: const EdgeInsets.only(bottom: 12),
child: Text(
error,
style: const TextStyle(color: Colors.red),
),
),
FormControl(
labelText: 'Tên đăng nhập',
isShowTextRequire: true,
child: TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Nhập tên đăng nhập',
),
validator: FormBuilderValidators.required<String>(
context,
),
onSaved: (value) {
_formKey.currentState?.setInternalFieldValue(
'userName',
value,
isUpdateState: false,
);
},
),
),
const SizedBox(height: 16),
FormControl(
labelText: 'Mật khẩu',
isShowTextRequire: true,
child: TextFormField(
obscureText: true,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Nhập mật khẩu',
),
validator: FormBuilderValidators.required<String>(
context,
),
onSaved: (value) {
_formKey.currentState?.setInternalFieldValue(
'password',
value,
isUpdateState: false,
);
},
),
),
// const SizedBox(height: 8),
// if (captcha != null) ...[
// const SizedBox(height: 16),
// GestureDetector(
// onTap: () {
// _loginBloc.loadCaptcha();
// },
// child: Column(
// crossAxisAlignment: CrossAxisAlignment.start,
// children: [
// const Text('Captcha'),
// const SizedBox(height: 8),
// Image.network(
// captcha.dntCaptchaImgUrl ?? '',
// height: 60,
// errorBuilder: (_, __, ___) => Text(
// captcha.dntCaptchaImgUrl ?? '',
// style: const TextStyle(color: Colors.blue),
// ),
// ),
// ],
// ),
// ),
// const SizedBox(height: 8),
// FormControl(
// labelText: 'Nhập mã captcha',
// isShowTextRequire: true,
// child: TextFormField(
// decoration: const InputDecoration(
// border: OutlineInputBorder(),
// hintText: 'Nhập mã captcha',
// ),
// validator: FormBuilderValidators.required<String>(
// context,
// ),
// onSaved: (value) {
// _formKey.currentState?.setInternalFieldValue(
// 'captchaInput',
// value,
// isUpdateState: false,
// );
// },
// ),
// ),
// ],
const SizedBox(height: 8),
Row(
children: <Widget>[
Checkbox(
value: _rememberMe,
onChanged: (v) {
setState(() {
_rememberMe = v ?? false;
});
},
),
const Text('Ghi nhớ đăng nhập'),
],
),
const SizedBox(height: 24),
SizedBox(
height: 48,
child: ConstantWidget.buildPrimaryButton(
onPressed: isLoading ? null : _onSubmit,
text: 'Đăng nhập',
),
),
],
);
},
),
),
),
),
),
);
}
}