recodeChooser method

Widget recodeChooser(
  1. dynamic inputs,
  2. dynamic selected2
)

Implementation

Widget recodeChooser(inputs, selected2) {
  final TextEditingController valCtrl = TextEditingController();
  valCtrl.text = ref.read(numberProvider.notifier).state.toString();

  bool isNumeric = true;

  // On startup with no dataset (so nothing selected), the default is to enable
  // all the chips.

  if (selected != 'NULL') {
    isNumeric = ref.read(typesProvider)[selected] == Type.numeric;
  }

  // TODO 20240819 gjw WHERE ARE THE TOOLTIPS?

  return Row(
    mainAxisAlignment: MainAxisAlignment.start, // Align children to the start

    spacing: configWidgetSpace,
    children: [
      configLeftGap,
      ChoiceChipTip(
        options: numericMethods,
        selectedOption: selectedTransform,
        enabled: isNumeric && selected != 'NULL',
        // Dynamic enabling based on the selected variable type
        tooltips: numericMethodsTooltips,
        onSelected: (String? selected) {
          setState(() {
            selectedTransform = selected ?? '';
          });
        },
      ),
      NumberField(
        label: 'Number',
        controller: valCtrl,
        stateProvider: numberProvider,
        validator: (value) => validateInteger(value, min: 1),
        inputFormatter: FilteringTextInputFormatter.digitsOnly,
        enabled: isNumeric && selected != 'NULL',
        tooltip: '''

        Set the number of bins to construct.

        ''',
      ),

      // As Categoric chip.

      ChoiceChipTip(
        options: asCategoricMethods,
        selectedOption: selectedTransform,
        tooltips: asCategoricMethodsTooltips,
        enabled: isNumeric && selected != 'NULL',
        onSelected: (String? selected) {
          setState(() {
            selectedTransform = selected ?? '';
          });
        },
      ),

      // As Numeric chip.

      ChoiceChipTip(
        options: asNumericMethods,
        selectedOption: selectedTransform,
        tooltips: asNumericMethodsTooltips,
        enabled: !isNumeric,
        onSelected: (String? selected) {
          setState(() {
            selectedTransform = selected ?? '';
          });
        },
      ),
      ChoiceChipTip(
        enabled: selected == 'NULL' || !isNumeric,
        options: categoricMethods,
        selectedOption: !isNumeric ? selectedTransform : '',
        tooltips: categoricMethodsTooltips,
        onSelected: (String? selected) {
          setState(() {
            selectedTransform = selected ?? '';

            // If "Join Categorics" is selected, filter inputs to only categoric types
            if (selectedTransform == 'Join Categorics') {
              inputs = inputs
                  .where(
                    (input) =>
                        ref.read(typesProvider)[input] == Type.categoric,
                  )
                  .toList();

              // Update selected and selected2 to ensure they are valid
              selected = inputs.isNotEmpty ? inputs.first : 'NULL';
              selected2 = inputs.length > 1 ? inputs[1] : 'NULL';

              ref.read(selectedProvider.notifier).state = selected!;
              ref.read(selected2Provider.notifier).state = selected2;
            }
          });
        },
      ),
    ],
  );
}