509d4c624f
A question's text, its description and its options' descriptions now use the transcript's markdown renderer (bold, code, lists, links) in the card's own text styles; tapping an option's formatted description still selects it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
92 lines
3.2 KiB
Dart
92 lines
3.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../../../core/api_gateway.dart';
|
|
import 'chat_question_card_model.dart';
|
|
import 'chat_question_markdown.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<String, dynamic> 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() : <String>{})
|
|
: <String>{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
|
|
: ChatQuestionMarkdown('${option['description']}'.trim()),
|
|
contentPadding: EdgeInsets.zero,
|
|
controlAffinity: ListTileControlAffinity.leading,
|
|
onChanged: (on) => onChanged(
|
|
keyName,
|
|
_toggle(selected, chatQuestionOptionValue(option)),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
return RadioGroup<String>(
|
|
groupValue: value is String ? value : null,
|
|
onChanged: (next) => onChanged(keyName, next),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
for (final option in options)
|
|
RadioListTile<String>(
|
|
value: chatQuestionOptionValue(option),
|
|
title: Text(chatQuestionOptionLabel(option)),
|
|
subtitle: '${option['description'] ?? ''}'.trim().isEmpty
|
|
? null
|
|
: ChatQuestionMarkdown('${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<String, dynamic> answers,
|
|
required void Function(String key, Object? value) onChanged,
|
|
}) {
|
|
final value = answers[keyName];
|
|
return DropdownButtonFormField<String>(
|
|
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<String> _toggle(Set<String> selected, String value) {
|
|
final next = {...selected};
|
|
if (!next.add(value)) next.remove(value);
|
|
return next.toList();
|
|
}
|