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

juancastillo0 / json_form / 10909757877

17 Sep 2024 07:29PM UTC coverage: 78.271% (+3.9%) from 74.365%
10909757877

Pull #6

github

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

380 of 453 new or added lines in 27 files covered. (83.89%)

22 existing lines in 5 files now uncovered.

1639 of 2094 relevant lines covered (78.27%)

1.27 hits per line

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

83.18
/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 = JsonFormController.setField(context, _schemaObject, this);
4✔
35
    syncValues();
1✔
36
  }
37

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

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

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

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

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

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

NEW
190
  @override
×
191
  set value(Map<String, Object?> newValue) {
NEW
192
    newValue.forEach((k, v) {
×
NEW
193
      fromValue.children.firstWhere((c) => c.id == k).field!.value = v;
×
194
    });
NEW
195
    for (final c in fromValue.children) {
×
NEW
196
      if (!newValue.containsKey(c.id)) {
×
NEW
197
        c.field!.value = null;
×
198
      }
199
    }
200
  }
201
}
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