151 lines
5.1 KiB
Dart
151 lines
5.1 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../../core/api_gateway.dart';
|
|
import 'chat_card_styles.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<String, dynamic> answers,
|
|
required bool cupertino,
|
|
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 (cupertino) {
|
|
return Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
for (final option in options)
|
|
CupertinoButton(
|
|
padding: const EdgeInsets.symmetric(vertical: 4),
|
|
onPressed: () => onChanged(
|
|
keyName,
|
|
multi
|
|
? _toggle(selected, chatQuestionOptionValue(option))
|
|
: chatQuestionOptionValue(option),
|
|
),
|
|
child: Row(
|
|
children: [
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(chatQuestionOptionLabel(option)),
|
|
if ('${option['description'] ?? ''}'.trim().isNotEmpty)
|
|
Text(
|
|
'${option['description']}'.trim(),
|
|
style: chatCardSecondaryStyle(context, cupertino),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
if (selected.contains(chatQuestionOptionValue(option)))
|
|
const Icon(CupertinoIcons.checkmark),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
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<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
|
|
: Text('${option['description']}'.trim()),
|
|
contentPadding: EdgeInsets.zero,
|
|
controlAffinity: ListTileControlAffinity.leading,
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Yes/No for a boolean field: native checkmarks under Cupertino, the web's
|
|
/// explicit "Choose Yes or No" menu under Material.
|
|
Widget chatQuestionBooleanField({
|
|
required BuildContext context,
|
|
required JsonMap field,
|
|
required String keyName,
|
|
required Map<String, dynamic> answers,
|
|
required bool cupertino,
|
|
required void Function(String key, Object? value) onChanged,
|
|
}) {
|
|
final value = answers[keyName];
|
|
if (cupertino) {
|
|
return Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
for (final entry in const [('Yes', true), ('No', false)])
|
|
CupertinoButton(
|
|
padding: const EdgeInsets.symmetric(vertical: 4),
|
|
onPressed: () => onChanged(keyName, entry.$2),
|
|
child: Row(
|
|
children: [
|
|
Expanded(child: Text(entry.$1)),
|
|
if (value == entry.$2) const Icon(CupertinoIcons.checkmark),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
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();
|
|
}
|