buildComponent method

  1. @protected
Widget buildComponent(
  1. BuildParameters params
)

Implementation

@protected
Widget buildComponent(BuildParameters params) {
  final spec = params.spec;
  WidgetNodeSpec? componentSpec;
  if (spec.props["component"] != null) {
    componentSpec = Schema.getComponent(spec.props["component"]);
  }
  if (componentSpec == null) {
    return Container();
  }

  /// Using the same component multiple times in the same Screen may have issues related to Widget's Ids
  /// Maybe when building Component's widget tree, ids should be generated so they can be unique

  // format: <widgetId>.<property type>.<property key>
  final exposedProps = Map<String, dynamic>.from(
      componentSpec.extra["exposedProperties"] ?? {});
  for (String key in exposedProps.keys) {
    if (exposedProps[key] == null || exposedProps[key].isEmpty) {
      continue;
    }

    dynamic value;
    var keyParts = (exposedProps[key] as String).split(".");
    var propertyType = keyParts.removeAt(1);
    var stateKey = keyParts.join(".");
    params.state.remove(
        stateKey); // To avoid inheriting this property from previous component build

    switch (propertyType) {
      case "action":
        if (!spec.actions.containsKey(key)) {
          continue;
        }
        value = spec.actions[key];
        break;
      case "property":
        if (!spec.props.containsKey(key)) {
          continue;
        }
        value = spec.props[key];
        break;
      case "widget":
        if (!spec.widgets.containsKey(key)) {
          continue;
        }
        value = spec.widgets[key];
        break;
      default:
        continue;
    }

    params.state[stateKey] = value;
  }

  // final componentState = Map<String, dynamic>.from(spec.props["state"] ?? {});
  // params.state.addAll(componentState);
  // for (String key in spec.props.keys) {
  //   if (key.split(".").length == 2) {
  //     params.state[key] = spec.props[key];
  //   }
  // }
  // for (String key in spec.widgets.keys) {
  //   if (key.split(".").length == 2) {
  //     params.state[key] = spec.widgets[key];
  //   }
  // }
  // for (String key in spec.actions.keys) {
  //   if (key.split(".").length == 2) {
  //     params.state[key] = spec.actions[key];
  //   }
  // }

  return builder.buildWidgetFromSpec(
      params.context, componentSpec, params.state, params.parentContext);
}