ActiveItemModel<T> constructor
- IdGenerator? idGenerator, {
- bool loop = false,
- bool activateFirstItemByDefault = true,
- List<
T> items = const [],
Creates an instance from the initial list of items
. If idGenerator
is
null, a default instance (SequentialIdGenerator) is created.
This is a convenience for components to create an ActiveItemModel as an
IdGenerator is not always bound in DI.
Note: While idGenerator
can be null, components should always pass
an injected instance of IdGenerator to ensure ActiveItemModel uses the
IdGenerator provided by the app.
If items
is null, it is set to an empty list.
loop
decides whether the active items loops from the end of the list to
the beginning of the list, and vise versa. Default is false.
Common a11y practice for select components, loop is set to false.
Reference: https://www.w3.org/TR/wai-aria-practices/#Listbox
Common a11y practice for menu components, loop is set to true.
Reference: https://www.w3.org/TR/wai-aria-practices/#menu
activateOnListChange
decides whether the first list item should
activate whenever list items change. Default is true.
If an item is active when the list changes and that item is in the new
list, the item will remain active regardless of what this flag is set to.
Auto-activating the first result may slightly speed up keyboard nav for
users. It can also be detremental to a11y in others. For example, to
avoid screen reader issues, multi-select comboboxes toggle list item
selection via
Implementation
ActiveItemModel(IdGenerator? idGenerator,
{bool loop = false,
this.activateFirstItemByDefault = true,
List<T> items = const []})
: _idGenerator = idGenerator ?? SequentialIdGenerator.fromUUID() {
_loop = loop;
_items = items;
if (_items.isNotEmpty) _activeIndex = activateFirstItemByDefault ? 0 : -1;
}