You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
7.3 KiB
Dart
177 lines
7.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
import '../../controllers/http_status_manger/http_status_manger.dart';
|
|
import '../../controllers/localization/localization.dart';
|
|
import '../../controllers/providers/api/user_provider.dart';
|
|
import '../../controllers/providers/settings/setting_provider.dart';
|
|
import '../../controllers/validator/validator.dart';
|
|
import '../../models/subtitle.dart';
|
|
import '../../models/user.dart';
|
|
import '../app_style/sizing.dart';
|
|
import '../widgets/app_text_form_field.dart';
|
|
import '../widgets/buttons/app_button.dart';
|
|
import '../widgets/loaders/loading_manager.dart';
|
|
import 'user/land_page.dart';
|
|
|
|
class Login extends StatefulWidget {
|
|
static const String id = "/login";
|
|
|
|
const Login({super.key});
|
|
|
|
@override
|
|
_LoginState createState() => _LoginState();
|
|
}
|
|
|
|
class _LoginState extends State<Login> {
|
|
UserProvider? _userProvider;
|
|
SettingProvider? _settingProvider;
|
|
final User _user = User();
|
|
bool _obscurePassword = true;
|
|
final bool _firstTime = true;
|
|
late double _height;
|
|
late double _width;
|
|
late String _payload;
|
|
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
|
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
_userProvider = Provider.of<UserProvider>(context);
|
|
_settingProvider = Provider.of<SettingProvider>(context);
|
|
_height = MediaQuery.of(context).size.height;
|
|
_width = MediaQuery.of(context).size.width;
|
|
Subtitle? subtitle = AppLocalization.of(context)?.subtitle;
|
|
return Scaffold(
|
|
key: _scaffoldKey,
|
|
body: SafeArea(
|
|
child: LoadingManager(
|
|
isLoading: (_userProvider?.isLoading ?? false) ||
|
|
!(_settingProvider?.isLoaded ?? false),
|
|
isFailedLoading: false,
|
|
stateCode: 200,
|
|
onRefresh: () async {},
|
|
child: Form(
|
|
key: _formKey,
|
|
child: SingleChildScrollView(
|
|
//padding: EdgeInsets.symmetric(horizontal: 32),
|
|
child: Column(
|
|
children: [
|
|
//AppNameBar(),
|
|
SizedBox(
|
|
height: MediaQuery.of(context).size.height / 7,
|
|
),
|
|
Hero(
|
|
tag: "logo",
|
|
child: Image(
|
|
height: _height / 6,
|
|
fit: BoxFit.contain,
|
|
image: const AssetImage("assets/images/logo.png"),
|
|
),
|
|
),
|
|
Padding(
|
|
padding: EdgeInsets.symmetric(
|
|
horizontal: 24 * AppStyle.getScaleFactor(context),
|
|
vertical: 24 * AppStyle.getScaleFactor(context)),
|
|
child: Column(
|
|
children: [
|
|
SizedBox(
|
|
height: 24 * AppStyle.getScaleFactor(context),
|
|
),
|
|
ATextFormField(
|
|
initialValue: _user.userName ?? "",
|
|
hintText: subtitle?.name ?? "",
|
|
textAlign: TextAlign.left,
|
|
style: Theme.of(context).textTheme.bodyLarge,
|
|
prefixIconData: Icons.account_circle,
|
|
validator: (value) => Validator.hasValue(value ?? '')
|
|
? null
|
|
: subtitle?.nameValidateMessage,
|
|
textInputType: TextInputType.name,
|
|
onSaved: (value) {
|
|
_user.userName = value!;
|
|
},
|
|
),
|
|
const SizedBox(height: 12),
|
|
ATextFormField(
|
|
initialValue: _user.password,
|
|
hintText: subtitle?.password ?? "",
|
|
obscureText: _obscurePassword,
|
|
style: Theme.of(context).textTheme.bodyText1,
|
|
prefixIconData: Icons.vpn_key_sharp,
|
|
textAlign: TextAlign.left,
|
|
validator: (value) =>
|
|
Validator.isValidPassword(value ?? '')
|
|
? null
|
|
: subtitle?.passwordValidateMessage,
|
|
showPassword: () {
|
|
_obscurePassword = !_obscurePassword;
|
|
setState(() {});
|
|
},
|
|
onSaved: (value) {
|
|
_user.password = value!;
|
|
},
|
|
),
|
|
SizedBox(
|
|
height: 32 * AppStyle.getScaleFactor(context),
|
|
),
|
|
AButton(
|
|
text: subtitle?.signIn ?? "",
|
|
onPressed: () async {
|
|
if (!(_formKey.currentState?.validate() ?? true)) {
|
|
return;
|
|
}
|
|
_formKey.currentState?.save();
|
|
int? status = await _userProvider?.login(
|
|
user: _user,
|
|
host: _settingProvider?.host ?? "",
|
|
);
|
|
if (status != null &&
|
|
status >= 200 &&
|
|
status < 300) {
|
|
_settingProvider
|
|
?.setUser(_userProvider?.user ?? User());
|
|
if (_userProvider?.user?.isActive ?? false) {
|
|
Navigator.of(context).pushNamed(LandPage.id);
|
|
} else {
|
|
Fluttertoast.showToast(
|
|
msg: subtitle?.activationAlert ?? "",
|
|
);
|
|
}
|
|
} else {
|
|
String errorMessage = status == 400
|
|
? subtitle?.wrongEmailOrPassword ?? ""
|
|
: HttpStatusManger.getStatusMessage(
|
|
status: status, subtitle: subtitle);
|
|
ScaffoldMessenger.of(context)
|
|
.showSnackBar(SnackBar(
|
|
content: Text(errorMessage),
|
|
));
|
|
}
|
|
},
|
|
),
|
|
// SizedBox(
|
|
// height: 140 * AppStyle.getScaleFactor(context),
|
|
// ),
|
|
// AOutLinedButton(
|
|
// text: _subtitle.signUp,
|
|
// //color: AColors.cyan,
|
|
// onPressed: () {
|
|
// Navigator.of(context).pushNamed(Register.id);
|
|
// },
|
|
// ),
|
|
const SizedBox(height: 32),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|