Flutter自定义数据网格插件datagrid_custom的使用
Flutter自定义数据网格插件datagrid_custom的使用
简介
Flutter 的数据网格控件(如 SfDataGrid
)用于以表格形式展示和操作数据。它从底层构建,旨在处理大量数据时也能保持最佳性能。
免责声明: 这是一个商业包。要使用此包,您需要拥有 Syncfusion 商业许可证或 免费 Syncfusion 社区许可证。更多详情请参阅 许可证文件。
目录
数据网格功能
列类型
支持在每列中加载任何小部件。
编辑
允许用户编辑单元格值。根据列类型加载适当的编辑器小部件来编辑单元格值。
列大小调整
通过多种大小调整选项设置列宽。根据单元格值调整列宽以提高可读性。
行高
设置标题行和数据行的高度。根据单元格值调整行高以提高可读性。还可以为特定行设置不同的高度。
排序
按升序或降序对一个或多个列进行排序。
选择
可以选择一行或多行。Web 平台支持键盘导航。内置复选框列可以在每行显示复选框,当勾选时选择整行。用户还可以通过勾选标题行的复选框选择或取消所有行。
样式
可以自定义单元格和标题的外观。还支持条件样式。
堆叠标题
显示无绑定的标题行。无绑定的标题行可以跨多行和多列扩展堆叠标题列。
汇总行
显示额外的无绑定行以显示汇总或总计。用户可以在列中显示最小值、最大值、平均值和计数。
列调整大小
通过单击并拖动列标题的右边界来调整列宽。
加载更多
当滚动到网格的最大偏移量时,显示交互视图。
分页
分段加载数据。这对于加载大量数据非常有用。
页脚
显示附加行,可以显示在最后一行之下。也可以在页脚行中显示小部件。
固定窗格
当滚动网格时冻结行和列。
滑动手势
向右或向左滑动行以执行自定义操作,例如删除或编辑。当用户滑动某行时,该行将移动并显示自定义操作。
下拉刷新
允许用户在刷新时拉动 DataGrid。
导出
将 DataGrid 内容(如行、堆叠标题行和表格汇总行)导出为 Excel 和 PDF 格式,并提供多种自定义选项。
主题
使用深色或浅色主题。
可访问性
DataGrid 可以轻松被屏幕阅读器访问。
右对齐(RTL)
支持右到左方向的语言,例如希伯来语和阿拉伯语。
获取演示应用
通过以下应用商店安装我们的示例浏览器应用程序,以便在您的设备上探索 Syncfusion Flutter 小部件的全部功能,并在 GitHub 上查看示例代码。
其他有用的链接
查看以下资源以了解有关 Syncfusion Flutter DataGrid 的更多信息:
安装
从 pub 安装最新版本。
开始使用
导入以下包:
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
创建应用程序的数据
SfDataGrid
依赖于数据。创建一个简单的数据源以供 SfDataGrid
使用,如以下代码所示。
class Employee {
Employee(this.id, this.name, this.designation, this.salary);
final int id;
final String name;
final String designation;
final int salary;
}
List<Employee> employees = [];
late EmployeeDataSource employeeDataSource;
[@override](/user/override)
void initState() {
super.initState();
employees = getEmployees();
employeeDataSource = EmployeeDataSource(employees: employees);
}
List<Employee> getEmployees() {
return [
Employee(10001, 'James', 'Project Lead', 20000),
Employee(10002, 'Kathryn', 'Manager', 30000),
Employee(10003, 'Lara', 'Developer', 15000),
Employee(10004, 'Michael', 'Designer', 15000),
Employee(10005, 'Martin', 'Developer', 15000),
Employee(10006, 'Newberry', 'Developer', 15000),
Employee(10007, 'Balnc', 'Developer', 15000),
Employee(10008, 'Perry', 'Developer', 15000),
Employee(10009, 'Gable', 'Developer', 15000),
Employee(10010, 'Grimes', 'Developer', 15000)
];
}
为数据网格创建数据源
DataGridSource
用于获取 SfDataGrid
的行数据。因此,从 DataGridSource
创建数据源并覆盖以下 API:
-
rows: 获取用于数据填充的行。它还用于获取相应的数据对象以处理选择。这包含
DataGridRow
的集合,每个行包含DataGridCell
的集合。每个单元格应在value
属性中具有单元格值。value
用于为列执行排序。 -
buildRow: 使用
DataGridRowAdapter
获取每个单元格的小部件。
class EmployeeDataSource extends DataGridSource {
EmployeeDataSource({List<Employee> employees}) {
_employees = employees
.map<DataGridRow>((e) => DataGridRow(cells: [
DataGridCell<int>(columnName: 'id', value: e.id),
DataGridCell<String>(columnName: 'name', value: e.name),
DataGridCell<String>(
columnName: 'designation', value: e.designation),
DataGridCell<int>(columnName: 'salary', value: e.salary),
]))
.toList();
}
List<DataGridRow> _employees = [];
[@override](/user/override)
List<DataGridRow> get rows => _employees;
[@override](/user/override)
DataGridRowAdapter? buildRow(DataGridRow row) {
return DataGridRowAdapter(
cells: row.getCells().map<Widget>((dataGridCell) {
return Container(
alignment: (dataGridCell.columnName == 'id' || dataGridCell.columnName == 'salary')
? Alignment.centerRight
: Alignment.centerLeft,
padding: EdgeInsets.all(16.0),
child: Text(dataGridCell.value.toString()),
);
}).toList());
}
}
创建 DataGridSource
的实例并将此对象设置为 SfDataGrid
的 source
属性。
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Syncfusion DataGrid'),
),
body: Center(
child: Expanded(
child: SfDataGrid(
source: _employeeDataSource,
),
),
));
}
定义列
SfDataGrid
支持在列中加载任何小部件。您可以将列集合添加到 columns
属性。
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Syncfusion Flutter DataGrid'),
),
body: SfDataGrid(
source: employeeDataSource,
columns: <GridColumn>[
GridColumn(
columnName: 'id',
label: Container(
padding: EdgeInsets.all(16.0),
alignment: Alignment.centerRight,
child: Text(
'ID',
))),
GridColumn(
columnName: 'name',
label: Container(
padding: EdgeInsets.all(16.0),
alignment: Alignment.centerLeft,
child: Text('Name'))),
GridColumn(
columnName: 'designation',
width: 120,
label: Container(
padding: EdgeInsets.all(16.0),
alignment: Alignment.centerLeft,
child: Text('Designation'))),
GridColumn(
columnName: 'salary',
label: Container(
padding: EdgeInsets.all(16.0),
alignment: Alignment.centerRight,
child: Text('Salary'))),
],
),
);
}
上述代码片段的结果如下图所示:
支持与反馈
- 如果有任何问题,请联系 Syncfusion 支持团队 或在 社区论坛 发帖。您还可以通过我们的 反馈门户 提交功能请求或报告错误。
- 要续订您的订阅,请点击 续订 或联系我们的销售团队:sales@syncfusion.com | 免费电话:1-888-9 DOTNET。
关于 Syncfusion
Syncfusion 成立于 2001 年,总部位于北卡罗来纳州研究三角园区,拥有超过 20,000 名客户和超过 100 万用户,包括大型金融机构、财富 500 强公司和全球 IT 咨询公司。
今天,我们提供 1,600 多个控件和框架,涵盖 Web(ASP.NET Core、ASP.NET MVC、ASP.NET WebForms、JavaScript、Angular、React、Vue、Flutter 和 Blazor)、移动(.NET MAUI、Xamarin、Flutter、UWP 和 JavaScript)和桌面开发(Flutter、.NET MAUI、WinForms、WPF、UWP 和 WinUI)。我们提供就绪部署的企业级软件,用于仪表板、报表、数据集成和大数据处理。许多客户通过部署我们的软件节省了数百万的许可费用。
完整示例代码
以下是完整的示例代码,展示了如何使用 SfDataGrid
。
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
class Employee {
Employee(this.id, this.name, this.designation, this.salary);
final int id;
final String name;
final String designation;
final int salary;
}
class EmployeeDataSource extends DataGridSource {
EmployeeDataSource({List<Employee> employees}) {
_employees = employees
.map<DataGridRow>((e) => DataGridRow(cells: [
DataGridCell<int>(columnName: 'id', value: e.id),
DataGridCell<String>(columnName: 'name', value: e.name),
DataGridCell<String>(
columnName: 'designation', value: e.designation),
DataGridCell<int>(columnName: 'salary', value: e.salary),
]))
.toList();
}
List<DataGridRow> _employees = [];
[@override](/user/override)
List<DataGridRow> get rows => _employees;
[@override](/user/override)
DataGridRowAdapter? buildRow(DataGridRow row) {
return DataGridRowAdapter(
cells: row.getCells().map<Widget>((dataGridCell) {
return Container(
alignment: (dataGridCell.columnName == 'id' || dataGridCell.columnName == 'salary')
? Alignment.centerRight
: Alignment.centerLeft,
padding: EdgeInsets.all(16.0),
child: Text(dataGridCell.value.toString()),
);
}).toList());
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Syncfusion DataGrid Example'),
),
body: SfDataGrid(
source: EmployeeDataSource(
employees: [
Employee(1, 'John Doe', 'Developer', 50000),
Employee(2, 'Jane Smith', 'Manager', 70000),
Employee(3, 'Emily Davis', 'Designer', 45000),
],
),
columns: [
GridColumn(
columnName: 'id',
label: Container(
padding: EdgeInsets.all(16.0),
alignment: Alignment.centerRight,
child: Text('ID'))),
GridColumn(
columnName: 'name',
label: Container(
padding: EdgeInsets.all(16.0),
alignment: Alignment.centerLeft,
child: Text('Name'))),
GridColumn(
columnName: 'designation',
label: Container(
padding: EdgeInsets.all(16.0),
alignment: Alignment.centerLeft,
child: Text('Designation'))),
GridColumn(
columnName: 'salary',
label: Container(
padding: EdgeInsets.all(16.0),
alignment: Alignment.centerRight,
child: Text('Salary'))),
],
),
),
);
}
}
更多关于Flutter自定义数据网格插件datagrid_custom的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter自定义数据网格插件datagrid_custom的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
datagrid_custom
是一个用于 Flutter 的自定义数据网格插件,可以帮助你展示和操作表格数据。以下是如何使用 datagrid_custom
插件的基本步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 datagrid_custom
插件的依赖:
dependencies:
flutter:
sdk: flutter
datagrid_custom: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来获取依赖。
2. 导入包
在你的 Dart 文件中导入 datagrid_custom
包:
import 'package:datagrid_custom/datagrid_custom.dart';
3. 创建数据模型
你需要创建一个数据模型来定义每一行的数据。例如:
class DataModel {
DataModel(this.id, this.name, this.age);
final int id;
final String name;
final int age;
}
4. 创建数据源
接下来,创建一个数据源来提供数据给 DataGrid
。你可以使用 List<DataModel>
来存储数据:
List<DataModel> dataSource = [
DataModel(1, 'Alice', 25),
DataModel(2, 'Bob', 30),
DataModel(3, 'Charlie', 35),
];
5. 配置列
定义表格的列,指定每一列的标题、字段和样式:
List<DataGridColumn> columns = [
DataGridColumn(
columnName: 'id',
label: 'ID',
width: 100,
),
DataGridColumn(
columnName: 'name',
label: 'Name',
width: 150,
),
DataGridColumn(
columnName: 'age',
label: 'Age',
width: 100,
),
];
6. 创建数据网格
最后,使用 DataGrid
组件来展示数据:
class MyDataGrid extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Custom DataGrid'),
),
body: DataGrid(
columns: columns,
dataSource: dataSource,
getValue: (data, columnName) {
switch (columnName) {
case 'id':
return data.id;
case 'name':
return data.name;
case 'age':
return data.age;
default:
return '';
}
},
),
);
}
}
7. 运行应用
在你的 main.dart
文件中运行应用:
void main() {
runApp(MaterialApp(
home: MyDataGrid(),
));
}
8. 其他功能
datagrid_custom
还支持排序、分页、单元格编辑等功能。你可以根据文档进一步配置和扩展。
示例代码
完整的示例代码如下:
import 'package:flutter/material.dart';
import 'package:datagrid_custom/datagrid_custom.dart';
void main() {
runApp(MaterialApp(
home: MyDataGrid(),
));
}
class DataModel {
DataModel(this.id, this.name, this.age);
final int id;
final String name;
final int age;
}
class MyDataGrid extends StatelessWidget {
final List<DataModel> dataSource = [
DataModel(1, 'Alice', 25),
DataModel(2, 'Bob', 30),
DataModel(3, 'Charlie', 35),
];
final List<DataGridColumn> columns = [
DataGridColumn(
columnName: 'id',
label: 'ID',
width: 100,
),
DataGridColumn(
columnName: 'name',
label: 'Name',
width: 150,
),
DataGridColumn(
columnName: 'age',
label: 'Age',
width: 100,
),
];
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Custom DataGrid'),
),
body: DataGrid(
columns: columns,
dataSource: dataSource,
getValue: (data, columnName) {
switch (columnName) {
case 'id':
return data.id;
case 'name':
return data.name;
case 'age':
return data.age;
default:
return '';
}
},
),
);
}
}