buildCheckbox method

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

Implementation

@protected
Widget buildCheckbox(BuildParameters params) {
  final alias = params.props["alias"] ?? params.id;
  final enabled = parseBool(params.props["enabled"], defaultValue: true);
  final triState = parseBool(params.props["triState"]);
  bool? initialValue = triState
      ? tryParseBool(params.props["value"])
      : parseBool(params.props["value"]);
  final controlAffinity = params.props["direction"] == "leading"
      ? ListTileControlAffinity.leading
      : params.props["direction"] == "trailing"
          ? ListTileControlAffinity.trailing
          : ListTileControlAffinity.platform;

  Widget? title = builder.tryBuildWidget(params.context,
      params.widgets["titleWidget"], params.state, params.parentContext);
  title ??= params.props["title"] != null
      ? Text(properties.getText(params.props["title"], "titleMessage"))
      : null;
  Widget? subtitle = builder.tryBuildWidget(params.context,
      params.widgets["subtitleWidget"], params.state, params.parentContext);
  subtitle ??= params.props["subtitle"] != null
      ? Text(properties.getText(params.props["subtitle"], "titleMessage"))
      : null;

  final onChanged = events.getValueFunction<bool?>(params.context,
      params.actions["onChanged"], params.state, params.parentContext);
  final onSaved = params.props["onSaved"] ??
      (v) {
        if (alias != null) {
          params.state[alias] = v;
        }
      };

  return FormField<bool>(
    key:
        UniqueKey(), // otherwise on a ListView, FormField's state would override the real value
    initialValue: initialValue,
    enabled: enabled,
    autovalidateMode: AutovalidateMode.disabled,
    validator: builder.getCheckboxValidator(params.props),
    onSaved: onSaved,
    builder: (state) {
      return CheckboxListTile(
        key: properties.getKey(params.id),
        dense: state.hasError,
        value: state.value,
        tristate: triState,
        controlAffinity: controlAffinity,
        activeColor: tryParseColor(params.props["activeColor"]),
        checkColor: tryParseColor(params.props["checkColor"]),
        shape: params.buildProp("shape"),
        title: title,
        subtitle: state.hasError
            ? Text(properties.getText(state.errorText!, "errorMessage"),
                style: TextStyle(
                    color: Theme.of(params.context).colorScheme.error))
            : subtitle,
        secondary: builder.tryBuildWidget(params.context,
            params.widgets["secondary"], params.state, params.parentContext),
        contentPadding: params.buildProp("contentPadding"),
        onChanged: enabled
            ? (val) {
                state.didChange(val);
                if (onChanged != null) {
                  onChanged(val);
                }
              }
            : null,
      );
    },
  );
}