feat(profile): some fixes, add buttons

This commit is contained in:
mso7nueh
2024-09-04 17:27:46 +03:00
parent d76f535663
commit f9c10047b2
154 changed files with 1249 additions and 296 deletions

View File

@@ -0,0 +1,232 @@
import 'dart:convert';
import 'package:eatvio_app/core/app_export.dart';
import 'package:eatvio_app/core/token_storage.dart';
import 'package:eatvio_app/widgets/app_bar/appbar_title_image.dart';
import 'package:eatvio_app/widgets/custom_text_form_field.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:http/http.dart' as http;
import 'package:url_launcher/url_launcher.dart';
import 'package:validators/validators.dart';
import 'bloc/auth_bloc.dart';
import 'models/auth_model.dart';
class AuthScreen extends StatefulWidget {
static Widget builder(BuildContext context) {
return BlocProvider<AuthBloc>(
create: (context) => AuthBloc(
AuthState(
authModelObj: AuthModel(),
),
)..add(
AuthInitialEvent(),
),
child: AuthScreen(),
);
}
@override
State<AuthScreen> createState() => _AuthScreenState();
}
class _AuthScreenState extends State<AuthScreen> {
bool checkboxState = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: AppbarTitleImage(imagePath: ImageConstant.imgLogoEatvio),
),
body: SafeArea(
child: Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("email".tr),
SizedBox(height: 4),
BlocSelector<AuthBloc, AuthState, TextEditingController?>(
selector: (state) => state.emailController,
builder: (context, emailController) {
return CustomTextFormField(
controller: emailController,
textInputAction: TextInputAction.next,
keyboardType: TextInputType.emailAddress,
textInputType: TextInputType.emailAddress,
hintText: 'Введите email',
);
},
),
SizedBox(height: 16),
Text("password".tr),
SizedBox(height: 4),
BlocSelector<AuthBloc, AuthState, TextEditingController?>(
selector: (state) => state.passwordController,
builder: (context, passwordController) {
return CustomTextFormField(
obscureText: true,
controller: passwordController,
hintText: 'Введите пароль',
);
},
),
SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Нет аккаунта? '),
GestureDetector(
onTap: () => GoRouter.of(context).goNamed('registrationScreen'),
child: Text(
'Зарегистрироваться',
style: TextStyle(
color: appTheme.blueA400,
decorationColor: appTheme.blueA400,
decoration: TextDecoration.underline,
),
),
),
],
),
Spacer(),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Нажимая "Войти", Вы соглашаетесь с ',
style: TextStyle(
fontSize: 12,
),
),
GestureDetector(
onTap: () async {
await launchUrl(Uri.parse(baseUrl + '/oferta_bs.pdf'));
},
child: Text(
'Публичной офертой',
style: TextStyle(
fontSize: 12,
color: appTheme.blueA400,
decorationColor: appTheme.blueA400,
decoration: TextDecoration.underline,
),
),
),
],
),
SizedBox(height: 16),
BlocBuilder<AuthBloc, AuthState>(
builder: (context, state) {
return Container(
height: 48,
width: MediaQuery.of(context).size.width - 32,
child: FloatingActionButton.extended(
heroTag: 'authButton',
highlightElevation: 0,
elevation: 0,
focusElevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
backgroundColor: appTheme.blueA400,
onPressed: onTapAuth(state, context),
label: Text(
'Войти',
style: theme.textTheme.titleSmall,
),
),
);
},
),
],
),
),
),
);
}
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(baseUrl + '/api/login'),
headers: {'Content-Type': 'application/json'},
body: json.encode(credentials),
encoding: utf8,
);
print(response.statusCode);
if (response.statusCode == 200) {
var res = await jsonDecode(response.body);
final token = res['token'];
await TokenStorage.saveTokenInStorage(token);
GoRouter.of(context).go('/my-profile-screen');
} 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'),
),
],
),
);
}
};
}
List<String> validateFields(String email, String password) {
List<String> errors = [];
if (!isEmail(email)) {
errors.add('email_invalid'.tr);
}
if (email.isEmpty) {
errors.add('email_empty'.tr);
}
if (password.isEmpty) {
errors.add('password_empty'.tr);
}
if (password.length < 6) {
errors.add('password_short'.tr);
}
return errors;
}
}

View File

@@ -0,0 +1,27 @@
import 'package:eatvio_app/old/auth/auth_screen/models/auth_model.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import '/core/app_export.dart';
part 'auth_event.dart';
part 'auth_state.dart';
/// A bloc that manages the state of a Auth according to the event that is dispatched to it.
class AuthBloc extends Bloc<AuthEvent, AuthState> {
AuthBloc(AuthState initialState) : super(initialState) {
on<AuthInitialEvent>(_onInitialize);
}
_onInitialize(
AuthInitialEvent event,
Emitter<AuthState> emit,
) async {
emit(
state.copyWith(
emailController: TextEditingController(),
passwordController: TextEditingController(),
),
);
}
}

View File

@@ -0,0 +1,16 @@
// ignore_for_file: must_be_immutable
part of 'auth_bloc.dart';
/// Abstract class for all events that can be dispatched from the
///Credentials widget.
///
/// Events must be immutable and implement the [Equatable] interface.
@immutable
abstract class AuthEvent extends Equatable {}
/// Event that is dispatched when the Credentials widget is first created.
class AuthInitialEvent extends AuthEvent {
@override
List<Object?> get props => [];
}

View File

@@ -0,0 +1,37 @@
// ignore_for_file: must_be_immutable
part of 'auth_bloc.dart';
/// Represents the state of Auth in the application.
class AuthState extends Equatable {
AuthState({
this.emailController,
this.passwordController,
this.authModelObj,
});
TextEditingController? emailController;
TextEditingController? passwordController;
AuthModel? authModelObj;
@override
List<Object?> get props => [
emailController,
passwordController,
authModelObj,
];
AuthState copyWith({
TextEditingController? emailController,
TextEditingController? passwordController,
AuthModel? authModelObj,
}) {
return AuthState(
emailController: emailController ?? this.emailController,
passwordController: passwordController ?? this.passwordController,
authModelObj: authModelObj ?? this.authModelObj,
);
}
}

View File

@@ -0,0 +1,16 @@
// ignore_for_file: must_be_immutable
import 'package:equatable/equatable.dart';
/// This class defines the variables used in the [auth_screen],
/// and is typically used to hold data that is passed between different parts of the application.
class AuthModel extends Equatable {
AuthModel();
AuthModel copyWith() {
return AuthModel();
}
@override
List<Object?> get props => [];
}