Flutter组合选择插件vit_combo_box的使用

发布于 1周前 作者 caililin 来自 Flutter

Flutter组合选择插件vit_combo_box的使用

本项目专注于提供三个小部件:

  • VitComboBox: 通用的组合框实现。
  • FutureComboBox: 接受一个future作为数据源。在future未完成时,会显示一个指示器。
  • CheckedComboBox: 在其覆盖层中包含复选框的组合框。

每个实现都专注于提供最大的可定制性,这意味着该小部件可以按你想要的方式呈现,以适应你的设计语言。

示例图

示例图

示例图

使用方法

// 普通组合框
VitComboBox.itemBuilder(
    label: '我的组合框',
    options: optionsSet,
    selection: selectedOption,
    itemBuilder: itemBuilder,
    onSelected: (key) {
        setState(() {
            selectedOption = key;
        });
        return null;
    },
),

限制和已知问题

  • 覆盖层始终显示在小部件下方。这意味着如果组合框靠近屏幕边缘,则覆盖层可能会部分超出屏幕。

完整示例

以下是一个完整的示例,展示了如何在Flutter应用中使用vit_combo_box插件。

import 'package:flutter/material.dart';
import 'package:vit_combo_box/theme/style/combo_box_style.dart';
import 'package:vit_combo_box/theme/style/vit_combo_box_style.dart';
import 'package:vit_combo_box/vit_combo_box.dart';

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: const Text('Vit 组合框'),
      ),
      body: const MyApp(),
    ),
  ));
}

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

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  DateTime? selectedDate;
  Set<DateTime> selectedDates = {};

  [@override](/user/override)
  void initState() {
    super.initState();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    var set = List.generate(20, (index) {
      return DateTime(2000 + index);
    }).toSet();

    itemBuilder(DateTime item) => Text('年份 ${item.year}');

    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Column(
        children: [
          // 普通组合框
          VitComboBox.itemBuilder(
            options: set,
            selection: selectedDate,
            itemBuilder: itemBuilder,
            onSelected: (key) {
              setState(() {
                selectedDate = key;
              });
              return null;
            },
            label: '日期',
          ),

          const SizedBox(height: 20),

          // 禁用的组合框
          VitComboBox.itemBuilder(
            options: set,
            selection: selectedDate,
            itemBuilder: itemBuilder,
            onSelected: (key) {
              setState(() {
                selectedDate = key;
              });
              return null;
            },
            enabled: false,
            label: '禁用',
          ),

          const SizedBox(height: 20),

          // 带有主题装饰的组合框
          VitComboBoxTheme(
            data: VitComboBoxStyle(
              comboBox: ComboBoxStyle(
                decoration: BoxDecoration(
                  color: Colors.purple.shade100,
                ),
              ),
            ),
            child: VitComboBox.itemBuilder(
              options: const {true, false},
              label: '带有主题装饰',
              itemBuilder: (item) {
                return Text(item.toString());
              },
              onSelected: (key) {
                return null;
              },
            ),
          ),

          const SizedBox(height: 20),

          // 带复选框的组合框
          CheckedComboBox<DateTime>(
            options: set,
            itemBuilder: (item) {
              return Text('年份 ${item.year}');
            },
            selectedItems: selectedDates,
            onSelected: (item, selected) {
              if (selected) {
                selectedDates.add(item);
              } else {
                selectedDates.remove(item);
              }
            },
            onClose: () {
              setState(() {});
            },
            label: '带复选框的组合框',
          ),

          const SizedBox(height: 20),

          // 需要加载数据的组合框
          FutureComboBox.eternal(
            label: '需要加载一些数据的组合框 [ 它永远不会得到它们 😔 ]',
          ),
        ],
      ),
    );
  }
}

更多关于Flutter组合选择插件vit_combo_box的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter组合选择插件vit_combo_box的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter中使用vit_combo_box插件的示例代码。vit_combo_box是一个组合选择插件,允许用户在UI中选择多个选项。假设你已经将vit_combo_box添加到你的pubspec.yaml文件中,并且已经运行了flutter pub get

1. 添加依赖

首先,确保你的pubspec.yaml文件中包含vit_combo_box的依赖:

dependencies:
  flutter:
    sdk: flutter
  vit_combo_box: ^最新版本号  # 替换为实际最新版本号

2. 导入插件

在你的Dart文件中,导入vit_combo_box

import 'package:vit_combo_box/vit_combo_box.dart';

3. 使用vit_combo_box

下面是一个完整的示例,展示如何在Flutter应用中使用vit_combo_box

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  // 定义一个列表来存储选项
  List<String> options = ['Option 1', 'Option 2', 'Option 3', 'Option 4'];
  // 定义一个列表来存储选中的选项
  List<String> selectedOptions = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('vit_combo_box Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text('Selected Options:'),
            Wrap(
              spacing: 8.0, // 子控件之间的间距
              runSpacing: 4.0, // 子控件运行之间的间距
              children: List.generate(
                selectedOptions.length,
                (index) => Chip(
                  label: Text(selectedOptions[index]),
                  onDeleted: () {
                    setState(() {
                      selectedOptions.removeAt(index);
                    });
                  },
                ),
              ),
            ),
            SizedBox(height: 16.0),
            VitComboBox(
              options: options,
              selectedOptions: selectedOptions,
              onSelectedOptionsChanged: (List<String> newSelectedOptions) {
                setState(() {
                  selectedOptions = newSelectedOptions;
                });
              },
            ),
          ],
        ),
      ),
    );
  }
}

解释

  1. 依赖添加:确保在pubspec.yaml中添加了vit_combo_box依赖。
  2. 导入插件:在Dart文件中导入vit_combo_box
  3. 定义选项和选中的选项:在_MyHomePageState类中,定义了两个列表,options存储所有可选的选项,selectedOptions存储当前选中的选项。
  4. UI构建
    • 使用TextWrap小部件显示当前选中的选项,每个选项用Chip小部件表示,并提供删除功能。
    • 使用VitComboBox小部件来呈现组合选择框,options属性传入所有可选的选项,selectedOptions属性传入当前选中的选项,onSelectedOptionsChanged回调函数用于更新选中的选项。

这样,你就可以在Flutter应用中使用vit_combo_box插件来实现组合选择功能了。

回到顶部