Refactoring

This commit is contained in:
2024-03-05 16:42:23 +03:00
parent 2723861b04
commit ba10b5bdb8
11 changed files with 337 additions and 264 deletions

View File

@@ -64,94 +64,106 @@ class AuthScreen extends StatelessWidget {
margin:
EdgeInsets.only(left: 16.h, right: 16.h, bottom: 50.v),
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
onPressed: () {
Navigator.pushNamed(
context, AppRoutes.languageSelectScreen);
}),
onPressed: onTapNoAccount(context)),
NextStepButton(
text: "Тест дневника".toUpperCase(),
margin:
EdgeInsets.only(left: 16.h, right: 16.h, bottom: 50.v),
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
onPressed: () {
Navigator.pushNamed(context, AppRoutes.diaryEdit);
}),
onPressed: onTapDiaryTest(context)),
BlocBuilder<AuthBloc, AuthState>(builder: (context, state) {
return NextStepButton(
text: "authentificate".tr.toUpperCase(),
margin:
EdgeInsets.only(left: 16.h, right: 16.h, bottom: 50.v),
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
onPressed: () async {
List<String> errors = validateFields(
state.emailController!.text,
state.passwordController!.text);
if (errors.isEmpty) {
final Map<String, dynamic> credentials = {
'email': state.emailController!.text,
'password': state.passwordController!.text,
};
final response = await http.post(
Uri.parse(dotenv.env['SERVER_ROOT']! + '/login'),
headers: {'Content-Type': 'application/json'},
body: json.encode(credentials),
encoding: utf8);
if (response.statusCode == 200) {
var res = await jsonDecode(response.body);
final box = await Hive.openBox('token');
final token = res['token'];
box.put('token', token);
Navigator.pushNamed(
context,
AppRoutes.myProfile,
);
} else {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Ошибка'),
content: Text('wrong_credentials'.tr),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('OK'),
),
],
),
);
}
} else {
String errorMessage = '';
for (var error in errors) {
errorMessage += error + '\n\n';
}
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Ошибка'),
content: Text(errorMessage),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('OK'),
),
],
),
);
}
});
onPressed: onTapAuth(state, context));
}),
])));
}
onTapAuth(AuthState state, BuildContext context) {
return () async {
List<String> errors = validateFields(
state.emailController!.text, state.passwordController!.text);
if (errors.isEmpty) {
final Map<String, dynamic> credentials = {
'email': state.emailController!.text,
'password': state.passwordController!.text,
};
final response = await http.post(
Uri.parse(dotenv.env['SERVER_ROOT']! + '/login'),
headers: {'Content-Type': 'application/json'},
body: json.encode(credentials),
encoding: utf8);
if (response.statusCode == 200) {
var res = await jsonDecode(response.body);
final box = await Hive.openBox('token');
final token = res['token'];
box.put('token', token);
Navigator.pushNamed(
context,
AppRoutes.myProfile,
);
} else {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Ошибка'),
content: Text('wrong_credentials'.tr),
actions: [
TextButton(
onPressed: onTapOk(context),
child: Text('OK'),
),
],
),
);
}
} else {
String errorMessage = '';
for (var error in errors) {
errorMessage += error + '\n\n';
}
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Ошибка'),
content: Text(errorMessage),
actions: [
TextButton(
onPressed: onTapOk(context),
child: Text('OK'),
),
],
),
);
}
};
}
onTapOk(BuildContext context) {
return () {
Navigator.pop(context);
};
}
onTapDiaryTest(BuildContext context) {
return () {
Navigator.pushNamed(context, AppRoutes.diaryEdit);
};
}
onTapNoAccount(BuildContext context) {
return () {
Navigator.pushNamed(context, AppRoutes.languageSelectScreen);
};
}
/// Section Widget
PreferredSizeWidget _buildAppBar(BuildContext context) {
_buildAppBar(BuildContext context) {
return CustomAppBar(
leadingWidth: 29.h,
centerTitle: true,