Skip to content

Commit

Permalink
Merge pull request #27 from udos86/v7.2.1
Browse files Browse the repository at this point in the history
v7.2.1
  • Loading branch information
udos86 authored Jun 12, 2022
2 parents c9f94bb + 94274e2 commit b5fb157
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [7.2.1] - 06/12/2022

* resets `FastFormArray` properly

## [7.2.0] - 06/11/2022

* adds new `FastFormArray` widget (see [example](https://github.com/udos86/flutter-fast-forms/blob/master/example/lib/main.dart#L120) for usage and implementing [`FastFormArray` items](https://github.com/udos86/flutter-fast-forms/blob/master/example/lib/form_array_item.dart))
Expand Down
20 changes: 15 additions & 5 deletions lib/src/form_array.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ class FastFormArrayState<T> extends FastFormFieldState<List<T?>> {
@override
void initState() {
super.initState();
_initKeys();
}

if (widget.initialValue != null) {
for (var index = 0; index < widget.initialValue!.length; index++) {
_keys.add(UniqueKey());
}
}
@override
void onReset() {
super.onReset();
_initKeys();
}

void didItemChange(int index, T? value) {
Expand Down Expand Up @@ -95,6 +96,15 @@ class FastFormArrayState<T> extends FastFormFieldState<List<T?>> {
didChange(newValue);
}
}

void _initKeys() {
if (_keys.isNotEmpty) _keys.clear();
if (widget.initialValue != null) {
for (var index = 0; index < widget.initialValue!.length; index++) {
_keys.add(UniqueKey());
}
}
}
}

Widget formArrayBuilder<T>(FormFieldState<List<T?>> field) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_fast_forms
description: Flutter Fast Forms is the only Dart package you'll ever need to build Flutter forms fast.
version: 7.2.0
version: 7.2.1
repository: https://github.com/udos86/flutter-fast-forms

environment:
Expand Down
35 changes: 35 additions & 0 deletions test/form_array_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,39 @@ void main() {
.elementAt(newIndex);
expect(itemState.counter, testValue);
});

testWidgets('resets FastFormArray', (WidgetTester tester) async {
final formKey = GlobalKey<FormState>();
const initialValue = [0, 42, 99];

await tester.pumpWidget(buildMaterialTestApp(
FastFormArray<int>(
name: 'form_array',
initialValue: initialValue,
itemBuilder: (key, index, field) => TestItem(key, index, field),
),
formKey: formKey,
));

final state = tester.state<FastFormArrayState<int>>(
find.byType(typeOf<FastFormArray<int>>()));
const testValue = 1001;

state.didItemChange(initialValue.indexOf(initialValue.first), testValue);
state.remove(initialValue.indexOf(initialValue.last));

await tester.pumpAndSettle();
expect(find.byType(TestItem), findsNWidgets(state.value!.length));
expect(state.value, [
testValue,
...[...initialValue]
..remove(initialValue.first)
..remove(initialValue.last)
]);

formKey.currentState?.reset();
await tester.pumpAndSettle();
expect(find.byType(TestItem), findsNWidgets(initialValue.length));
expect(state.value, [...initialValue]);
});
}

0 comments on commit b5fb157

Please sign in to comment.