• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

juancastillo0 / json_form / 11535823100

26 Oct 2024 11:42PM UTC coverage: 80.642% (+6.3%) from 74.365%
11535823100

Pull #6

github

web-flow
Merge 71c636276 into 419da37b9
Pull Request #6: Form value in controller recursive

537 of 640 new or added lines in 27 files covered. (83.91%)

26 existing lines in 6 files now uncovered.

1658 of 2056 relevant lines covered (80.64%)

1.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/lib/src/builder/object_schema_builder.dart
1
import 'package:flutter/material.dart';
2
import 'package:json_form/json_form.dart';
3
import 'package:json_form/src/builder/general_subtitle_widget.dart';
4
import 'package:json_form/src/builder/logic/object_schema_logic.dart';
5
import 'package:json_form/src/builder/logic/widget_builder_logic.dart';
6
import 'package:json_form/src/builder/widget_builder.dart';
7
import 'package:json_form/src/fields/shared.dart';
8
import 'package:json_form/src/models/models.dart';
9

10
class ObjectSchemaBuilder extends StatefulWidget {
11
  ObjectSchemaBuilder({
1✔
12
    Key? key,
13
    required this.mainSchema,
14
    required this.schemaObject,
15
    // TODO: validate array key
16
  }) : super(key: key ?? ValueKey(schemaObject));
2✔
17

18
  final Schema mainSchema;
19
  final SchemaObject schemaObject;
20

21
  @override
1✔
22
  State<ObjectSchemaBuilder> createState() => _ObjectSchemaBuilderState();
1✔
23
}
24

