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

juancastillo0 / json_form / 10986779271

23 Sep 2024 02:41AM UTC coverage: 78.638% (+4.3%) from 74.365%
10986779271

Pull #6

github

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

479 of 585 new or added lines in 27 files covered. (81.88%)

26 existing lines in 6 files now uncovered.

1594 of 2027 relevant lines covered (78.64%)

1.27 hits per line

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

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

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

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

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

27
  final SchemaProperty property;
28

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

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

43
  CustomValidatorHandler? _previousValidator;
44
  String? Function(Object?)? _customValidator;
45

46
  @override
47
  T get value;
48
  @override
49
  set value(T newValue);
50

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

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

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

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

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

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

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

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

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

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

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

137
    return completer.future;
1✔
138
  }
139

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