buildSelect method

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

Implementation

@protected
Widget buildSelect(BuildParameters params) {
  final props = params.props;
  var value = props["value"];
  final stateClone = params.state.clone();
  final valueKey = props["valueKey"] ?? "id";
  final textKey = props["textKey"] ?? valueKey;
  final alias = props["alias"] ?? params.id;
  final controller = props["controller"] ?? TextEditingController();
  props["controller"] = controller;
  props["onSaved"] = (v) => params.state[alias] = value;

  final headerSpec = params.widgets["dialogHeader"];
  final listTileSpec = params.widgets["dialogListTile"] ??
      {
        "_id": "${params.id}_listTile",
        "_type": "text",
        "properties": {
          "alias": textKey,
          "margin": 10,
        },
      };
  final separatorSpec = {
    "_id": "${params.id}_listSeparator",
    "_type": "container",
    "properties": {
      "height": 1,
      "decoration": {"color": "#33888888"},
    },
  };
  final listSpec = params.widgets["dialogList"] ??
      {
        "_id": "${params.id}_listView",
        "_type": "listView",
        "actions": {
          "loadPage": params.actions["loadData"],
        },
        "widgets": {
          "child": listTileSpec,
          "separator": separatorSpec,
        }
      };
  if (listSpec["actions"] == null) {
    listSpec["actions"] = <String, dynamic>{};
  }
  if (listSpec["actions"]["loadPage"] == null) {
    listSpec["actions"]["loadPage"] = params.actions["loadData"];
  }

  setValue(BuildContext context, v) {
    value = v is Map ? v[valueKey] : v;
    controller.text = v is Map ? v[textKey] : v?.toString() ?? "";
    Navigator.of(context).pop();
  }

  if (!parseBool(props["readOnly"])) {
    params.actions["onTap"] = () async {
      showDialog(
        context: params.context,
        useSafeArea: true,
        barrierDismissible: true,
        builder: (context) {
          listSpec["actions"]["onSelect"] = (v) => setValue(context, v);

          final content = Container(
            constraints: BoxConstraints(
              maxWidth:
                  parseDouble(props["dialogMaxWidth"], defaultValue: 350),
              maxHeight:
                  parseDouble(props["dialogMaxHeight"], defaultValue: 550),
            ),
            child: LocalBlocWidget(
              (context, state) => Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  if (props["dialogTitle"] != null)
                    Padding(
                        padding: const EdgeInsets.fromLTRB(10, 10, 10, 20),
                        child: Text(
                          Strings.getCapitalized(props["dialogTitle"]),
                          style: DialogTheme.of(context).titleTextStyle ??
                              Theme.of(context).textTheme.titleLarge!,
                        )),
                  if (headerSpec != null)
                    builder.buildWidget(context, headerSpec, stateClone,
                        params.parentContext),
                  Expanded(
                      child: builder.buildWidget(context, listSpec,
                          stateClone, params.parentContext)),
                  Padding(
                      padding: const EdgeInsets.all(10),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          TextButton(
                              onPressed: () => Navigator.of(context).pop(),
                              child: Text(Strings.getCapitalized("cancel"))),
                          TextButton(
                              onPressed: () => setValue(context, null),
                              child: Text(Strings.getCapitalized("clear"))),
                        ],
                      )),
                ],
              ),
            ),
          );
          return Dialog(child: content);
        },
      );
    };
  }
  final widget = buildTextFormField(params);

  final loadValueSpec = params.actions["loadValue"];
  if (value == null || loadValueSpec == null) {
    controller.text = "";
    return widget;
  }

  final bloc = events.createListBloc();
  return BlocConsumer<ListBloc, BaseState>(
    bloc: bloc,
    listener: (context, state) {},
    buildWhen: (oldState, newState) {
      return oldState != newState &&
          (newState is InitialState || newState is PageLoadedState);
    },
    builder: (context, state) {
      if (state is InitialState) {
        events.executePageLoadAction(context, bloc, 1, 1, [], loadValueSpec,
            stateClone, params.parentContext,
            value: value);
      } else if (state is PageLoadedState) {
        if (state.fullData.isNotEmpty) {
          final v = state.fullData.first;
          controller.text = v is Map ? v[textKey] : v?.toString() ?? "";
        }
      }
      return widget;
    },
  );
}