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.
165 lines
7.2 KiB
Dart
165 lines
7.2 KiB
Dart
|
3 years ago
|
import 'package:test_sa/controllers/api_routes/urls.dart';
|
||
|
|
import 'package:test_sa/controllers/http_status_manger/http_status_manger.dart';
|
||
|
|
import 'package:test_sa/controllers/localization/localization.dart';
|
||
|
|
import 'package:test_sa/controllers/notification/notification_manger.dart';
|
||
|
|
import 'package:test_sa/controllers/providers/api/user_provider.dart';
|
||
|
|
import 'package:test_sa/controllers/providers/settings/setting_provider.dart';
|
||
|
|
import 'package:test_sa/controllers/validator/validator.dart';
|
||
|
|
import 'package:test_sa/models/subtitle.dart';
|
||
|
|
import 'package:test_sa/models/user.dart';
|
||
|
|
import 'package:test_sa/views/app_style/sizing.dart';
|
||
|
|
import 'package:test_sa/views/pages/register.dart';
|
||
|
|
import 'package:test_sa/views/pages/user/land_page.dart';
|
||
|
|
import 'package:test_sa/views/widgets/app_text_form_field.dart';
|
||
|
|
import 'package:test_sa/views/widgets/buttons/app_button.dart';
|
||
|
|
import 'package:test_sa/views/widgets/buttons/app_flat_button.dart';
|
||
|
|
import 'package:test_sa/views/widgets/loaders/loading_manager.dart';
|
||
|
|
import 'package:flutter/material.dart';
|
||
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
||
|
|
import 'package:provider/provider.dart';
|
||
|
|
|
||
|
|
import '../widgets/buttons/app_outlined_button.dart';
|
||
|
|
class Login extends StatefulWidget {
|
||
|
|
static final String id = "/login";
|
||
|
|
@override
|
||
|
|
_LoginState createState() => _LoginState();
|
||
|
|
}
|
||
|
|
|
||
|
|
class _LoginState extends State<Login> {
|
||
|
|
UserProvider _userProvider;
|
||
|
|
SettingProvider _settingProvider;
|
||
|
|
User _user = User();
|
||
|
|
bool _obscurePassword = true;
|
||
|
|
bool _firstTime = true;
|
||
|
|
double _height;
|
||
|
|
double _width;
|
||
|
|
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 || !_settingProvider.isLoaded,
|
||
|
|
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 / 5,),
|
||
|
|
Hero(
|
||
|
|
tag: "logo",
|
||
|
|
child: Image(
|
||
|
|
height: _height/6,
|
||
|
|
fit: BoxFit.contain,
|
||
|
|
image: 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.center,
|
||
|
|
style: Theme.of(context).textTheme.headline6,
|
||
|
|
prefixIconData: Icons.account_circle,
|
||
|
|
validator: (value) =>
|
||
|
|
Validator.hasValue(value)
|
||
|
|
? null : _subtitle.nameValidateMessage,
|
||
|
|
textInputType: TextInputType.name,
|
||
|
|
onSaved: (value){
|
||
|
|
_user.userName = value;
|
||
|
|
},
|
||
|
|
),
|
||
|
|
SizedBox(height: 24 * AppStyle.getScaleFactor(context),),
|
||
|
|
ATextFormField(
|
||
|
|
initialValue: _user?.password,
|
||
|
|
hintText: _subtitle.password,
|
||
|
|
obscureText: _obscurePassword,
|
||
|
|
style: Theme.of(context).textTheme.headline6,
|
||
|
|
prefixIconData: Icons.vpn_key_sharp,
|
||
|
|
textAlign: TextAlign.center,
|
||
|
|
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())
|
||
|
|
return;
|
||
|
|
_formKey.currentState.save();
|
||
|
|
int status = await _userProvider.login(
|
||
|
|
user: _user,
|
||
|
|
host: _settingProvider.host,
|
||
|
|
);
|
||
|
|
if(status >= 200 && status < 300){
|
||
|
|
_settingProvider.setUser(_userProvider.user);
|
||
|
|
if(_userProvider.user.isActive)
|
||
|
|
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);
|
||
|
|
},
|
||
|
|
),
|
||
|
|
|
||
|
|
SizedBox(height: 32,),
|
||
|
|
],
|
||
|
|
),
|
||
|
|
),
|
||
|
|
],
|
||
|
|
),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|