diff --git a/example/lib/main.dart b/example/lib/main.dart index 104cb60..5245259 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -206,6 +206,12 @@ class _MyHomePageState extends State { _multiSelectKey.currentState.validate(); }, ), + errorDecoration: BoxDecoration( + border: Border.all( + color: Theme.of(context).colorScheme.error, + width: 2, + ), + ), ), SizedBox(height: 40), //################################################################################################ diff --git a/lib/bottom_sheet/multi_select_bottom_sheet_field.dart b/lib/bottom_sheet/multi_select_bottom_sheet_field.dart index ff11320..8dbcd18 100644 --- a/lib/bottom_sheet/multi_select_bottom_sheet_field.dart +++ b/lib/bottom_sheet/multi_select_bottom_sheet_field.dart @@ -11,6 +11,9 @@ class MultiSelectBottomSheetField extends FormField> { /// Style the Container that makes up the field. final BoxDecoration? decoration; + /// Style the Container with error state. + final BoxDecoration? errorDecoration; + /// Set text that is displayed on the button. final Text? buttonText; @@ -120,6 +123,7 @@ class MultiSelectBottomSheetField extends FormField> { this.buttonIcon, this.listType, this.decoration, + this.errorDecoration, this.onSelectionChanged, this.chipDisplay, this.initialValue = const [], @@ -160,6 +164,7 @@ class MultiSelectBottomSheetField extends FormField> { _MultiSelectBottomSheetFieldView( items: items, decoration: decoration, + errorDecoration: errorDecoration, unselectedColor: unselectedColor, colorator: colorator, itemsTextStyle: itemsTextStyle, @@ -199,6 +204,7 @@ class MultiSelectBottomSheetField extends FormField> { // ignore: must_be_immutable class _MultiSelectBottomSheetFieldView extends StatefulWidget { final BoxDecoration? decoration; + final BoxDecoration? errorDecoration; final Text? buttonText; final Icon? buttonIcon; final List> items; @@ -239,6 +245,7 @@ class _MultiSelectBottomSheetFieldView extends StatefulWidget { this.buttonIcon, this.listType, this.decoration, + this.errorDecoration, this.onSelectionChanged, this.onConfirm, this.chipDisplay, @@ -276,6 +283,7 @@ class _MultiSelectBottomSheetFieldView extends StatefulWidget { buttonIcon = field.buttonIcon, listType = field.listType, decoration = field.decoration, + errorDecoration = field.errorDecoration, onSelectionChanged = field.onSelectionChanged, onConfirm = field.onConfirm, chipDisplay = field.chipDisplay, @@ -439,6 +447,31 @@ class __MultiSelectBottomSheetFieldViewState _selectedItems = myVar!; } + _buildContainerDecoration() { + final hasError = widget.state?.hasError ?? false; + final decoration = widget.decoration ?? + BoxDecoration( + border: Border( + bottom: BorderSide( + color: hasError + ? Colors.red.shade800.withOpacity(0.6) + : _selectedItems.isNotEmpty + ? (widget.selectedColor != null && + widget.selectedColor != Colors.transparent) + ? widget.selectedColor! + : Theme.of(context).primaryColor + : Colors.black45, + width: _selectedItems.isNotEmpty + ? hasError + ? 1.4 + : 1.8 + : 1.2, + ), + ), + ); + return hasError ? widget.errorDecoration ?? decoration : decoration; + } + @override Widget build(BuildContext context) { return Column( @@ -449,29 +482,7 @@ class __MultiSelectBottomSheetFieldViewState _showBottomSheet(context); }, child: Container( - decoration: widget.state != null - ? widget.decoration ?? - BoxDecoration( - border: Border( - bottom: BorderSide( - color: widget.state != null && widget.state!.hasError - ? Colors.red.shade800.withOpacity(0.6) - : _selectedItems.isNotEmpty - ? (widget.selectedColor != null && - widget.selectedColor != - Colors.transparent) - ? widget.selectedColor! - : Theme.of(context).primaryColor - : Colors.black45, - width: _selectedItems.isNotEmpty - ? (widget.state != null && widget.state!.hasError) - ? 1.4 - : 1.8 - : 1.2, - ), - ), - ) - : widget.decoration, + decoration: _buildContainerDecoration(), padding: EdgeInsets.all(10), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, diff --git a/lib/dialog/multi_select_dialog_field.dart b/lib/dialog/multi_select_dialog_field.dart index b229a4d..b2a39c6 100644 --- a/lib/dialog/multi_select_dialog_field.dart +++ b/lib/dialog/multi_select_dialog_field.dart @@ -14,6 +14,9 @@ class MultiSelectDialogField extends FormField> { /// Style the Container that makes up the field. final BoxDecoration? decoration; + /// Style the Container with error state. + final BoxDecoration? errorDecoration; + /// Set text that is displayed on the button. final Text? buttonText; @@ -114,6 +117,7 @@ class MultiSelectDialogField extends FormField> { this.buttonIcon, this.listType, this.decoration, + this.errorDecoration, this.onSelectionChanged, this.chipDisplay, this.searchable = false, @@ -156,6 +160,7 @@ class MultiSelectDialogField extends FormField> { buttonIcon: buttonIcon, chipDisplay: chipDisplay, decoration: decoration, + errorDecoration: errorDecoration, listType: listType, onConfirm: onConfirm, onSelectionChanged: onSelectionChanged, @@ -189,6 +194,7 @@ class MultiSelectDialogField extends FormField> { class _MultiSelectDialogFieldView extends StatefulWidget { final MultiSelectListType? listType; final BoxDecoration? decoration; + final BoxDecoration? errorDecoration; final Text? buttonText; final Icon? buttonIcon; final Widget? title; @@ -226,6 +232,7 @@ class _MultiSelectDialogFieldView extends StatefulWidget { this.buttonIcon, this.listType, this.decoration, + this.errorDecoration, this.onSelectionChanged, this.onConfirm, this.chipDisplay, @@ -261,6 +268,7 @@ class _MultiSelectDialogFieldView extends StatefulWidget { buttonIcon = field.buttonIcon, listType = field.listType, decoration = field.decoration, + errorDecoration = field.errorDecoration, onSelectionChanged = field.onSelectionChanged, onConfirm = field.onConfirm, chipDisplay = field.chipDisplay, @@ -416,6 +424,31 @@ class __MultiSelectDialogFieldViewState ); } + _buildContainerDecoration() { + final hasError = widget.state?.hasError ?? false; + final decoration = widget.decoration ?? + BoxDecoration( + border: Border( + bottom: BorderSide( + color: hasError + ? Colors.red.shade800.withOpacity(0.6) + : _selectedItems.isNotEmpty + ? (widget.selectedColor != null && + widget.selectedColor != Colors.transparent) + ? widget.selectedColor! + : Theme.of(context).primaryColor + : Colors.black45, + width: _selectedItems.isNotEmpty + ? hasError + ? 1.4 + : 1.8 + : 1.2, + ), + ), + ); + return hasError ? widget.errorDecoration ?? decoration : decoration; + } + @override Widget build(BuildContext context) { return Column( @@ -426,29 +459,7 @@ class __MultiSelectDialogFieldViewState _showDialog(context); }, child: Container( - decoration: widget.state != null - ? widget.decoration ?? - BoxDecoration( - border: Border( - bottom: BorderSide( - color: widget.state != null && widget.state!.hasError - ? Colors.red.shade800.withOpacity(0.6) - : _selectedItems.isNotEmpty - ? (widget.selectedColor != null && - widget.selectedColor != - Colors.transparent) - ? widget.selectedColor! - : Theme.of(context).primaryColor - : Colors.black45, - width: _selectedItems.isNotEmpty - ? (widget.state != null && widget.state!.hasError) - ? 1.4 - : 1.8 - : 1.2, - ), - ), - ) - : widget.decoration, + decoration: _buildContainerDecoration(), padding: const EdgeInsets.all(10), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween,