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

juancastillo0 / json_form / 14147619926

29 Mar 2025 04:54PM UTC coverage: 81.034% (+0.4%) from 80.605%
14147619926

push

github

juancastillo0
fix example rendering test

1739 of 2146 relevant lines covered (81.03%)

1.32 hits per line

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

98.48
/lib/src/fields/fields.dart
1
import 'dart:async';
2

3
import 'package:flutter/material.dart';
4
import 'package:json_form/json_form.dart';
5
import 'package:json_form/src/builder/logic/object_schema_logic.dart';
6
import 'package:json_form/src/builder/logic/widget_builder_logic.dart';
7
import 'package:json_form/src/models/property_schema.dart';
8
import 'package:json_form/src/utils/date_text_input_json_formatter.dart';
9

10
export 'checkbox_form_field.dart';
11
export 'date_form_field.dart';
12
export 'dropdown_form_field.dart';
13
export 'dropdown_oneof_form_field.dart';
14
export 'file_form_field.dart';
15
export 'number_form_field.dart';
16
export 'radio_button_form_field.dart';
17
export 'slider_form_field.dart';
18
export 'text_form_field.dart';
19

20
abstract class PropertyFieldWidget<T> extends StatefulWidget {
21
  const PropertyFieldWidget({
1✔
22
    super.key,
23
    required this.property,
24
  });
25

26
  final SchemaProperty property;
27

28
  @override
29
  PropertyFieldState<T, PropertyFieldWidget<T>> createState();
30
}
31

32
abstract class PropertyFieldState<T, W extends PropertyFieldWidget<T>>
33
    extends State<W> implements JsonFormField<T> {
34
  late JsonFormValue formValue;
35
  @override
36
  final focusNode = FocusNode();
37
  @override
1✔
38
  SchemaProperty get property => widget.property;
2✔
39
  bool get readOnly => property.uiSchema.readOnly;
4✔
40
  bool get enabled => !property.uiSchema.disabled && !readOnly;
5✔
41

42
  JsonFormValidatorHandler? _previousValidator;
43
  String? Function(Object?)? _customValidator;
44

45
  @override
46
  T get value;
47
  @override
1✔
48
  @mustCallSuper
49
  set value(T newValue) {
50
    WidgetBuilderInherited.of(context).controller.updateData(idKey, newValue);
5✔
51
  }
52

53
  @override
1✔
54
  String get idKey => formValue.idKey;
2✔
55

56
  @override
1✔
57
  void initState() {
58
    super.initState();
1✔
59
    triggerDefaultValue();
1✔
60
    formValue = PrivateJsonFormController.setField(context, property, this);
4✔
61
  }
62

63
  @override
1✔
64
  void didChangeDependencies() {
65
    super.didChangeDependencies();
1✔
66
    final currentValidator = WidgetBuilderInherited.of(context).fieldValidator;
3✔
67
    if (_previousValidator != currentValidator) {
2✔
68
      _customValidator = currentValidator?.call(this);
2✔
69
      _previousValidator = currentValidator;
1✔
70
    }
71
  }
72

73
  String? customValidator(Object? newValue) {
1✔
74
    return _customValidator?.call(newValue);
2✔
75
  }
76

77
  void onSaved(Object? newValue) {
1✔
78
    final widgetBuilderInherited = WidgetBuilderInherited.of(context);
2✔
79
    if (newValue is! DateTime) {
1✔
80
      widgetBuilderInherited.controller.updateData(idKey, newValue);
3✔
81
    } else {
82
      String date;
83
      if (property.format == PropertyFormat.date) {
3✔
84
        date = formatDate(newValue);
1✔
85
      } else {
86
        date = formatDateTime(newValue);
1✔
87
      }
88
      widgetBuilderInherited.controller.updateData(idKey, date);
3✔
89
    }
90
  }
91

92
  void onChanged(Object? value) {
1✔
93
    _dispatchChangeEventToParentObject(value);
1✔
94
    onSaved(value);
1✔
95
  }
96

97
  void _dispatchChangeEventToParentObject(Object? value) {
1✔
98
    bool isActive;
99
    if (value is bool) {
1✔
100
      isActive = value;
101
    } else if (value is String) {
1✔
102
      isActive = value.isNotEmpty;
1✔
103
    } else if (value is List) {
1✔
104
      isActive = value.isNotEmpty;
×
105
    } else {
106
      isActive = value != null;
107
    }
108

109
    final isSelect = property.enumm != null && property.enumm!.isNotEmpty ||
5✔
110
        property.oneOf.isNotEmpty;
3✔
111
    if (isActive != formValue.isDependentsActive || isSelect) {
3✔
112
      ObjectSchemaInherited.of(context).listenChangeProperty(
3✔
113
        isActive,
114
        formValue,
1✔
115
        optionalValue: isSelect ? value : null,
116
      );
117
    }
118
  }
119

120
  @override
1✔
121
  void dispose() {
122
    // TODO: remove field
123
    // if (property.formField == this) {
124
    //   property.formField = null;
125
    // }
126
    super.dispose();
1✔
127
  }
128

129
  Future<T?> triggerDefaultValue() async {
1✔
130
    final completer = Completer<T?>();
1✔
131
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
3✔
132
      final value = getDefaultValue<T>();
1✔
133
      if (value == null) return completer.complete();
1✔
134

135
      onChanged(value);
1✔
136
      completer.complete(value);
1✔
137
    });
138

139
    return completer.future;
1✔
140
  }
141

142
  D? getDefaultValue<D>({bool parse = true}) {
1✔
143
    final widgetBuilderInherited = WidgetBuilderInherited.get(context);
2✔
144
    final objectData = widgetBuilderInherited.controller.retrieveData(idKey);
3✔
145
    final isDate = property.format == PropertyFormat.date ||
3✔
146
        property.format == PropertyFormat.dateTime;
3✔
147
    var data = (objectData is D || isDate && parse && objectData is String
2✔
148
            ? objectData
149
            : null) ??
150
        property.defaultValue;
2✔
151
    if (data != null && parse) {
152
      if (isDate && data is String) {
1✔
153
        data = DateTime.parse(
1✔
154
          property.format == PropertyFormat.date ? data.split(' ').first : data,
5✔
155
        );
156
      }
157
    }
158
    return data is D ? data : null;
1✔
159
  }
160

161
  @override
1✔
162
  String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
163
    return '${super.toString(minLevel: minLevel)}(idKey: $idKey, property: $property)';
4✔
164
  }
165
}
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