Skip to content

Commit

Permalink
fix panel_dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
goxiaoy committed Jul 12, 2023
1 parent 75ba741 commit 1fc8d94
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/ui/elements/panel_dynamic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:flutter_survey_js/ui/survey_configuration.dart';
import 'package:flutter_survey_js_model/flutter_survey_js_model.dart' as s;
import 'package:reactive_forms/reactive_forms.dart';


Widget panelDynamicBuilder(BuildContext context, s.Elementbase element,
{ElementConfiguration? configuration}) {
return PanelDynamicElement(
Expand All @@ -25,12 +24,13 @@ class PanelDynamicElement extends StatelessWidget {

@override
Widget build(BuildContext context) {
createNew() {
createNew({Object? value}) {
//create new formGroup
return elementsToFormGroup(
context,
(element.templateElements?.map((p0) => p0.realElement).toList() ?? [])
.toList());
.toList(),
value: value);
}

return ReactiveNestedGroupArray(
Expand Down
20 changes: 17 additions & 3 deletions lib/ui/reactive/reactive_nested_group_array.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ReactiveNestedGroupArray<T> extends StatelessWidget {
final FormArray<T>? formArray;
final Widget? child;
final ReactiveNestedGroupArrayBuilder builder;
final AbstractControl<T> Function()? createNew;
final FormGroup Function({Object? value}) createNew;
final int minLength;
final int? maxLength;

Expand All @@ -23,7 +23,7 @@ class ReactiveNestedGroupArray<T> extends StatelessWidget {
this.formArray,
this.child,
required this.builder,
this.createNew,
required this.createNew,
this.minLength = 0,
this.maxLength,
}) : assert(minLength >= 0),
Expand All @@ -48,7 +48,21 @@ class ReactiveNestedGroupArray<T> extends StatelessWidget {
child: child,
builder: (context, formArray, child) {
while (formArray.controls.length < minLength) {
formArray.add(createNew!());
formArray.add(createNew());
}
final formGroups = <FormGroup>[];
bool modified = false;
for (final c in formArray.controls) {
if (c is FormGroup) {
formGroups.add(c);
} else {
formGroups.add(createNew(value: c.value));
modified = true;
}
}
if (modified) {
formArray.clear();
formArray.addAll(formGroups);
}
final controls = formArray.controls.cast<FormGroup>().toList();
return Column(children: [
Expand Down

0 comments on commit 1fc8d94

Please sign in to comment.