buildComponent method

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

Implementation

@protected
Widget buildComponent(BuildParameters params) {
  final spec = params.spec;
  WidgetNodeSpec? componentSpec;
  final componentId = spec.props["component"];
  if (componentId != null) {
    componentSpec = Schema.getComponent(componentId, newId: params.id);
  }
  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
  var stateKeys = [...params.state.keys];
  for (var key in stateKeys) {
    if (key is String &&
        key.length == 36 &&
        key.contains('-') &&
        !key.contains(' ')) {
      params.state.remove(key);
    }
  }

  // format: <widgetId>.<property type>.<property key>
  final exposedProps = Map<String, dynamic>.from(
      componentSpec.extra["exposedProperties"] ?? {});
  for (var 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(".");
    stateKey = stateKey.replaceAll(componentId, params.id);

    // To avoid inheriting this property from previous component build
    params.state.remove(stateKey);

    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 evaluatorContext = params.parentContext ?? {};
  evaluatorContext["component"] = params.props["value"];

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