import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; import 'chat_question_card_model.dart'; /// Native option controls for one question field. Single-select uses the host's /// radio/checklist pattern, multi-select toggles choices, and `onChanged` /// receives the protocol value with `false`/`0` preserved. Widget chatQuestionOptionsField({ required BuildContext context, required JsonMap field, required String keyName, required Map answers, required void Function(String key, Object? value) onChanged, }) { final options = chatQuestionOptions(field); if (options.isEmpty) return const SizedBox.shrink(); final multi = '${field['type'] ?? ''}' == 'multiselect'; final value = answers[keyName]; final selected = multi ? (value is List ? value.map((entry) => '$entry').toSet() : {}) : {if (value is String) value}; if (multi) { return Column( mainAxisSize: MainAxisSize.min, children: [ for (final option in options) CheckboxListTile( value: selected.contains(chatQuestionOptionValue(option)), title: Text(chatQuestionOptionLabel(option)), subtitle: '${option['description'] ?? ''}'.trim().isEmpty ? null : Text('${option['description']}'.trim()), contentPadding: EdgeInsets.zero, controlAffinity: ListTileControlAffinity.leading, onChanged: (on) => onChanged( keyName, _toggle(selected, chatQuestionOptionValue(option)), ), ), ], ); } return RadioGroup( groupValue: value is String ? value : null, onChanged: (next) => onChanged(keyName, next), child: Column( mainAxisSize: MainAxisSize.min, children: [ for (final option in options) RadioListTile( value: chatQuestionOptionValue(option), title: Text(chatQuestionOptionLabel(option)), subtitle: '${option['description'] ?? ''}'.trim().isEmpty ? null : Text('${option['description']}'.trim()), contentPadding: EdgeInsets.zero, controlAffinity: ListTileControlAffinity.leading, ), ], ), ); } /// Yes/No for a boolean field: the web's explicit "Choose Yes or No" menu. Widget chatQuestionBooleanField({ required BuildContext context, required JsonMap field, required String keyName, required Map answers, required void Function(String key, Object? value) onChanged, }) { final value = answers[keyName]; return DropdownButtonFormField( initialValue: value is bool ? '$value' : null, hint: const Text('Choose Yes or No'), items: const [ DropdownMenuItem(value: 'true', child: Text('Yes')), DropdownMenuItem(value: 'false', child: Text('No')), ], onChanged: (next) => onChanged(keyName, chatFormFieldValue(field, next ?? '')), ); } List _toggle(Set selected, String value) { final next = {...selected}; if (!next.add(value)) next.remove(value); return next.toList(); }