initializeCustomFormItemUIBuilders function

dynamic initializeCustomFormItemUIBuilders()

Implementation

initializeCustomFormItemUIBuilders() {
  formItemUIBuilders['location'] = (FormItemBuildEvent event) {
    BuildContext context = event.context;
    FormItemDelegate formItem = event.formItem;
    FormItemDelegateState? formState = event.formState;
    Function(VoidCallback)? setState = event.setState;

    if (isWeb(context)) {
      return const SizedBox.shrink();
    }

    return FormSection(
      label: "",
      child: Column(
        children: [
          formItem.values != null && formItem.values!.length == 4
              ? Column(
                  children: [
                    Text("${formItem.values![0]}, ${formItem.values![1]}"),
                    Text("${formItem.values![2]}, ${formItem.values![3]}"),
                  ],
                )
              : const SizedBox.shrink(),
          TextButton(
            style: TextButton.styleFrom(
                backgroundColor: Colors.transparent,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(18.0),
                    side: const BorderSide(color: Colors.black))),
            child: Text(formItem.values != null && formItem.values!.isNotEmpty
                ? "Change Region"
                : "Select Region"),
            onPressed: () {
              ContentRoutes.regionSelector.push(context: context).then((value) {
                if (value != null) {
                  setState!(() {
                    LatLngBounds bounds = value as LatLngBounds;
                    formItem.values = [];
                    formItem.values!.add(bounds.northeast.latitude.toString());
                    formItem.values!.add(bounds.northeast.longitude.toString());
                    formItem.values!.add(bounds.southwest.latitude.toString());
                    formItem.values!.add(bounds.southwest.longitude.toString());
                  });
                }
              });
            },
          ),
          ButtonBar(
            children: <Widget>[
              MaterialButton(
                onPressed: () {
                  setState!(() {
                    formState!.clearValue(formItem);
                  });
                },
                child: const Text("Clear"),
              ),
            ],
          )
        ],
      ),
    );
  };

  formItemUIBuilders['array'] = (FormItemBuildEvent event) {
    BuildContext context = event.context;
    FormItemDelegate formItem = event.formItem;
    FormItemDelegateState? formState = event.formState;
    Function(VoidCallback)? setState = event.setState;
    List<Widget> children = [];
    if (formItem.values != null) {
      for (var id in formItem.values!) {
        var item = formState!.getItem(id!);
        if (item != null) {
          children.add(buildItemWidget(context, item, onDismissed: (direction) {
            setState!(() {
              formItem.values!.remove(id);
            });
          }, onItemSelected: (item) => true));
        } else {
          children.add(EntityLoader(
            entityId: id,
            childBuilder: (context, item) {
              return buildItemWidget(context, item, onDismissed: (direction) {
                setState!(() {
                  formItem.values!.remove(id);
                });
              }, onItemSelected: (item) => true);
            },
          ));
        }
      }
    }

    children.add(OverflowBar(
      alignment: MainAxisAlignment.end,
      children: <Widget>[
        ElevatedButton(
          onPressed: () {
            showSearch(
              context: context,
              delegate: ContentSearchDelegate(formState,
                  builder: (query) => SearchFilter(
                      filterRef: formItem.property('search_filter')),
                  cellBuilder: defaultCellBuilder,
                  filter: formItem),
            );
          },
          child: Text("Add ${formItem.label}"),
        ),
        TextButton(
          onPressed: () {
            setState!(() {
              formState!.clearValue(formItem);
            });
          },
          child: const Text("Clear All"),
        ),
      ],
    ));
    return FormSection(
      label: formItem.label,
      child: Column(
        children: children,
      ),
    );
  };
}