搜索插件、代码仓与 APP 分类

choice 封面
插件详情v2.3.2

choice

smart_select 和 chips_choice 的继任者,具有更简洁、更灵活且可组合的 API,用于创建支持单选或多选的内联或提示选择控件。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1.4万
喜欢数
143
Pub 分数
150

安装插件

flutter pub add choice

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

Choice 是一个现代的 Flutter 包,旨在构建灵活且可组合的选择控件,支持单选和多选。它取代了旧的 smart_select 和 chips_choice 等包,为内联或提示式选择界面提供了更简洁的 API。适用于表单、筛选器和基于标签的输入,可在不同平台上提供一致且可定制的体验。该包强调开发人员的使用便捷性和 UI 的灵活性,使集成到各种应用设计中变得轻松。

适合场景

  • 具有可选选项的表单输入
  • 过滤接口
  • 基于标签的数据输入
  • 动态选择列表
  • 用户偏好选择
  • 多选下拉菜单

主要特点

  • 简洁直观的 API
  • 支持单选和多选
  • 灵活的内联或提示布局
  • 高度可定制的外观
  • 跨平台兼容性
  • 可组合的组件设计

适合对象

  • 寻求现代替代方案以取代传统选择包的开发者
  • 需要动态、可重用选择组件的应用程序
  • 强调用户界面一致性和可维护性的项目
  • 优先考虑开发者体验和代码清晰度的团队
  • 具有复杂筛选或标记需求的应用程序

使用建议

  • 需要理解 Flutter 的 widget 组合
  • 可能需要额外的样式来支持复杂主题
  • 与其他成熟包相比,文档有限
  • 最适合频繁用户选择的应用程序
  • 不适合用于没有交互性的简单静态选择

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Pub Version GitHub GitHub GitHub

The successor to smart_select and chips_choice with cleaner, more flexible, and composable API for creating inline or prompted choice widgets with single or multiple selection.

Features

  • Cleaner, flexible, and composable API
  • Inline or prompted choice widget
  • Single or multiple choice widget
  • Searchable choice items with highlighted result
  • Specifies whether the choice selection needs to be confirmed
  • Use any type of choice data and load it easily, either synchronously or asynchronously
  • Build your own choice item that fits your needs
  • Groupable choice items that can be sorted
  • Supports leading and trailing choice items, as well as divider choice items
  • Use a predefined wrapped, scrollable, virtualized choice list, or build one that fits your needs
  • Use a predefined popup dialog, bottom sheet, new page prompt, or build one that fits your needs
  • Use a predefined modal header, footer, and separator, or build one that fits your needs
  • Use a predefined group list, group item, and group header, or build one that fits your needs
  • Use a predefined anchor widget or build one that fits your needs
  • Use a predefined widget on loading, on error, and placeholder when empty, or build one that fits your needs

Usage

For a complete usage, please see the example.

To read more about classes and other references used by choice, see the API Reference.

Single Choice

Preview

There are a few constructors to create a single selection choice widget:

By default, the choice controller maintains the selection state within a List. Any constructors other than those dedicated to single choice will have a List<T> for their value and onChanged prop. If we want to use a single value of T, we can use ChoiceSingle.value adapter to fill the value prop and ChoiceSingle.onChanged adapter to fill the onChanged prop.

Here is an example of how to use the ChoiceSingle.value and ChoiceSingle.onChanged adapters

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

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

  @override
  State<InlineScrollableX> createState() => _InlineScrollableXState();
}

class _InlineScrollableXState extends State<InlineScrollableX> {
  List<String> choices = [
    'News',
    'Entertainment',
    'Politics',
    'Automotive',
    'Sports',
    'Education',
    'Fashion',
    'Travel',
    'Food',
    'Tech',
    'Science',
    'Arts'
  ];

  String? selectedValue;

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

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Choice<String>.inline(
          clearable: true,
          value: ChoiceSingle.value(selectedValue),
          onChanged: ChoiceSingle.onChanged(setSelectedValue),
          itemCount: choices.length,
          itemBuilder: (state, i) {
            return ChoiceChip(
              selected: state.selected(choices[i]),
              onSelected: state.onSelected(choices[i]),
              label: Text(choices[i]),
            );
          },
          listBuilder: ChoiceList.createScrollable(
            spacing: 10,
            padding: const EdgeInsets.symmetric(
              horizontal: 20,
              vertical: 25,
            ),
          ),
        ),
        InlineChoice<String>.single(
          clearable: true,
          value: selectedValue,
          onChanged: setSelectedValue,
          itemCount: choices.length,
          itemBuilder: (state, i) {
            return ChoiceChip(
              selected: state.selected(choices[i]),
              onSelected: state.onSelected(choices[i]),
              label: Text(choices[i]),
            );
          },
          listBuilder: ChoiceList.createWrapped(
            spacing: 10,
            runSpacing: 10,
            padding: const EdgeInsets.symmetric(
              horizontal: 20,
              vertical: 25,
            ),
          ),
        ),
      ],
    );
  }
}
Multiple Choice

Preview

There are a few constructors to create an multiple selection choice widget:

Inline Choice

Preview

There are a few constructors to create an inline choice widget:

Prompted Choice

Preview

There are a few constructors to create a prompted choice widget:

Choice Data

Preview

Choice Item

Preview

With Future Builder

Preview

With Form and FormField

Preview

TODO

  • :white_check_mark: Grouped choice items
  • :x: Sortable choice items better to do this outside
  • :black_square_button: Add scroll wheel list
  • :black_square_button: Add dropdown choice widget
  • :black_square_button: Add chainable choice widget

Sponsoring

Buy Me A Coffee Ko-Fi

If this package or any other package I created is helping you, please consider to sponsor me so that I can take time to read the issues, fix bugs, merge pull requests and add features to these packages.

所属分类

源码仓库

使用此插件的项目