Choice<T>.prompt constructor

Choice<T>.prompt({
  1. Key? key,
  2. List<T> value = const [],
  3. ValueChanged<List<T>>? onChanged,
  4. String? title,
  5. bool multiple = false,
  6. bool clearable = false,
  7. bool confirmation = false,
  8. bool loading = false,
  9. bool error = false,
  10. required int itemCount,
  11. required IndexedChoiceStateBuilder<T> itemBuilder,
  12. ChoiceGroupResolver? itemGroup,
  13. ChoiceSkipResolver<T>? itemSkip,
  14. ChoiceStateBuilder<T>? dividerBuilder,
  15. ChoiceStateBuilder<T>? leadingBuilder,
  16. ChoiceStateBuilder<T>? trailingBuilder,
  17. ChoiceStateBuilder<T>? placeholderBuilder,
  18. ChoiceStateBuilder<T>? errorBuilder,
  19. ChoiceStateBuilder<T>? loaderBuilder,
  20. ChoiceGroupSortResolver? groupSort,
  21. ChoiceGroupBuilder? groupBuilder,
  22. ChoiceGroupItemBuilder? groupItemBuilder,
  23. ChoiceGroupHeaderBuilder<T>? groupHeaderBuilder,
  24. ChoiceListBuilder? listBuilder,
  25. ChoiceStateBuilder<T>? modalHeaderBuilder,
  26. ChoiceStateBuilder<T>? modalFooterBuilder,
  27. ChoiceStateBuilder<T>? modalSeparatorBuilder,
  28. FlexFit modalFit = FlexFit.loose,
  29. ChoicePromptBuilder<T>? anchorBuilder,
  30. ChoicePromptDelegate<T>? promptDelegate,
  31. bool searchable = false,
  32. ValueSetter<String>? onSearch,
})

Create prompted choice widget with single or multiple selection

The value prop is the initial selection value

The onChanged prop called when the choice selection should change

The clearable prop determines whether the choice can be cleared

The multiple prop determines whether the choice is multiple or single selection

The confirmation prop specifies whether the choice selection needs to be confirmed

The searchable prop specifies whether the choice can be searched

The title prop is primary text of the modal and trigger widget

The itemCount prop is the total number of item, this choice list can provide

The itemSkip prop called to specify which indices to skip when building choice item

The itemBuilder prop called to build choice item

The dividerBuilder prop called to build divider item

The leadingBuilder prop called to build leading item of the item collection

The trailingBuilder prop called to build trailing item of the item collection

The placeholderBuilder prop called to build placeholder when there are no choice items

The listBuilder prop called to build the list of choice items

The modalBodyBuilder prop called to build modal body widget

The modalHeaderBuilder prop called to build modal header widget

The modalFooterBuilder prop called to build modal footer widget

The modalSeparatorBuilder prop called to build modal separator widget

The modalFit prop is how a flexible modal body is inscribed into the available space.

The anchorBuilder prop called to build trigger widget to invoke modal prompt

The promptDelegate prop called to specify ways to invoke modal prompt

A StatefulWidget that illustrates use of an Choice.prompt usage.

import 'package:flutter/material.dart';
import 'package:choice/choice.dart';

class PromptedPopupDialog extends StatefulWidget {
  const PromptedPopupDialog({super.key});

  @override
  State<PromptedPopupDialog> createState() => _PromptedPopupDialogState();
}

class _PromptedPopupDialogState extends State<PromptedPopupDialog> {
  List<String> choices = [
    'News',
    'Entertainment',
    'Politics',
    'Automotive',
    'Sports',
    'Education',
  ];
  String? selectedValue;

  void setSelectedValue(String? value) {
    setState(() => selectedValue = value);
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 300,
      child: Card(
        clipBehavior: Clip.antiAlias,
        child: Choice<String>.prompt(
          title: 'Category',
          value: selectedValue,
          onChanged: setSelectedValue,
          itemCount: choices.length,
          itemBuilder: (state, i) {
            return RadioListTile(
              value: choices[i],
              groupValue: state.single,
              onChanged: (value) {
                state.select(choices[i]);
              },
              title: ChoiceText(
                choices[i],
                highlight: state.search?.value,
              ),
            );
          },
          promptDelegate: ChoicePrompt.delegatePopupDialog(
            maxHeightFactor: .5,
            constraints: const BoxConstraints(maxWidth: 300),
          ),
          anchorBuilder: ChoiceAnchor.create(inline: true),
        ),
      ),
    );
  }
}

Implementation

Choice.prompt({
  super.key,
  this.value = const [],
  this.onChanged,
  this.title,
  this.multiple = false,
  this.clearable = false,
  this.confirmation = false,
  bool loading = false,
  bool error = false,
  required int itemCount,
  required IndexedChoiceStateBuilder<T> itemBuilder,
  ChoiceGroupResolver? itemGroup,
  ChoiceSkipResolver<T>? itemSkip,
  ChoiceStateBuilder<T>? dividerBuilder,
  ChoiceStateBuilder<T>? leadingBuilder,
  ChoiceStateBuilder<T>? trailingBuilder,
  ChoiceStateBuilder<T>? placeholderBuilder,
  ChoiceStateBuilder<T>? errorBuilder,
  ChoiceStateBuilder<T>? loaderBuilder,
  ChoiceGroupSortResolver? groupSort,
  ChoiceGroupBuilder? groupBuilder,
  ChoiceGroupItemBuilder? groupItemBuilder,
  ChoiceGroupHeaderBuilder<T>? groupHeaderBuilder,
  ChoiceListBuilder? listBuilder,
  ChoiceStateBuilder<T>? modalHeaderBuilder,
  ChoiceStateBuilder<T>? modalFooterBuilder,
  ChoiceStateBuilder<T>? modalSeparatorBuilder,
  FlexFit modalFit = FlexFit.loose,
  ChoicePromptBuilder<T>? anchorBuilder,
  ChoicePromptDelegate<T>? promptDelegate,
  bool searchable = false,
  ValueSetter<String>? onSearch,
}) : child = PromptedChoice<T>(
        value: value,
        onChanged: onChanged,
        title: title,
        multiple: multiple,
        clearable: clearable,
        confirmation: confirmation,
        loading: loading,
        error: error,
        itemCount: itemCount,
        itemBuilder: itemBuilder,
        itemGroup: itemGroup,
        itemSkip: itemSkip,
        dividerBuilder: dividerBuilder,
        leadingBuilder: leadingBuilder,
        trailingBuilder: trailingBuilder,
        placeholderBuilder: placeholderBuilder,
        errorBuilder: errorBuilder,
        loaderBuilder: loaderBuilder,
        groupSort: groupSort,
        groupBuilder: groupBuilder,
        groupItemBuilder: groupItemBuilder,
        groupHeaderBuilder: groupHeaderBuilder,
        listBuilder: listBuilder,
        modalHeaderBuilder: modalHeaderBuilder,
        modalFooterBuilder: modalFooterBuilder,
        modalSeparatorBuilder: modalSeparatorBuilder,
        modalFit: modalFit,
        anchorBuilder: anchorBuilder,
        promptDelegate: promptDelegate,
        searchable: searchable,
        onSearch: onSearch,
      );