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'; 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 chatQuestionFieldWidgets({ required BuildContext context, required List fields, required Map answers, required bool cupertino, required Map controllers, required void Function(String key, Object? value) onChanged, }) { final widgets = []; 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, cupertino: cupertino, controllers: controllers, onChanged: onChanged, ), ); } return widgets; } Widget _field({ required BuildContext context, required JsonMap field, required String keyName, required Map answers, required bool cupertino, required Map 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, cupertino)), if (description.isNotEmpty && description != title) Text( description, style: chatCardSecondaryStyle(context, cupertino), ), if ('${field['type'] ?? ''}' == 'external') _external(context, field, cupertino) else if ('${field['type'] ?? ''}' == 'boolean') chatQuestionBooleanField( context: context, field: field, keyName: keyName, answers: answers, cupertino: cupertino, onChanged: onChanged, ) else ...[ chatQuestionOptionsField( context: context, field: field, keyName: keyName, answers: answers, cupertino: cupertino, onChanged: onChanged, ), if (chatQuestionFieldNeedsInput(field)) _input( context: context, field: field, label: label, keyName: keyName, controller: controllers[keyName]!, cupertino: cupertino, 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 cupertino, required bool hasOptions, required ValueChanged 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 = cupertino ? CupertinoTextField( key: ValueKey('chat-question-input-$keyName'), controller: controller, placeholder: placeholder, obscureText: secret, keyboardType: keyboard, padding: const EdgeInsets.all(8), onChanged: onChanged, ) : TextField( key: ValueKey('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, bool cupertino) { final raw = '${field['url'] ?? ''}'.trim(); if (!chatExternalUrlIsSafe(raw)) { return Text( 'This form has an unsupported external URL.', style: chatCardBodyStyle( context, cupertino, )?.copyWith(color: chatCardErrorColor(context, cupertino)), ); } return SelectableText(raw); }