buildSelect method

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

Implementation

@protected
Widget buildSelect(BuildParameters params) {
  var value = params.props["value"];
  final stateClone = params.state.clone();
  final valueKey = params.props["valueKey"] ?? "id";
  final textKey = params.props["textKey"] ?? valueKey;
  final alias = params.props["alias"] ?? params.id;
  final controller = TextEditingController();
  params.props["controller"] = controller;
  params.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(params.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(params.props["dialogMaxWidth"],
                  defaultValue: 350),
              maxHeight: parseDouble(params.props["dialogMaxHeight"],
                  defaultValue: 550),
            ),
            child: LocalBlocWidget(
              (context, state) => Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  if (params.props["dialogTitle"] != null)
                    Padding(
                        padding: const EdgeInsets.fromLTRB(10, 10, 10, 20),
                        child: Text(
                          Strings.getCapitalized(params.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;
    },
  );
}