Flutter可扩展分组插件expandable_group的使用

Flutter可扩展分组插件expandable_group的使用

ExpandableGroup 是一个用于在 Flutter 应用程序列表中支持展开和折叠分组项的新小部件。它支持 Android、iOS、Web 和桌面平台。

构建可扩展分组小部件以支持展开和折叠分组数据。

ExpandableGroup 示例

特性

  • 列表中的头部展开和折叠。
  • 支持自定义头部和列表项。
  • 单个 ListView

开始使用

1. 添加依赖到项目的 pubspec.yaml 并运行 pub get

dependencies:
  expandable_group: ^0.0.6

2. 导入 expandable_group_widget.dart 到你的项目文件中

import 'package:expandable_group/expandable_group_widget.dart';

3. ExpandableGroup 的一些属性

3.1 必需的属性

  • header: 显示在列表中的头部小部件。
  • items: 每个分组中要显示的 ListTile 列表。

3.2 可选的属性

  • isExpanded: 一个布尔值,用于展开或折叠头部。默认值为 false
  • expandedIconcollapsedIcon: 扩展和折叠状态下的小部件。
  • headerEdgeInsets: 头部小部件的 EdgeInsets
  • headerBackgroundColor: 自定义头部背景颜色。

4. 示例

ExpandableGroup(
    isExpanded: index == 0,
    header: _header('Group $index'),
    items: _buildItems(context, group),
    headerEdgeInsets: EdgeInsets.only(left: 16.0, right: 16.0)
)

完整示例

import 'package:flutter/material.dart';
import 'package:expandable_group/expandable_group_widget.dart';

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

class MyApp extends StatelessWidget {
  // 这个小部件是你的应用的根。
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Expandable Group demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      debugShowCheckedModeBanner: false,
      home: MyHomePage(
        title: 'Expandable Group demo',
        data: _generateData(),
      ),
    );
  }

  List<List<String>> _generateData() {
    int numberOfGroups = 5;
    List<List<String>> results = [];
    for (int i = 0; i < numberOfGroups; i++) {
      List<String> items = [];
      for (int j = 0; j < numberOfGroups * 5 + i; j++) {
        items.add("Item $j in group $i");
      }
      results.add(items);
    }
    return results;
  }
}

class MyHomePage extends StatefulWidget {
  final List<List<String>> data;
  MyHomePage({Key? key, required this.title, required this.data}) : super(key: key);

  final String title;

  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView(
        children: [
          Column(
            children: widget.data.map((group) {
              int index = widget.data.indexOf(group);
              return ExpandableGroup(
                isExpanded: index == 0,
                header: _header('Group $index'),
                items: _buildItems(context, group),
                headerEdgeInsets: EdgeInsets.only(left: 16.0, right: 16.0),
              );
            }).toList(),
          )
        ],
      ),
    );
  }

  Widget _header(String name) => Text(name,
      style: TextStyle(
        fontSize: 16,
        fontWeight: FontWeight.bold,
      ));

  List<ListTile> _buildItems(BuildContext context, List<String> items) => items
      .map((e) => ListTile(
            title: Text(e),
          ))
      .toList();
}

更多关于Flutter可扩展分组插件expandable_group的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

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


expandable_group 是一个用于 Flutter 的插件,它允许你创建可展开和折叠的分组列表。这对于展示分组数据或创建折叠式的 UI 元素非常有用。以下是使用 expandable_group 插件的基本步骤和示例代码。

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 expandable_group 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  expandable_group: ^2.0.0  # 请确保使用最新版本

然后运行 flutter pub get 来安装依赖。

2. 导入包

在你的 Dart 文件中导入 expandable_group

import 'package:expandable_group/expandable_group.dart';

3. 使用 ExpandableGroupExpandableGroupItem

ExpandableGroup 是一个可展开的分组,它包含一个标题(header)和多个子项(items)。ExpandableGroupItem 是分组中的单个项目。

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

class ExpandableGroupExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Expandable Group Example'),
      ),
      body: SingleChildScrollView(
        child: ExpandableGroup(
          isExpanded: false, // 初始是否展开
          header: Text('Group 1', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
          items: [
            ExpandableGroupItem(child: Text('Item 1')),
            ExpandableGroupItem(child: Text('Item 2')),
            ExpandableGroupItem(child: Text('Item 3')),
          ],
        ),
      ),
    );
  }
}

void main() => runApp(MaterialApp(
  home: ExpandableGroupExample(),
));

4. 自定义样式

你可以通过 headerDecorationitemDecoration 属性来自定义分组标题和项目的样式。

ExpandableGroup(
  isExpanded: false,
  header: Text('Group 1', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
  headerDecoration: BoxDecoration(
    color: Colors.blue,
    borderRadius: BorderRadius.circular(8),
  ),
  items: [
    ExpandableGroupItem(
      child: Text('Item 1'),
      decoration: BoxDecoration(
        color: Colors.lightBlue[100],
        borderRadius: BorderRadius.circular(4),
      ),
    ),
    ExpandableGroupItem(
      child: Text('Item 2'),
      decoration: BoxDecoration(
        color: Colors.lightBlue[100],
        borderRadius: BorderRadius.circular(4),
      ),
    ),
  ],
)

5. 处理展开/折叠事件

你可以通过 onHeaderPressed 回调来处理用户点击分组标题时的展开/折叠事件。

ExpandableGroup(
  isExpanded: false,
  header: Text('Group 1'),
  onHeaderPressed: (expanded) {
    print('Group is ${expanded ? 'expanded' : 'collapsed'}');
  },
  items: [
    ExpandableGroupItem(child: Text('Item 1')),
    ExpandableGroupItem(child: Text('Item 2')),
  ],
)

6. 多个分组

你可以在一个页面中添加多个 ExpandableGroup 来实现多个可展开的分组。

SingleChildScrollView(
  child: Column(
    children: [
      ExpandableGroup(
        header: Text('Group 1'),
        items: [
          ExpandableGroupItem(child: Text('Item 1')),
          ExpandableGroupItem(child: Text('Item 2')),
        ],
      ),
      ExpandableGroup(
        header: Text('Group 2'),
        items: [
          ExpandableGroupItem(child: Text('Item 3')),
          ExpandableGroupItem(child: Text('Item 4')),
        ],
      ),
    ],
  ),
)
回到顶部