Skip to content

Commit 3876c0a

Browse files
committed
compose [nfc]: Use controller's error list in send button
1 parent 8c74a45 commit 3876c0a

File tree

1 file changed

+21
-39
lines changed

1 file changed

+21
-39
lines changed

lib/widgets/compose_box.dart

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -574,67 +574,49 @@ class _StreamSendButton extends StatefulWidget {
574574
}
575575

576576
class _StreamSendButtonState extends State<_StreamSendButton> {
577-
late List<TopicValidationError> _topicValidationErrors;
578-
late List<ContentValidationError> _contentValidationErrors;
579-
580-
_topicChanged() {
581-
final oldIsEmpty = _topicValidationErrors.isEmpty;
582-
final newErrors = widget.topicController.validationErrors;
583-
final newIsEmpty = newErrors.isEmpty;
584-
_topicValidationErrors = newErrors;
585-
if (oldIsEmpty != newIsEmpty) {
586-
setState(() {
587-
// Update disabled/non-disabled state
588-
});
589-
}
590-
}
591-
592-
_contentChanged() {
593-
final oldIsEmpty = _contentValidationErrors.isEmpty;
594-
final newErrors = widget.contentController.validationErrors;
595-
final newIsEmpty = newErrors.isEmpty;
596-
_contentValidationErrors = newErrors;
597-
if (oldIsEmpty != newIsEmpty) {
598-
setState(() {
599-
// Update disabled/non-disabled state
600-
});
601-
}
577+
_hasErrorsChanged() {
578+
setState(() {
579+
// Update disabled/non-disabled state
580+
});
602581
}
603582

604583
@override
605584
void initState() {
606585
super.initState();
607-
_topicValidationErrors = widget.topicController.validationErrors;
608-
_contentValidationErrors = widget.contentController.validationErrors;
609-
widget.topicController.addListener(_topicChanged);
610-
widget.contentController.addListener(_contentChanged);
586+
widget.topicController.hasValidationErrors.addListener(_hasErrorsChanged);
587+
widget.contentController.hasValidationErrors.addListener(_hasErrorsChanged);
611588
}
612589

613590
@override
614591
void didUpdateWidget(covariant _StreamSendButton oldWidget) {
615592
super.didUpdateWidget(oldWidget);
616593
if (widget.topicController != oldWidget.topicController) {
617-
oldWidget.topicController.removeListener(_topicChanged);
618-
widget.topicController.addListener(_topicChanged);
594+
oldWidget.topicController.hasValidationErrors.removeListener(_hasErrorsChanged);
595+
widget.topicController.hasValidationErrors.addListener(_hasErrorsChanged);
619596
}
620597
if (widget.contentController != oldWidget.contentController) {
621-
oldWidget.contentController.removeListener(_contentChanged);
622-
widget.contentController.addListener(_contentChanged);
598+
oldWidget.contentController.hasValidationErrors.removeListener(_hasErrorsChanged);
599+
widget.contentController.hasValidationErrors.addListener(_hasErrorsChanged);
623600
}
624601
}
625602

626603
@override
627604
void dispose() {
628-
widget.topicController.removeListener(_topicChanged);
629-
widget.contentController.removeListener(_contentChanged);
605+
widget.topicController.hasValidationErrors.removeListener(_hasErrorsChanged);
606+
widget.contentController.hasValidationErrors.removeListener(_hasErrorsChanged);
630607
super.dispose();
631608
}
632609

610+
bool get _hasValidationErrors {
611+
return widget.topicController.hasValidationErrors.value
612+
|| widget.contentController.hasValidationErrors.value;
613+
}
614+
633615
void _showSendFailedDialog(BuildContext context) {
634616
List<String> validationErrorMessages = [
635-
for (final error in _topicValidationErrors)
617+
for (final error in widget.topicController.validationErrors)
636618
error.message(),
637-
for (final error in _contentValidationErrors)
619+
for (final error in widget.contentController.validationErrors)
638620
error.message(),
639621
];
640622

@@ -645,7 +627,7 @@ class _StreamSendButtonState extends State<_StreamSendButton> {
645627
}
646628

647629
void _handleSendPressed(BuildContext context) {
648-
if (_topicValidationErrors.isNotEmpty || _contentValidationErrors.isNotEmpty) {
630+
if (_hasValidationErrors) {
649631
_showSendFailedDialog(context);
650632
return;
651633
}
@@ -663,7 +645,7 @@ class _StreamSendButtonState extends State<_StreamSendButton> {
663645
Widget build(BuildContext context) {
664646
ColorScheme colorScheme = Theme.of(context).colorScheme;
665647

666-
bool disabled = _topicValidationErrors.isNotEmpty || _contentValidationErrors.isNotEmpty;
648+
final disabled = _hasValidationErrors;
667649

668650
// Copy FilledButton defaults (_FilledButtonDefaultsM3.backgroundColor)
669651
final backgroundColor = disabled

0 commit comments

Comments
 (0)