96de9f5bea
Keep the Material-only conversation implementation while preserving public Cupertino parameters for callers. Narrow affected tests to Material hosts so the dead platform paths are no longer exercised.
205 lines
6.0 KiB
Dart
205 lines
6.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../../../core/api_gateway.dart';
|
|
import 'chat_card_styles.dart';
|
|
import 'chat_question_card_controls.dart';
|
|
import 'chat_question_card_fields.dart';
|
|
import 'chat_question_card_model.dart';
|
|
import 'chat_question_recorded_answers.dart';
|
|
import 'chat_question_validation.dart';
|
|
|
|
/// One recorded agent question as a native card.
|
|
///
|
|
/// A pending question keeps the operator's choices locally until Send answer;
|
|
/// the answer map is field-keyed, exactly as `chat_answer` expects. Resolved
|
|
/// and expired questions show the recorded answers read-only.
|
|
class ChatQuestionCard extends StatefulWidget {
|
|
const ChatQuestionCard({
|
|
super.key,
|
|
required this.form,
|
|
required this.onSubmit,
|
|
this.error,
|
|
});
|
|
|
|
final JsonMap form;
|
|
final Future<void> Function(Map<String, dynamic> answers, String action)
|
|
onSubmit;
|
|
final String? error;
|
|
|
|
@override
|
|
State<ChatQuestionCard> createState() => _ChatQuestionCardState();
|
|
}
|
|
|
|
class _ChatQuestionCardState extends State<ChatQuestionCard> {
|
|
late final List<JsonMap> _fields = chatFormFields(widget.form);
|
|
late final Map<String, dynamic> _answers = _initialAnswers();
|
|
final Map<String, TextEditingController> _controllers = {};
|
|
String _localError = '';
|
|
bool _sending = false;
|
|
|
|
bool get _pending => chatFormPending(widget.form);
|
|
|
|
Map<String, dynamic> _initialAnswers() {
|
|
final recorded = widget.form['answers'];
|
|
final answers = recorded is Map
|
|
? Map<String, dynamic>.from(recorded)
|
|
: <String, dynamic>{};
|
|
for (final field in _fields) {
|
|
final key = '${field['key'] ?? ''}'.trim();
|
|
if (key.isEmpty || answers.containsKey(key)) continue;
|
|
if (field['default'] != null) answers[key] = field['default'];
|
|
}
|
|
return answers;
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
for (final field in _fields) {
|
|
if (!chatQuestionFieldNeedsInput(field)) continue;
|
|
final key = '${field['key'] ?? ''}'.trim();
|
|
if (key.isEmpty) continue;
|
|
final value = _answers[key];
|
|
final options = chatQuestionOptions(
|
|
field,
|
|
).map(chatQuestionOptionValue).toSet();
|
|
final custom = value is List
|
|
? value.where((entry) => !options.contains('$entry')).join(', ')
|
|
: (value is String && !options.contains(value) ? value : '');
|
|
final controller = TextEditingController(text: custom);
|
|
_controllers[key] = controller;
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
for (final controller in _controllers.values) {
|
|
controller.dispose();
|
|
}
|
|
super.dispose();
|
|
}
|
|
|
|
void _changed(String key, Object? value) {
|
|
setState(() {
|
|
if (value == null) {
|
|
_answers.remove(key);
|
|
} else {
|
|
_answers[key] = value;
|
|
}
|
|
_localError = '';
|
|
});
|
|
}
|
|
|
|
Future<void> _submit() async {
|
|
if (_sending || !_pending) return;
|
|
final reason = chatFormAnswerError(_fields, _answers);
|
|
if (reason.isNotEmpty) {
|
|
setState(() => _localError = reason);
|
|
return;
|
|
}
|
|
setState(() {
|
|
_sending = true;
|
|
_localError = '';
|
|
});
|
|
final accept =
|
|
widget.form['actions'] is List &&
|
|
(widget.form['actions'] as List).contains('accept');
|
|
await widget.onSubmit(
|
|
chatVisibleFormAnswers(_fields, _answers),
|
|
accept ? 'accept' : '',
|
|
);
|
|
if (mounted) setState(() => _sending = false);
|
|
}
|
|
|
|
Future<void> _action(String action) async {
|
|
if (_sending || !_pending) return;
|
|
setState(() {
|
|
_sending = true;
|
|
_localError = '';
|
|
});
|
|
await widget.onSubmit(const {}, action);
|
|
if (mounted) setState(() => _sending = false);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => Card(
|
|
// Keep consecutive cards visually separate; the transcript row only
|
|
// supplies the horizontal inset.
|
|
margin: const EdgeInsets.only(bottom: 12),
|
|
child: Padding(padding: const EdgeInsets.all(16), child: _body(context)),
|
|
);
|
|
|
|
Widget _body(BuildContext context) {
|
|
final actions = widget.form['actions'];
|
|
final error = widget.error?.trim().isNotEmpty == true
|
|
? widget.error!.trim()
|
|
: _localError;
|
|
return Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Text(
|
|
'${widget.form['title'] ?? ''}'.trim().isEmpty
|
|
? 'Question'
|
|
: '${widget.form['title']}'.trim(),
|
|
style: chatCardTitleStyle(context, false),
|
|
),
|
|
Text(_statusLine(), style: chatCardSecondaryStyle(context, false)),
|
|
if (error.isNotEmpty)
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 8),
|
|
child: Semantics(
|
|
liveRegion: true,
|
|
child: Text(
|
|
error,
|
|
style: chatCardBodyStyle(
|
|
context,
|
|
false,
|
|
)?.copyWith(color: chatCardErrorColor(context, false)),
|
|
),
|
|
),
|
|
),
|
|
if (_pending)
|
|
...chatQuestionFieldWidgets(
|
|
context: context,
|
|
fields: _fields,
|
|
answers: _answers,
|
|
cupertino: false,
|
|
controllers: _controllers,
|
|
onChanged: _changed,
|
|
)
|
|
else
|
|
...chatQuestionRecordedAnswers(
|
|
context: context,
|
|
form: widget.form,
|
|
cupertino: false,
|
|
),
|
|
if (_pending) ...[
|
|
const SizedBox(height: 12),
|
|
chatQuestionControls(
|
|
context: context,
|
|
cupertino: false,
|
|
actions: actions,
|
|
submitLabel: _submitLabel,
|
|
sending: _sending,
|
|
onSubmit: _submit,
|
|
onAction: _action,
|
|
),
|
|
],
|
|
],
|
|
);
|
|
}
|
|
|
|
String _statusLine() {
|
|
if (_pending) return chatQuestionWaiting;
|
|
return chatQuestionSettledStatus(widget.form);
|
|
}
|
|
|
|
String get _submitLabel {
|
|
final allExternal =
|
|
_fields.isNotEmpty &&
|
|
_fields.every((field) => '${field['type'] ?? ''}' == 'external');
|
|
return allExternal ? chatQuestionConfirm : chatQuestionSubmit;
|
|
}
|
|
}
|