SmartSearchDropdown<T, K> class
A convenient widget that combines search functionality with a dropdown overlay showing paginated results from a SmartPaginationCubit.
This widget provides a complete search-with-results solution that can be placed anywhere in your UI. The results dropdown automatically positions itself in the best available space.
Generic Types
T: The data type of items (e.g., Product, User)K: The key type used for identification (e.g., String, int)
Key-based Selection
When using key-based selection, you can:
- Set initial selection by key even before data loads
- Compare items by key instead of object equality
- Get notified of selected keys in addition to items
Example with key-based selection:
SmartSearchDropdown<Product, String>.withProvider(
request: PaginationRequest(page: 1, pageSize: 20),
provider: PaginationProvider.future((request) async {
return await api.searchProducts(request.searchQuery ?? '');
}),
searchRequestBuilder: (query) => PaginationRequest(...),
itemBuilder: (context, product) => ListTile(title: Text(product.name)),
keyExtractor: (product) => product.sku,
selectedKey: 'SKU-001',
selectedKeyLabelBuilder: (key) => 'Product: $key',
onKeySelected: (key) => print('Selected key: $key'),
)
Example with provider (creates cubit internally):
SmartSearchDropdown<Product, Product>.withProvider(
request: PaginationRequest(page: 1, pageSize: 20),
provider: PaginationProvider.future((request) async {
return await api.searchProducts(request.searchQuery ?? '');
}),
searchRequestBuilder: (query) => PaginationRequest(
page: 1,
pageSize: 20,
searchQuery: query,
),
itemBuilder: (context, product) => ListTile(
title: Text(product.name),
),
onItemSelected: (product) {
print('Selected: ${product.name}');
},
)
Example with showSelected mode:
SmartSearchDropdown<Product, String>.withProvider(
// ... other properties
showSelected: true,
selectedItemBuilder: (context, product, onClear) => ListTile(
title: Text(product.name),
trailing: IconButton(
icon: Icon(Icons.close),
onPressed: onClear,
),
),
)
Example with external cubit:
SmartSearchDropdown<Product, String>.withCubit(
cubit: productSearchCubit,
searchRequestBuilder: (query) => PaginationRequest(
page: 1,
pageSize: 20,
searchQuery: query,
),
itemBuilder: (context, product) => ListTile(
title: Text(product.name),
),
onItemSelected: (product) {
print('Selected: ${product.name}');
},
)
- Inheritance
-
- Object
- DiagnosticableTree
- Widget
- StatefulWidget
- SmartSearchDropdown
Constructors
-
SmartSearchDropdown.withCubit({Key? key, required SmartPaginationCubit<
T> cubit, required PaginationRequest searchRequestBuilder(String query), required Widget itemBuilder(BuildContext context, T item), ValueChanged<T> ? onItemSelected, ValueChanged<K> ? onKeySelected, SmartSearchConfig searchConfig = const SmartSearchConfig(), SmartSearchOverlayConfig overlayConfig = const SmartSearchOverlayConfig(), InputDecoration? decoration, TextStyle? style, Widget? prefixIcon, Widget? suffixIcon, bool showClearButton = true, BorderRadius? borderRadius, WidgetBuilder? loadingBuilder, WidgetBuilder? emptyBuilder, Widget errorBuilder(BuildContext context, Exception error)?, IndexedWidgetBuilder? separatorBuilder, WidgetBuilder? headerBuilder, BoxDecoration? overlayDecoration, bool showSelected = false, Widget selectedItemBuilder(BuildContext context, T item, VoidCallback onClear)?, Widget selectedKeyBuilder(BuildContext context, K key, VoidCallback onClear)?, T? initialSelectedValue, K? selectedKey, K keyExtractor(T item)?, String selectedKeyLabelBuilder(K key)?, String? validator(String?)?, TextInputAction textInputAction = TextInputAction.search, List<TextInputFormatter> ? inputFormatters, AutovalidateMode? autovalidateMode, ValueChanged<String> ? onChanged, int? maxLength, TextCapitalization textCapitalization = TextCapitalization.none, TextInputType keyboardType = TextInputType.text}) -
Creates a search dropdown with an external cubit.
const
-
SmartSearchDropdown.withProvider({Key? key, required PaginationRequest request, required PaginationProvider<
T> provider, required PaginationRequest searchRequestBuilder(String query), required Widget itemBuilder(BuildContext context, T item), ValueChanged<T> ? onItemSelected, ValueChanged<K> ? onKeySelected, SmartSearchConfig searchConfig = const SmartSearchConfig(), SmartSearchOverlayConfig overlayConfig = const SmartSearchOverlayConfig(), InputDecoration? decoration, TextStyle? style, Widget? prefixIcon, Widget? suffixIcon, bool showClearButton = true, BorderRadius? borderRadius, WidgetBuilder? loadingBuilder, WidgetBuilder? emptyBuilder, Widget errorBuilder(BuildContext context, Exception error)?, IndexedWidgetBuilder? separatorBuilder, WidgetBuilder? headerBuilder, BoxDecoration? overlayDecoration, bool showSelected = false, Widget selectedItemBuilder(BuildContext context, T item, VoidCallback onClear)?, Widget selectedKeyBuilder(BuildContext context, K key, VoidCallback onClear)?, T? initialSelectedValue, K? selectedKey, K keyExtractor(T item)?, String selectedKeyLabelBuilder(K key)?, String? validator(String?)?, TextInputAction textInputAction = TextInputAction.search, List<TextInputFormatter> ? inputFormatters, AutovalidateMode? autovalidateMode, ValueChanged<String> ? onChanged, int? maxLength, TextCapitalization textCapitalization = TextCapitalization.none, TextInputType keyboardType = TextInputType.text, ListBuilder<T> ? listBuilder, OnInsertionCallback<T> ? onInsertionCallback, int maxPagesInMemory = 5, Logger? logger, RetryConfig? retryConfig, Duration? dataAge, SortOrderCollection<T> ? orders}) -
Creates a search dropdown with an internal cubit.
const
Properties
- autovalidateMode → AutovalidateMode?
-
When to validate the input.
final
- borderRadius → BorderRadius?
-
Border radius for the search box.
final
- decoration → InputDecoration?
-
Decoration for the search text field.
final
- emptyBuilder → WidgetBuilder?
-
Builder for the empty state.
final
- errorBuilder → Widget Function(BuildContext context, Exception error)?
-
Builder for the error state.
final
-
Builder for a footer in the dropdown.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- headerBuilder → WidgetBuilder?
-
Builder for a header in the dropdown.
final
- initialSelectedValue → T?
-
The initially selected value.
final
-
inputFormatters
→ List<
TextInputFormatter> ? -
Input formatters to restrict or format input.
final
- itemBuilder → Widget Function(BuildContext context, T item)
-
Builder for each result item.
final
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
- keyboardType → TextInputType
-
The type of keyboard to display.
final
- keyExtractor → K Function(T item)?
-
Function to extract the key from an item.
final
- loadingBuilder → WidgetBuilder?
-
Builder for the loading state.
final
- maxLength → int?
-
Maximum length of the input.
final
-
onChanged
→ ValueChanged<
String> ? -
Called when the text changes.
final
-
onItemSelected
→ ValueChanged<
T> ? -
Called when an item is selected.
final
-
onKeySelected
→ ValueChanged<
K> ? -
Called when an item is selected by key.
final
- overlayConfig → SmartSearchOverlayConfig
-
Configuration for the overlay appearance.
final
- overlayDecoration → BoxDecoration?
-
Decoration for the overlay container.
final
- prefixIcon → Widget?
-
Prefix icon for the search box.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- searchConfig → SmartSearchConfig
-
Configuration for search behavior.
final
- searchRequestBuilder → PaginationRequest Function(String query)
-
Builds the pagination request for a search query.
final
- selectedItemBuilder → Widget Function(BuildContext context, T item, VoidCallback onClear)?
-
Builder for displaying the selected item when showSelected is true.
final
- selectedKey → K?
-
The initially selected key.
final
- selectedKeyBuilder → Widget Function(BuildContext context, K key, VoidCallback onClear)?
-
Builder for displaying the selected key when item is not yet loaded.
final
- selectedKeyLabelBuilder → String Function(K key)?
-
Function to build a display label for a key when the item is not yet loaded.
final
- separatorBuilder → IndexedWidgetBuilder?
-
Builder for separators between items.
final
- showClearButton → bool
-
Whether to show a clear button.
final
- showSelected → bool
-
Whether to show the selected item instead of the search box after selection.
final
- style → TextStyle?
-
Text style for the search input.
final
- suffixIcon → Widget?
-
Suffix icon for the search box.
final
- textCapitalization → TextCapitalization
-
Text capitalization behavior.
final
- textInputAction → TextInputAction
-
The action button on the keyboard (e.g., search, done, next).
final
- validator → String? Function(String?)?
-
Validator function for form validation.
final
Methods
-
createElement(
) → StatefulElement -
Creates a StatefulElement to manage this widget's location in the tree.
inherited
-
createState(
) → State< SmartSearchDropdown< T, K> > -
Creates the mutable state for this widget at a given location in the tree.
override
-
debugDescribeChildren(
) → List< DiagnosticsNode> -
Returns a list of DiagnosticsNode objects describing this node's
children.
inherited
-
debugFillProperties(
DiagnosticPropertiesBuilder properties) → void -
Add additional properties associated with the node.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toDiagnosticsNode(
{String? name, DiagnosticsTreeStyle? style}) → DiagnosticsNode -
Returns a debug representation of the object that is used by debugging
tools and by DiagnosticsNode.toStringDeep.
inherited
-
toString(
{DiagnosticLevel minLevel = DiagnosticLevel.info}) → String -
A string representation of this object.
inherited
-
toStringDeep(
{String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) → String -
Returns a string representation of this node and its descendants.
inherited
-
toStringShallow(
{String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a one-line detailed description of the object.
inherited
-
toStringShort(
) → String -
A short, textual description of this widget.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited