Files
pipelines/app/lib/features/chat/conversation/chat_question_card_fields.dart
alan 9551eceaed Complete Material-only APIs and fixtures
Remove obsolete control-family flags and icon metadata across all callers, dead alternate themes and host helpers, obsolete test setup, and the unused icon-font dependency. Preserve native Material Back transitions. Make navigation fixtures independent of update plugins and answer auxiliary shell snapshots so the complete application suite can run deterministically.
2026-11-11 21:55:39 +00:00

149 lines
4.8 KiB
Dart

import 'package:flutter/material.dart';
import '../../../core/api_gateway.dart';
import 'chat_card_styles.dart';
import 'chat_question_card_model.dart';
import 'chat_question_option_controls.dart';
/// A free-text input is part of a field when the form allows an answer outside
/// its option list, or when the field has no options at all.
bool chatQuestionFieldNeedsInput(JsonMap field) =>
field['custom'] == true || chatQuestionOptions(field).isEmpty;
/// The editable controls for one form. [onChanged] receives the protocol value
/// (`false`/`0` preserved), or null when the input was cleared.
List<Widget> chatQuestionFieldWidgets({
required BuildContext context,
required List<JsonMap> fields,
required Map<String, dynamic> answers,
required Map<String, TextEditingController> controllers,
required void Function(String key, Object? value) onChanged,
}) {
final widgets = <Widget>[];
for (final field in fields) {
if (!chatFormFieldVisible(field, answers)) continue;
final key = '${field['key'] ?? ''}'.trim();
if (key.isEmpty) continue;
widgets.add(
_field(
context: context,
field: field,
keyName: key,
answers: answers,
controllers: controllers,
onChanged: onChanged,
),
);
}
return widgets;
}
Widget _field({
required BuildContext context,
required JsonMap field,
required String keyName,
required Map<String, dynamic> answers,
required Map<String, TextEditingController> controllers,
required void Function(String key, Object? value) onChanged,
}) {
final title = '${field['title'] ?? ''}'.trim();
final description = '${field['description'] ?? ''}'.trim();
final label = title.isNotEmpty
? title
: (description.isNotEmpty ? description : keyName);
return Padding(
padding: const EdgeInsets.only(top: 12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.min,
children: [
Text(label, style: chatCardLabelStyle(context)),
if (description.isNotEmpty && description != title)
Text(description, style: chatCardSecondaryStyle(context)),
if ('${field['type'] ?? ''}' == 'external')
_external(context, field)
else if ('${field['type'] ?? ''}' == 'boolean')
chatQuestionBooleanField(
context: context,
field: field,
keyName: keyName,
answers: answers,
onChanged: onChanged,
)
else ...[
chatQuestionOptionsField(
context: context,
field: field,
keyName: keyName,
answers: answers,
onChanged: onChanged,
),
if (chatQuestionFieldNeedsInput(field))
_input(
context: context,
field: field,
label: label,
keyName: keyName,
controller: controllers[keyName]!,
hasOptions: chatQuestionOptions(field).isNotEmpty,
onChanged: (value) => onChanged(
keyName,
chatQuestionTextValue(field, value, answers),
),
),
],
],
),
);
}
Widget _input({
required BuildContext context,
required JsonMap field,
required String label,
required String keyName,
required TextEditingController controller,
required bool hasOptions,
required ValueChanged<String> onChanged,
}) {
final placeholder = '${field['placeholder'] ?? ''}'.trim().isNotEmpty
? '${field['placeholder']}'.trim()
: (hasOptions ? 'Type another answer' : 'Your answer');
final secret = field['secret'] == true;
final type = '${field['type'] ?? ''}';
final format = '${field['format'] ?? ''}';
final keyboard = type == 'number' || type == 'integer'
? TextInputType.number
: format == 'email'
? TextInputType.emailAddress
: format == 'uri'
? TextInputType.url
: format == 'date'
? TextInputType.datetime
: TextInputType.text;
// The visual label sits above the input; the same words must also reach the
// input's own semantics node, as the web's `aria-label` does.
final input = TextField(
key: ValueKey<String>('chat-question-input-$keyName'),
controller: controller,
obscureText: secret,
keyboardType: keyboard,
decoration: InputDecoration(hintText: placeholder),
onChanged: onChanged,
);
return Semantics(label: label, child: input);
}
Widget _external(BuildContext context, JsonMap field) {
final raw = '${field['url'] ?? ''}'.trim();
if (!chatExternalUrlIsSafe(raw)) {
return Text(
'This form has an unsupported external URL.',
style: chatCardBodyStyle(
context,
)?.copyWith(color: chatCardErrorColor(context)),
);
}
return SelectableText(raw);
}