maybeFromDynamic static method

JsonDropdownButtonFormFieldBuilderModel? maybeFromDynamic(
  1. dynamic map, {
  2. Map<String, dynamic> args = const {},
  3. JsonWidgetRegistry? registry,
})

Builds the builder from a Map-like dynamic structure. This expects the JSON format to be of the following structure:

{
  "alignment": "<Alignment>",
  "autofocus": "<bool>",
  "autovalidateMode": "<AutovalidateMode>",
  "borderRadius": "<BorderRadius>",
  "decoration": "<InputDecorationDecoder>",
  "disabledHint": "<JsonWidgetData>",
  "dropdownColor": "<Color>",
  "elevation": "<int>",
  "enabled": "<bool>",
  "focusColor": "<Color>",
  "focusNode": "<FocusNode>",
  "hint": "<JsonWidgetData>",
  "icon": "<JsonWidgetData>",
  "items": "<List<String>> || <Map<String, dynamic>>",
  "iconDisabledColor": "<Color>",
  "iconEnabledColor": "<Color>",
  "iconSize": "<double>",
  "isDense": "<bool>",
  "isExpanded": "<bool>",
  "itemHeight": "<double>",
  "onChanged": "<ValueChanged>",
  "onSaved": "<FormFieldSetter>",
  "onTap": "<VoidCallback>",
  "selectedItemBuilder": "<DropdownButtonBuilder> || <JsonWidgetData>",
  "validators": "<List<ValueValidator>>",
  "style": "<TextStyle>",
  "value": "<dynamic>"
}

As a note, the FocusNode, ValueChanged, FormFieldSetter, and VoidCallback objects cannot be decoded via JSON. Instead, the only way to bind those values to the builder is to use a function or a variable reference via the JsonWidgetRegistry.

The selectedItemBuilder can be a DropdownButtonBuilder or JsonWidgetData. The DropdownButtonBuilder cannot be decoded via JSON and would have to be bound via the JsonWidgetRegistry. If a JsonWidgetData object is used then the item's display will be bound to the dropdown_item_display variable on the registry and the item's value will be bound to dropdown_item_value. This way the widget can associate properties to those two keys for proper rendering.

The items can be either a string array where the string will be used for both the display and the value or a map of strings to values where the key will be used as the display and the value will be the internal dropdown value.

If the map is null then null will be returned.

See also:

Implementation

static JsonDropdownButtonFormFieldBuilderModel? maybeFromDynamic(
  dynamic map, {
  Map<String, dynamic> args = const {},
  JsonWidgetRegistry? registry,
}) {
  JsonDropdownButtonFormFieldBuilderModel? result;

  if (map != null) {
    if (map is String) {
      map = yaon.parse(
        map,
        normalize: true,
      );
    }

    result = JsonDropdownButtonFormFieldBuilderModel(
      args,
      alignment: ThemeDecoder.decodeAlignment(
        map['alignment'],
        validate: false,
      ),
      autofocus: JsonClass.maybeParseBool(map['autofocus']),
      autovalidateMode: map['autovalidate'] == null
          ? ThemeDecoder.decodeAutovalidateMode(
              map['autovalidateMode'],
              validate: false,
            )
          : JsonClass.maybeParseBool(map['autovalidate']) == true
              ? AutovalidateMode.always
              : AutovalidateMode.disabled,
      borderRadius: ThemeDecoder.decodeBorderRadius(
        map['borderRadius'],
        validate: false,
      ),
      decoration: map['decoration'],
      disabledHint: JsonWidgetData.maybeFromDynamic(map['disabledHint']),
      dropdownColor: ThemeDecoder.decodeColor(
        map['dropdownColor'],
        validate: false,
      ),
      elevation: JsonClass.maybeParseInt(map['elevation']) ?? 8,
      enableFeedback: JsonClass.parseBool(
        map['enableFeedback'],
        whenNull: true,
      ),
      enabled: map['enabled'] == null
          ? true
          : JsonClass.maybeParseBool(map['enabled']),
      hint: JsonWidgetData.maybeFromDynamic(map['hint']),
      icon: JsonWidgetData.maybeFromDynamic(map['icon']),
      items: map['items'],
      iconDisabledColor: ThemeDecoder.decodeColor(
        map['iconDisabledColor'],
        validate: false,
      ),
      iconEnabledColor: ThemeDecoder.decodeColor(
        map['iconEnabledColor'],
        validate: false,
      ),
      iconSize: JsonClass.maybeParseDouble(map['iconSize'], 24.0),
      isDense: JsonClass.parseBool(
        map['isDense'],
        whenNull: true,
      ),
      isExpanded: JsonClass.maybeParseBool(map['isExpanded']),
      itemHeight: JsonClass.maybeParseDouble(map['itemHeight']),
      menuMaxHeight: JsonClass.maybeParseDouble(map['menuMaxHeight']),
      onChanged: map['onChanged'],
      onSaved: map['onSaved'],
      onTap: map['onTap'],
      selectedItemBuilder: map['selectedItemBuilder'],
      style: ThemeDecoder.decodeTextStyle(
        map['style'],
        validate: false,
      ),
      validator: map['validators'] == null
          ? null
          : Validator.fromDynamic({'validators': map['validators']}),
      value: map['value'],
    );
  }

  return result;
}