Flutter表格粘性头部插件sticky_table的使用
Flutter表格粘性头部插件sticky_table的使用
一个可以固定表头、首尾列、以及可以自定义单元格和表头的表格组件。
开始使用
在命令行中执行以下命令来获取 sticky_table
插件:
flutter pub add sticky_table
效果演示
固定列,表头自动固定
demo() {
StickyTable(
data: [1, 2, 3, 4, 5],
columns: [
StickyTableColumn(
"Title",
// 固定在开头
fixedStart: true,
// 固定在结尾
fixedEnd: false,
)
]
);
}
去除默认的单元格内边距
demo() {
StickyTable(
cellPadding: EdgeInsets.zero,
);
}
自定义 表格的头、尾的背景描述符
demo() {
StickyTable(
titleDecoration: BoxDecoration(
color: Colors.white
),
rowDecoration: BoxDecoration(
color: Colors.black12
),
);
}
显示斑马线/ Show the zebra crossing
demo() {
StickyTable(
showZebraCrossing: true,
zebraCrossingColor: (Colors.white, const Color(0xfff5f5f5)),
zebraCrossingRadius: const Radius.circular(12),
);
}
完整使用方式 / Full Usage
demo() {
return StickyTable(
// 支持任意数据数组
data: List.generate(50, (index) => sort ? 50 - index : index),
// 默认列宽
defaultColumnWidth: const FixedColumnWidth(80),
// 标题的高度 default: 58
titleHeight: 58,
// 单元格高度 default: 58
cellHeight: 58,
// 每一列的配置
columns: [
StickyTableColumn(
"名称", // 标题 / TITLE
fixedStart: true,
// 是否在开头固定 / Is it fixed at the beginning?
showSort: true,
// 是否支持排序 / Support sorting
sort: sort,
// 排序图标 / Sort icon
// 列宽:支持百分比和自适应 / Column width, Support percentage and adaptive
columnWidth: const FixedColumnWidth(80),
// 单元格的对齐方式 / Align cells
alignment: Alignment.centerLeft,
// 标题的点击事件 / Click event of the title
onTitleClick: (context, title) {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(content: Text("排序")));
setState(() {
sort = !(title.sort ?? false);
});
},
// 自定义渲染单元格 / Custom rendering cells
renderCell: (context, title, data, row, column) {
return Text("名称$data");
},
// 自定义渲染标题 / Custom rendering title
renderTitle: (context, title) {
return Text(
title.title,
style: const TextStyle(color: Colors.purple),
);
},
),
StickyTableColumn(
"年龄",
showSort: true,
sort: false,
onCellClick: (context, title, data, row, column) {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text("年龄$data")));
},
renderCell: (context, title, data, row, column) {
return Text("*$data");
},
),
...List.generate(
10,
(index) => StickyTableColumn(
"标题$index",
showSort: true,
sort: false,
renderCell: (context, title, data, row, column) {
return Text("内容$row-$column");
},
),
),
StickyTableColumn(
"操作",
// 固定在结尾
fixedEnd: true,
columnWidth: const FixedColumnWidth(100),
renderCell: (context, title, data, row, column) {
return MaterialButton(
onPressed: () {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text("删除$data成功")));
},
color: Colors.red,
minWidth: 0,
child: const Text(
"删除",
style: TextStyle(color: Colors.white),
),
);
},
),
],
);
}
更多关于Flutter表格粘性头部插件sticky_table的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter表格粘性头部插件sticky_table的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter中使用sticky_table
插件来创建一个带有粘性头部的表格的示例代码。首先,你需要确保在你的pubspec.yaml
文件中添加了sticky_table
依赖项:
dependencies:
flutter:
sdk: flutter
sticky_table_headers: ^0.13.0 # 请检查最新版本号
然后,运行flutter pub get
来安装依赖。
以下是一个完整的示例代码,展示了如何使用sticky_table_headers
来创建一个带有粘性头部的表格:
import 'package:flutter/material.dart';
import 'package:sticky_table_headers/sticky_table_headers.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Sticky Table Headers Example'),
),
body: StickyTableHeaders(
headerBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return Container(
color: Colors.blueGrey[50],
child: Table(
columnWidths: const {
0: FlexColumnWidth(1.0),
1: FlexColumnWidth(1.5),
2: FlexColumnWidth(1.0),
},
children: [
TableRow(
decoration: BoxDecoration(
color: Colors.blueGrey[100],
),
children: [
Text('Header 1', style: TextStyle(color: Colors.white)),
Text('Header 2', style: TextStyle(color: Colors.white)),
Text('Header 3', style: TextStyle(color: Colors.white)),
],
),
],
),
);
},
contentBuilder: (BuildContext context) {
return Container(
height: 300, // 设置内容区域的高度
child: Table(
columnWidths: const {
0: FlexColumnWidth(1.0),
1: FlexColumnWidth(1.5),
2: FlexColumnWidth(1.0),
},
defaultVerticalAlignment: TableCellVerticalAlignment.middle,
children: List.generate(20, (index) {
return TableRow(
children: [
Text('Row ${index + 1} Col 1'),
Text('Row ${index + 1} Col 2'),
Text('Row ${index + 1} Col 3'),
],
);
}),
),
);
},
),
),
);
}
}
解释
-
依赖项:在
pubspec.yaml
中添加sticky_table_headers
依赖项。 -
主应用:
MyApp
是一个StatelessWidget
,它创建了一个MaterialApp
,其中包含一个带有标题的Scaffold
。 -
粘性头部和内容构建器:
headerBuilder
函数用于构建表格的头部。在这个例子中,头部是一个简单的Table
,包含三列,每列都有一个文本标题。contentBuilder
函数用于构建表格的内容。这里,我们使用List.generate
来生成20行数据,每行都包含三个文本单元格。
-
样式和布局:
- 头部和内容部分都使用了
Container
来设置背景颜色和布局。 - 头部表格的单元格背景颜色设置为
Colors.blueGrey[100]
,文本颜色设置为白色。 - 内容部分的高度设置为300,以便能够滚动并查看粘性头部的效果。
- 头部和内容部分都使用了
运行这个代码,你会看到一个带有粘性头部的表格,当你滚动内容时,头部会保持固定在顶部。