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

juancastillo0 / json_form / 13743422655

09 Mar 2025 01:23AM UTC coverage: 80.521% (-0.01%) from 80.534%
13743422655

push

github

juancastillo0
ui schema width property

7 of 10 new or added lines in 3 files covered. (70.0%)

20 existing lines in 2 files now uncovered.

1670 of 2074 relevant lines covered (80.52%)

1.31 hits per line

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

98.39
/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
48
  set value(T newValue);
49

50
  @override
1✔
51
  String get idKey => formValue.idKey;
2✔
52

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

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

70
  String? customValidator(Object? newValue) {
1✔
71
    return _customValidator?.call(newValue);
2✔
72
  }
73

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

89
  void onChanged(Object? value) {
1✔
90
    _dispatchChangeEventToParentObject(value);
1✔
91
    onSaved(value);
1✔
92
  }
93

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

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

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

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

132
      onChanged(value);
1✔
133
      completer.complete(value);
1✔
134
    });
135

136
    return completer.future;
1✔
137
  }
138

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