Skip to content

Commit

Permalink
refactor(form_flow): support for patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
marcossevilla committed Jul 24, 2023
1 parent 1b69a9d commit ec39cd3
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 64 deletions.
6 changes: 3 additions & 3 deletions examples/form_flow/lib/app/bloc/app_event.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
part of 'app_bloc.dart';

abstract class AppEvent extends Equatable {
sealed class AppEvent extends Equatable {
const AppEvent();

@override
List<Object> get props => [];
}

class AppLogoutRequested extends AppEvent {}
final class AppLogoutRequested extends AppEvent {}

class AppSignUpCompleted extends AppEvent {
final class AppSignUpCompleted extends AppEvent {
const AppSignUpCompleted(this.user);

final User user;
Expand Down
6 changes: 3 additions & 3 deletions examples/form_flow/lib/app/bloc/app_state.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
part of 'app_bloc.dart';

abstract class AppState extends Equatable {
sealed class AppState extends Equatable {
const AppState();

@override
List<Object> get props => [];
}

class AppUnauthenticated extends AppState {}
final class AppUnauthenticated extends AppState {}

class AppAuthenticated extends AppState {
final class AppAuthenticated extends AppState {
const AppAuthenticated(this.user);

final User user;
Expand Down
9 changes: 4 additions & 5 deletions examples/form_flow/lib/app/routes/routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ List<Page<void>> onGenerateAppViewPages(
AppState state,
List<Page<void>> pages,
) {
if (state is AppAuthenticated) {
return [ProfilePage.page(state.user)];
} else {
return [SignUpPage.page()];
}
return switch (state) {
AppAuthenticated() => [ProfilePage.page(state.user)],
AppUnauthenticated() => [SignUpPage.page()],
};
}
8 changes: 4 additions & 4 deletions examples/form_flow/lib/signup/bloc/sign_up_event.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
part of 'sign_up_bloc.dart';

abstract class SignUpEvent extends Equatable {
sealed class SignUpEvent extends Equatable {
const SignUpEvent();
}

class SignUpCredentialsSubmitted extends SignUpEvent {
final class SignUpCredentialsSubmitted extends SignUpEvent {
const SignUpCredentialsSubmitted({
required this.email,
required this.name,
Expand All @@ -17,7 +17,7 @@ class SignUpCredentialsSubmitted extends SignUpEvent {
List<Object> get props => [email, name];
}

class SignUpBiographySubmitted extends SignUpEvent {
final class SignUpBiographySubmitted extends SignUpEvent {
const SignUpBiographySubmitted(this.biography);

final String biography;
Expand All @@ -26,7 +26,7 @@ class SignUpBiographySubmitted extends SignUpEvent {
List<Object> get props => [biography];
}

class SignUpPinSubmitted extends SignUpEvent {
final class SignUpPinSubmitted extends SignUpEvent {
const SignUpPinSubmitted(this.pin);

final String pin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,13 @@ class _EmailInput extends StatelessWidget {
return TextField(
key: const Key('credentialsForm_emailInput_textField'),
autocorrect: false,
onChanged: (email) {
context.read<CredentialsCubit>().changeEmail(email);
},
onChanged: context.read<CredentialsCubit>().changeEmail,
decoration: InputDecoration(
labelText: l10n.emailInputLabelText,
errorText: () {
if (state.isPure) return null;
return switch (state.error) {
EmailValidationError.invalid => l10n.invalidEmailInputErrorText,
null => null,
};
}(),
errorText: switch (state.error) {
EmailValidationError.invalid => l10n.invalidEmailInputErrorText,
null => null,
},
),
);
},
Expand All @@ -63,18 +58,13 @@ class _NameInput extends StatelessWidget {
key: const Key('credentialsForm_nameInput_textField'),
autocorrect: false,
textCapitalization: TextCapitalization.words,
onChanged: (name) {
context.read<CredentialsCubit>().changeName(name);
},
onChanged: context.read<CredentialsCubit>().changeName,
decoration: InputDecoration(
labelText: l10n.nameInputLabelText,
errorText: () {
if (state.isPure) return null;
return switch (state.error) {
NameValidationError.tooShort => l10n.shortNameInputErrorText,
null => null,
};
}(),
errorText: switch (state.error) {
NameValidationError.tooShort => l10n.shortNameInputErrorText,
null => null,
},
),
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import 'package:form_flow/signup/signup.dart';
class CredentialsPage extends StatelessWidget {
const CredentialsPage({super.key});

static Page<void> page() =>
const MaterialPage<void>(child: CredentialsPage());
static Page<void> page() {
return const MaterialPage<void>(
child: CredentialsPage(),
);
}

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -43,9 +46,7 @@ class _SubmitButton extends StatelessWidget {
final l10n = context.l10n;
return BlocBuilder<CredentialsCubit, CredentialsState>(
builder: (context, state) {
if (!state.isValid || state.isPure) {
return const SizedBox.shrink();
}
if (!state.isValid || state.isPure) return const SizedBox.shrink();

return FloatingActionButton.extended(
key: const Key('credentialsPage_submitButton_floatingActionButton'),
Expand Down
19 changes: 8 additions & 11 deletions examples/form_flow/lib/signup/pin/view/pin_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,17 @@ class _PinInput extends StatelessWidget {
key: const Key('pinView_pinInput_textField'),
autocorrect: false,
obscureText: true,
onChanged: (pin) => context.read<PinCubit>().changePin(pin),
decoration: InputDecoration(
labelText: l10n.pinInputLabelText,
errorText: () {
if (state.isPure) return null;
return switch (state.error) {
PinValidationError.invalid => l10n.invalidPinInputErrorText,
null => null
};
}(),
),
onChanged: context.read<PinCubit>().changePin,
inputFormatters: [
LengthLimitingTextInputFormatter(PinFormInput.maxLength),
],
decoration: InputDecoration(
labelText: l10n.pinInputLabelText,
errorText: switch (state.error) {
PinValidationError.invalid => l10n.invalidPinInputErrorText,
null => null
},
),
);
},
);
Expand Down
15 changes: 8 additions & 7 deletions examples/form_flow/lib/signup/routes/routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ List<Page<void>> onGenerateSignUpPages(
SignUpState state,
List<Page<void>> pages,
) {
if (state.user.email.isEmpty && state.user.name.isEmpty) {
return [CredentialsPage.page()];
} else if (state.user.biography.isEmpty) {
return [BiographyPage.page()];
} else {
return [PinPage.page()];
}
return switch (state) {
SignUpState(user: final user) when user.email.isEmpty && user.pin.isEmpty =>
[CredentialsPage.page()],
SignUpState(user: final user) when user.biography.isEmpty => [
BiographyPage.page()
],
_ => [PinPage.page()],
};
}
6 changes: 0 additions & 6 deletions examples/form_flow/test/helpers/mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,8 @@ class MockUser extends Mock implements User {}
class FakeBloc extends Fake implements Bloc<Object, Object> {}

class FakeAppState extends Fake implements AppState {}

class FakeAppEvent extends Fake implements AppEvent {}

class FakeSignUpState extends Fake implements SignUpState {}

class FakeSignUpEvent extends Fake implements SignUpEvent {}

class FakeCredentialsState extends Fake implements CredentialsState {}

class FakeBiographyState extends Fake implements BiographyState {}
Expand Down

0 comments on commit ec39cd3

Please sign in to comment.