Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:formz/formz.dart';
void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
const MyApp({super.key});
const new({super.key});

@override
Widget build(BuildContext context) {
Expand All @@ -23,7 +23,7 @@ class MyApp extends StatelessWidget {
}

class MyForm extends StatefulWidget {
MyForm({super.key, Random? seed}) : seed = seed ?? Random();
new({super.key, Random? seed}) : seed = seed ?? Random();

final Random seed;

Expand Down Expand Up @@ -168,7 +168,7 @@ class _MyFormState extends State<MyForm> {
}

class MyFormState with FormzMixin {
MyFormState({
new({
Email? email,
this.password = const Password.pure(),
this.status = FormzSubmissionStatus.initial,
Expand Down Expand Up @@ -198,9 +198,9 @@ enum EmailValidationError { invalid, empty }

class Email extends FormzInput<String, EmailValidationError>
with FormzInputErrorCacheMixin {
Email.pure([super.value = '']) : super.pure();
new pure([super.value = '']) : super.pure();

Email.dirty([super.value = '']) : super.dirty();
new dirty([super.value = '']) : super.dirty();

static final _emailRegExp = RegExp(
r'^[a-zA-Z\d.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z\d-]+(?:\.[a-zA-Z\d-]+)*$',
Expand All @@ -221,12 +221,13 @@ class Email extends FormzInput<String, EmailValidationError>
enum PasswordValidationError { invalid, empty }

class Password extends FormzInput<String, PasswordValidationError> {
const Password.pure([super.value = '']) : super.pure();
const new pure([super.value = '']) : super.pure();

const Password.dirty([super.value = '']) : super.dirty();
const new dirty([super.value = '']) : super.dirty();

static final _passwordRegex =
RegExp(r'^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$');
static final _passwordRegex = RegExp(
r'^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$',
);

@override
PasswordValidationError? validator(String value) {
Expand Down
25 changes: 9 additions & 16 deletions example/test/main_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

class MockRandom extends Mock implements Random {}
class MockRandom extends Mock implements Random;

final _seed = MockRandom();

Expand Down Expand Up @@ -70,10 +70,7 @@ void main() {
testWidgets('empty email', (tester) async {
await tester.pumpMyForm();

await tester.enterText(
find.byKey(const Key('myForm_emailInput')),
'',
);
await tester.enterText(find.byKey(const Key('myForm_emailInput')), '');
await tester.pumpAndSettle();

await tester.enterText(
Expand All @@ -85,10 +82,7 @@ void main() {
await tester.pumpAndSettle();
await tester.pumpAndSettle();

expect(
find.text('Please ensure the email entered is valid'),
findsOneWidget,
);
expect(find.text('Please enter an email'), findsOneWidget);
});

testWidgets('invalid password', (tester) async {
Expand Down Expand Up @@ -135,19 +129,18 @@ void main() {
await tester.pumpAndSettle();
await tester.pumpAndSettle();

expect(
find.text(
'''Password must be at least 8 characters and contain at least one letter and number''',
),
findsOneWidget,
);
expect(find.text('Please enter a password'), findsOneWidget);
});
});
});
}

extension on WidgetTester {
Future<void> pumpMyForm() async {
await pumpWidget(MaterialApp(home: Scaffold(body: MyForm(seed: _seed))));
await pumpWidget(
MaterialApp(
home: Scaffold(body: MyForm(seed: _seed)),
),
);
}
}
6 changes: 3 additions & 3 deletions lib/formz.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ extension FormzSubmissionStatusX on FormzSubmissionStatus {
/// {@endtemplate}
@immutable
abstract class FormzInput<T, E> {
const FormzInput._({required this.value, this.isPure = true});
const new _({required this.value, this.isPure = true});

/// Constructor which create a `pure` [FormzInput] with a given value.
const FormzInput.pure(T value) : this._(value: value);
const new pure(T value) : this._(value: value);

/// Constructor which create a `dirty` [FormzInput] with a given value.
const FormzInput.dirty(T value) : this._(value: value, isPure: false);
const new dirty(T value) : this._(value: value, isPure: false);

/// The value of the given [FormzInput].
/// For example, if you have a `FormzInput` for `FirstName`,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ dependencies:

dev_dependencies:
test: ^1.26.3
very_good_analysis: ^10.3.0
very_good_analysis: ">=10.3.0 <12.0.0"
10 changes: 5 additions & 5 deletions test/helpers/name_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import 'package:formz/formz.dart';
enum NameInputError { empty }

class NameInput extends FormzInput<String, NameInputError> {
const NameInput.pure({String value = ''}) : super.pure(value);
const NameInput.dirty({String value = ''}) : super.dirty(value);
const new pure({String value = ''}) : super.pure(value);
const new dirty({String value = ''}) : super.dirty(value);

@override
NameInputError? validator(String value) {
Expand All @@ -13,7 +13,7 @@ class NameInput extends FormzInput<String, NameInputError> {
}

class NameInputFormzMixin with FormzMixin {
NameInputFormzMixin({this.name = const NameInput.pure()});
new({this.name = const NameInput.pure()});

final NameInput name;

Expand All @@ -25,8 +25,8 @@ class NameInputFormzMixin with FormzMixin {
// ignore: must_be_immutable
class NameInputErrorCacheMixin extends FormzInput<String, NameInputError>
with FormzInputErrorCacheMixin {
NameInputErrorCacheMixin.pure({String value = ''}) : super.pure(value);
NameInputErrorCacheMixin.dirty({String value = ''}) : super.dirty(value);
new pure({String value = ''}) : super.pure(value);
new dirty({String value = ''}) : super.dirty(value);

int validatorCalls = 0;

Expand Down
Loading