flutter DataTable数据表格
2024-10-21 13:33:40
数据表显示原始数据集。它们通常出现在桌面企业产品中。DataTable Widget实现这个组件
文档:https://api.flutter.dev/flutter/material/DataTable-class.html
import 'package:flutter/material.dart';
import './model/post.dart'; class DataTableDemo extends StatefulWidget {
@override
_DataTableDemoState createState() => _DataTableDemoState();
} class _DataTableDemoState extends State<DataTableDemo> {
int _sortColumnIndex;
bool _sortAscending = true; @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('DataTableDemo'),
elevation: 0.0,
),
body: Container(
padding: EdgeInsets.all(16.0),
child: ListView(
children: <Widget>[
DataTable(
sortColumnIndex: _sortColumnIndex,
sortAscending: _sortAscending,
// onSelectAll: (bool value) {},
columns: [
DataColumn(
label: Text('Title'),
onSort: (int index, bool ascending) {
setState(() {
_sortColumnIndex = index;
_sortAscending = ascending; posts.sort((a, b) {
if (!ascending) {
final c = a;
a = b;
b = c;
} return a.title.length.compareTo(b.title.length);
});
});
},
),
DataColumn(
label: Text('Author'),
),
DataColumn(
label: Text('Image'),
), ],
rows: posts.map((post) {
return DataRow(
selected: post.selected,
onSelectChanged: (bool value) {
setState(() {
if (post.selected != value) {
post.selected = value;
}
});
},
cells: [
DataCell(Text(post.title)),
DataCell(Text(post.author)),
DataCell(Image.network(post.imageUrl)), ]
);
}).toList(),
),
],
),
)
);
}
}
效果:
分页demo
import 'package:flutter/material.dart';
import './model/post.dart'; class PostDataSource extends DataTableSource {
final List<Post> _posts = posts;
int _selectedCount = 0; @override
int get rowCount => _posts.length; @override
bool get isRowCountApproximate => false; @override
int get selectedRowCount => _selectedCount; @override
DataRow getRow(int index) {
final Post post = _posts[index]; return DataRow.byIndex(
index: index,
cells: <DataCell>[
DataCell(Text(post.title)),
DataCell(Text(post.author)),
DataCell(Image.network(post.imageUrl)),
],
);
} void _sort(getField(post), bool ascending) {
_posts.sort((a, b) {
if (!ascending) {
final c = a;
a = b;
b = c;
} final aValue = getField(a);
final bValue = getField(b); return Comparable.compare(aValue, bValue);
}); notifyListeners();
}
} class PaginatedDataTableDemo extends StatefulWidget {
@override
_PaginatedDataTableDemoState createState() => _PaginatedDataTableDemoState();
} class _PaginatedDataTableDemoState extends State<PaginatedDataTableDemo> {
int _sortColumnIndex;
bool _sortAscending = true; final PostDataSource _postsDataSource = PostDataSource(); @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('PaginatedDataTableDemo'),
elevation: 0.0,
),
body: Container(
padding: EdgeInsets.all(16.0),
child: ListView(
children: <Widget>[
PaginatedDataTable(
header: Text('Posts'),
rowsPerPage: 5,
source: _postsDataSource,
sortColumnIndex: _sortColumnIndex,
sortAscending: _sortAscending,
// onSelectAll: (bool value) {},
columns: [
DataColumn(
label: Text('Title'),
onSort: (int columnIndex, bool ascending) {
_postsDataSource._sort((post) => post.title.length, ascending); setState(() {
_sortColumnIndex = columnIndex;
_sortAscending = ascending;
});
},
),
DataColumn(
label: Text('Author'),
),
DataColumn(
label: Text('Image'),
),
],
),
],
),
));
}
}
效果:
最新文章
- perl学习之路1
- 微信电脑版-微信for windows客户端发布
- RHEL6 --部署phpMyAdmin与论坛系统
- pthread——pthread_cleanup
- 如何给开源的DUILib支持Accessibility
- VIJOS1476旅游规划[树形DP 树的直径]
- 25个增强iOS应用程序性能的提示和技巧(中级篇)(2)
- C# Bitmap类型与Byte[]类型相互转化
- 读书笔记_Effective_C++_条款四十四:将与参数无关的代码抽离template
- Oracle 11g 默认用户名和密码
- Android FM模块学习之二 FM搜索频道
- oracle 备份脚步
- 使用php实现爬虫程序 套取网站的图片实例
- JavaEE Tutorials (6) - 使用嵌入式企业bean容器
- web项目部署到阿里云服务器步骤
- 关于CSRF
- JS应用猜数游戏
- KNN-笔记(2)
- 「SCOI2016」围棋 解题报告
- hdu 5084 前缀和预处理
热门文章
- java之比较器
- 学到了林海峰,武沛齐讲的Day35 完 协程
- RabbitMQ后台管理界面
- jsp页面解析出错,或出现下载情况.
- loj 3102
- Angular动态组件
- shell脚本编程基础之文件测试
- flutter 省市区选择器 city_pickers 的简单实用
- 原创:从海量数据中查找出前k个最小或最大值的算法(java)
- 【随记】安装SQL Server 2008 R2 提示创建usersettings/microsoft.sqlserver.configuration.landingpage.properties.se