25
class _ObjectSchemaBuilderState extends State<ObjectSchemaBuilder>
26
    implements JsonFormField<Map<String, Object?>> {
27
  late SchemaObject _schemaObject;
28
  late final JsonFormValue fromValue;
29

30
  @override
1✔
31
  void initState() {
32
    super.initState();
1✔
33
    _schemaObject = widget.schemaObject;
3✔
34
    fromValue =
1✔
35
        PrivateJsonFormController.setField(context, _schemaObject, this);
3✔
36
    syncValues();
1✔
37
  }
38

39
  @override
1✔
40
  void didUpdateWidget(covariant ObjectSchemaBuilder oldWidget) {
41
    super.didUpdateWidget(oldWidget);
1✔
42
    if (oldWidget.schemaObject != widget.schemaObject) {
4✔
UNCOV
43
      _schemaObject = widget.schemaObject;
×
NEW
44
      syncValues();
×
45
    }
46
  }
47

48
  void syncValues() {
1✔
49
    fromValue.children.removeWhere(
3✔
50
      (c) =>
1✔
51
          _schemaObject.properties.every((p) => p.id != c.id) &&
7✔
NEW
52
              c.dependentsAddedBy.isEmpty ||
×
53
          c.parent?.schema != _schemaObject,
4✔
54
    );
55
    final added = Set.of(fromValue.children.map((c) => c.id));
6✔
56
    fromValue.children.addAll(
3✔
57
      _schemaObject.properties.where((p) => !added.contains(p.id)).map((e) {
8✔
58
        return JsonFormValue(
1✔
59
          id: e.id,
1✔
60
          parent: fromValue,
1✔
61
          schema: e,
62
        );
63
      }),
64
    );
65
  }
66

67
  @override
1✔
68
  Widget build(BuildContext context) {
69
    final properties =
70
        _schemaObject.properties.where((p) => !p.uiSchema.hidden);
6✔
71
    final directionality = Directionality.of(context);
1✔
72
    final widgetBuilderInherited = WidgetBuilderInherited.of(context);
1✔
73
    final uiConfig = widgetBuilderInherited.uiConfig;
1✔
74
    final isTableLabel = uiConfig.labelPosition == LabelPosition.table;
2✔
75
    final objectKey = fromValue.idKey;
2✔
76

77
    final Set<Schema> dependentSchemas = {};
78
    for (final property in properties) {
2✔
79
      if (property is SchemaProperty && property.dependents?.schema != null) {
3✔
80
        dependentSchemas.add(property.dependents!.schema!);
3✔
81
      }
82
    }
83

84
    return ObjectSchemaInherited(
1✔
85
      schemaObject: _schemaObject,
1✔
86
      listen: (value) {
1✔
87
        if (value is ObjectSchemaDependencyEvent) {
1✔
88
          setState(() => _schemaObject = value.schemaObject);
4✔
89
        }
90
      },
91
      child: FormSection(
1✔
92
        child: Column(
1✔
93
          crossAxisAlignment: CrossAxisAlignment.start,
94
          children: [
1✔
95
            if (!isTableLabel || widget.schemaObject.parent is! SchemaArray)
4✔
96
              GeneralSubtitle(
1✔
97
                field: widget.schemaObject,
2✔
98
                mainSchema: widget.mainSchema,
2✔
99
              ),
100
            if (isTableLabel)
101
              Table(
1✔
102
                children: [
1✔
103
                  ...fromValue.children
2✔
104
                      .where((c) => c.schema is SchemaProperty)
4✔
105
                      .expand(
1✔
106
                    (e) {
1✔
107
                      final r = (e.schema as SchemaProperty).dependents?.schema;
3✔
108
                      return r != null && e.isDependentsActive
1✔
109
                          ? [
1✔
110
                              e,
111
                              ...((r is SchemaObject) ? r.properties : [r]).map(
3✔
112
                                (s) => JsonFormValue(
2✔
113
                                  id: s.id,
1✔
114
                                  parent: fromValue,
1✔
115
                                  schema: s,
116
                                ),
117
                              ),
118
                            ]
119
                          : [e];
1✔
120
                    },
121
                  ).map(
1✔
122
                    (e) {
1✔
123
                      final s = e.schema;
1✔
124
                      final title =
125
                          uiConfig.titleAndDescriptionBuilder?.call(s) ??
1✔
126
                              Column(
1✔
127
                                crossAxisAlignment: CrossAxisAlignment.start,
128
                                children: [
1✔
129
                                  const SizedBox(height: 15),
130
                                  Text(
1✔
131
                                    s.titleOrId,
1✔
132
                                    style: uiConfig.fieldLabel,
1✔
133
                                  ),
134
                                  if (s.description != null)
1✔
135
                                    Text(
×
NEW
136
                                      s.description!,
×
137
                                      style: uiConfig.description,
×
138
                                    ),
139
                                ],
140
                              );
141
                      return TableRow(
1✔
142
                        key: JsonFormKeys.objectProperty(
1✔
143
                          JsonFormKeyPath.appendId(objectKey, e.id),
2✔
144
                        ),
145
                        children: [
1✔
146
                          if (directionality == TextDirection.ltr) title,
2✔
147
                          FormFromSchemaBuilder(
1✔
148
                            schemaObject: widget.schemaObject,
2✔
149
                            mainSchema: widget.mainSchema,
2✔
150
                            formValue: e,
151
                          ),
152
                          if (directionality == TextDirection.rtl) title,
1✔
153
                        ],
154
                      );
155
                    },
156
                  ),
157
                ],
158
              ),
159
            ...fromValue.children
2✔
160
                .where(
1✔
161
                  (p) =>
1✔
162
                      !isTableLabel ||
163
                      p.schema is! SchemaProperty &&
2✔
164
                          !dependentSchemas.contains(p.schema),
2✔
165
                )
166
                .map(
1✔
167
                  (e) => FormFromSchemaBuilder(
2✔
168
                    key: JsonFormKeys.objectProperty(
1✔
169
                      JsonFormKeyPath.appendId(objectKey, e.id),
2✔
170
                    ),
171
                    schemaObject: widget.schemaObject,
2✔
172
                    mainSchema: widget.mainSchema,
2✔
173
                    formValue: e,
174
                  ),
175
                ),
176
          ],
177
        ),
178
      ),
179
    );
180
  }
181

NEW
182
  @override
×
NEW
183
  Map<String, Object?> get value => fromValue.toJson()! as Map<String, Object?>;
×
184
  @override
185
  final FocusNode focusNode = FocusNode();
NEW
186
  @override
×
NEW
187
  String get idKey => fromValue.idKey;
×
NEW
188
  @override
×
NEW
189
  JsonSchemaInfo get property => widget.schemaObject;
×
190

NEW
191
  @override
×
192
  set value(Map<String, Object?> newValue) {
NEW
193
    newValue.forEach((k, v) {
×
NEW
194
      fromValue.children.firstWhere((c) => c.id == k).field!.value = v;
×
195
    });
NEW
196
    for (final c in fromValue.children) {
×
NEW
197
      if (!newValue.containsKey(c.id)) {
×
NEW
198
        c.field!.value = null;
×
199
      }
200
    }
201
  }
202
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc