variableChooser function

Widget variableChooser(
  1. String label,
  2. List<String> inputs,
  3. String selected,
  4. WidgetRef ref,
  5. StateProvider stateProvider, {
  6. required bool enabled,
  7. required String tooltip,
  8. dynamic onChanged(
    1. String?
    )?,
})

Implementation

Widget variableChooser(
  String label,
  List<String> inputs,
  String selected,
  WidgetRef ref,
  StateProvider stateProvider, {
  // Add this parameter to control if the dropdown is enabled.
  required bool enabled,
  required String tooltip,
  // Add a callback for onChanged to handle custom logic.
  Function(String?)? onChanged,
}) {
  return MarkdownTooltip(
    message: tooltip,
    child: DropdownMenu(
      label: Text(label),
      width: 200,
      initialSelection: selected,
      dropdownMenuEntries: inputs.map((s) {
        return DropdownMenuEntry(value: s, label: s);
      }).toList(),

      // Use the enabled parameter to control the dropdown state.

      enabled: enabled,
      onSelected: (String? value) {
        if (enabled) {
          ref.read(stateProvider.notifier).state = value ?? 'IMPOSSIBLE';
          if (onChanged != null) {
            // Call the custom callback if provided.

            onChanged(value);
          }
        }
      },

      // Add a custom style for when it's disabled.

      textStyle: TextStyle(
        // Set grey when disabled.

        color: enabled ? Colors.black : Colors.grey,
      ),
    ),
  );
}