175 lines
5.8 KiB
Dart
175 lines
5.8 KiB
Dart
import 'package:eatvio_app/app_router.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
import 'bloc/name_bloc.dart';
|
|
import 'models/name_model.dart';
|
|
import 'package:eatvio_app/core/app_export.dart';
|
|
import 'package:eatvio_app/widgets/app_bar/appbar_leading_image.dart';
|
|
import 'package:eatvio_app/widgets/app_bar/appbar_title_image.dart';
|
|
import 'package:eatvio_app/widgets/app_bar/custom_app_bar.dart';
|
|
import 'package:eatvio_app/widgets/next_step_button.dart';
|
|
import 'package:eatvio_app/data/models/userRegistrationModel/user_registration_model.dart';
|
|
import 'package:eatvio_app/widgets/custom_text_form_field.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class NameScreen extends StatelessWidget {
|
|
const NameScreen({Key? key}) : super(key: key);
|
|
|
|
static Widget builder(BuildContext context, sheet) {
|
|
return BlocProvider<NameBloc>(
|
|
create: (context) => NameBloc(
|
|
NameState(
|
|
nameModelObj: NameModel(userRegistrationSheet: sheet),
|
|
),
|
|
)..add(NameInitialEvent()),
|
|
child: NameScreen(),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
mediaQueryData = MediaQuery.of(context);
|
|
return Scaffold(
|
|
resizeToAvoidBottomInset: false,
|
|
appBar: _buildAppBar(context),
|
|
body: Container(
|
|
width: double.maxFinite,
|
|
padding: EdgeInsets.symmetric(horizontal: 16.h, vertical: 9.v),
|
|
child: Column(
|
|
children: [
|
|
SizedBox(height: 7.v),
|
|
Container(
|
|
height: 16.v,
|
|
width: 343.h,
|
|
decoration: BoxDecoration(
|
|
color: theme.colorScheme.onPrimary,
|
|
borderRadius: BorderRadius.circular(8.h),
|
|
border: Border.all(color: theme.colorScheme.primary, width: 1.h),
|
|
),
|
|
child: ClipRRect(
|
|
borderRadius: BorderRadius.circular(8.h),
|
|
child: LinearProgressIndicator(
|
|
value: (2 / 15).toDouble(),
|
|
backgroundColor: theme.colorScheme.onPrimary,
|
|
valueColor: AlwaysStoppedAnimation<Color>(appTheme.blueA400),
|
|
),
|
|
),
|
|
),
|
|
SizedBox(height: 39.v),
|
|
Text("what_is_your_name".tr, style: theme.textTheme.headlineSmall),
|
|
SizedBox(height: 93.v),
|
|
Text("first_name".tr, style: theme.textTheme.bodyLarge),
|
|
SizedBox(height: 3.v),
|
|
BlocSelector<NameBloc, NameState, TextEditingController?>(
|
|
selector: (state) => state.firstNameController,
|
|
builder: (context, firstNameController) {
|
|
return CustomTextFormField(controller: firstNameController, textInputAction: TextInputAction.done);
|
|
},
|
|
),
|
|
SizedBox(height: 19.v),
|
|
Text("last_name".tr, style: theme.textTheme.bodyLarge),
|
|
SizedBox(height: 5.v),
|
|
BlocSelector<NameBloc, NameState, TextEditingController?>(
|
|
selector: (state) => state.lastNameController,
|
|
builder: (context, lastNameController) {
|
|
return CustomTextFormField(
|
|
controller: lastNameController,
|
|
);
|
|
},
|
|
),
|
|
Spacer(),
|
|
BlocBuilder<NameBloc, NameState>(
|
|
builder: (context, state) {
|
|
return NextStepButton(
|
|
text: "continue".tr.toUpperCase(),
|
|
margin: EdgeInsets.only(left: 16.h, right: 16.h, bottom: 50.v),
|
|
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
|
|
onPressed: () => _onTapNext(context, state), // Assign callback function here
|
|
);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Section Widget
|
|
_buildAppBar(BuildContext context) {
|
|
return CustomAppBar(
|
|
leadingWidth: 29.h,
|
|
leading: AppbarLeadingImage(
|
|
imagePath: ImageConstant.imgArrowLeft,
|
|
margin: EdgeInsets.only(left: 16.h, top: 15.v, bottom: 15.v),
|
|
onTap: () => onTapArrowLeft(context),
|
|
),
|
|
centerTitle: true,
|
|
title: AppbarTitleImage(imagePath: ImageConstant.imgLogoEatvio),
|
|
);
|
|
}
|
|
|
|
/// Navigates to the previous screen.
|
|
onTapArrowLeft(BuildContext context) {
|
|
GoRouter.of(context).pop();
|
|
}
|
|
|
|
_onTapNext(BuildContext context, NameState state) {
|
|
if (state.firstNameController != null && state.lastNameController != null) {
|
|
List<String> errors = validateFields(state.firstNameController!.text, state.lastNameController!.text);
|
|
|
|
if (errors.isEmpty) {
|
|
UserRegistrationSheet sheet = state.nameModelObj!.userRegistrationSheet;
|
|
sheet.firstName = state.firstNameController!.text;
|
|
sheet.lastName = state.lastNameController!.text;
|
|
AppRouter.navigatorKey.currentContext?.goNamed('genderScreen', extra: sheet);
|
|
} else {
|
|
String errorMessage = '';
|
|
|
|
for (var error in errors) {
|
|
errorMessage += error + '\n\n';
|
|
}
|
|
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: Text('Error'),
|
|
content: Text(errorMessage),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => onTapOk(context),
|
|
child: Text('OK'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
void onTapOk(BuildContext context) {
|
|
Navigator.pop(context);
|
|
}
|
|
|
|
List<String> validateFields(String firstName, String lastName) {
|
|
List<String> errors = [];
|
|
|
|
if (firstName.isEmpty) {
|
|
errors.add('First name cannot be empty');
|
|
}
|
|
|
|
if (firstName.length < 1) {
|
|
errors.add('First name is too short');
|
|
}
|
|
|
|
if (lastName.isEmpty) {
|
|
errors.add('Last name cannot be empty');
|
|
}
|
|
|
|
if (lastName.length < 1) {
|
|
errors.add('Last name is too short');
|
|
}
|
|
|
|
return errors;
|
|
}
|
|
}
|