Flutter可滚动表格视图插件scrollable_table_view的使用
Flutter可滚动表格视图插件scrollable_table_view的使用
Features
这是一个多轴可滚动的数据表,它允许你在垂直和水平轴上滚动,同时保持标题在垂直轴上的静态位置。请查看下面的演示。
Demo
此小部件的作用与DataTable
相同,但有一个优势,即它可以同时在水平和垂直轴上滚动,同时保持标题的垂直位置不变。
Note
对于更复杂的功能,如冻结列和行,请参阅data_table_2包。
Getting Started
只需在你的依赖项中添加以下行。
dependencies:
scrollable_table_view: <latest-version>
Usage
以下是ScrollableTableView
的基本用法示例:
ScrollableTableView(
headers: [
"product_id",
"product_name",
"price",
].map((label) {
return TableViewHeader(
label: label,
);
}).toList(),
rows: [
["PR1000", "Milk", "20.00"],
["PR1001", "Soap", "10.00"],
].map((record) {
return TableViewRow(
height: 60,
cells: record.map((value) {
return TableViewCell(
child: Text(value),
);
}).toList(),
);
}).toList(),
);
Pagination
从版本1.0.0-beta开始支持分页。首先,初始化一个PaginationController
如下:
final PaginationController paginationController = PaginationController(
rowCount: many_products.length,
rowsPerPage: 10,
);
接下来,初始化你的表格并传递分页控制器到paginationController
参数:
ScrollableTableView(
paginationController: paginationController,
headers: labels.map((label) {
return TableViewHeader(
label: label,
);
}).toList(),
rows: many_products.map((product) {
return TableViewRow(
height: 60,
cells: labels.map((label) {
return TableViewCell(
child: Text(product[label] ?? ""),
);
}).toList(),
);
}).toList(),
)
通过上述设置,可以使用paginationController.next()
和paginationController.backwards()
分别向前和向后导航。要直接跳转到某一页,使用paginationController.jumpTo(pageToJumpTo)
。
有关完整示例,请参阅main.dart文件中的示例项目。
Additional Information
- GitHub Repo: https://github.com/herbertamukhuma/scrollable_table_view
- 提交问题和功能请求:https://github.com/herbertamukhuma/scrollable_table_view/issues
Common Issues
- 加载过多记录:从版本1.0.0-beta开始实现了分页功能。请务必使用此功能以避免一次加载过多记录,这将使您的应用程序不堪重负。
完整示例代码
下面是包含分页功能的完整示例代码:
import 'package:flutter/material.dart';
import 'package:scrollable_table_view/scrollable_table_view.dart';
import 'model.dart'; // 假设你有一个模型文件定义了products和many_products
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const DefaultTabController(
length: 2,
child: MyHomePage(title: 'Scrollable Table View Example'),
),
debugShowCheckedModeBanner: false,
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
Key? key,
required this.title,
}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final PaginationController _paginationController = PaginationController(
rowCount: many_products.length,
rowsPerPage: 10,
);
@override
Widget build(BuildContext context) {
var columns = products.first.keys.toList();
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
bottom: const TabBar(
tabs: [
Tab(text: "Simple"),
Tab(text: "Paginated"),
],
),
),
body: TabBarView(
children: [
// 简单表格
ScrollableTableView(
headers: columns.map((column) {
return TableViewHeader(
label: column,
);
}).toList(),
rows: products.map((product) {
return TableViewRow(
height: 60,
cells: columns.map((column) {
return TableViewCell(
child: Text(product[column] ?? ""),
);
}).toList(),
);
}).toList(),
),
// 分页表格
Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25),
child: ValueListenableBuilder(
valueListenable: _paginationController,
builder: (context, value, child) {
return Row(
children: [
Text(
"${_paginationController.currentPage} of ${_paginationController.pageCount}"),
Row(
children: [
IconButton(
onPressed:
_paginationController.currentPage <= 1
? null
: () {
_paginationController.previous();
},
iconSize: 20,
splashRadius: 20,
icon: Icon(
Icons.arrow_back_ios_new_rounded,
color: _paginationController.currentPage <= 1
? Colors.black26
: Theme.of(context).primaryColor,
),
),
IconButton(
onPressed: _paginationController.currentPage >=
_paginationController.pageCount
? null
: () {
_paginationController.next();
},
iconSize: 20,
splashRadius: 20,
icon: Icon(
Icons.arrow_forward_ios_rounded,
color: _paginationController.currentPage >=
_paginationController.pageCount
? Colors.black26
: Theme.of(context).primaryColor,
),
),
],
),
],
);
}),
),
Expanded(
child: ScrollableTableView(
paginationController: _paginationController,
headers: columns.map((column) {
return TableViewHeader(
label: column,
);
}).toList(),
rows: many_products.map((product) {
return TableViewRow(
height: 60,
cells: columns.map((column) {
return TableViewCell(
child: Text(product[column] ?? ""),
);
}).toList(),
);
}).toList(),
),
),
],
),
],
),
);
}
}
更多关于Flutter可滚动表格视图插件scrollable_table_view的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter可滚动表格视图插件scrollable_table_view的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中使用scrollable_table_view
插件来创建可滚动表格视图的示例代码。scrollable_table_view
插件允许你创建具有水平和垂直滚动功能的表格视图。
首先,确保你的pubspec.yaml
文件中包含scrollable_table_view
依赖项:
dependencies:
flutter:
sdk: flutter
scrollable_table_view: ^x.y.z # 请替换为最新版本号
然后运行flutter pub get
来安装依赖项。
接下来是一个简单的示例代码,展示如何使用scrollable_table_view
:
import 'package:flutter/material.dart';
import 'package:scrollable_table_view/scrollable_table_view.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Scrollable Table View Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
final List<List<String>> tableData = [
['Header 1', 'Header 2', 'Header 3', 'Header 4'],
['Row 1 Col 1', 'Row 1 Col 2', 'Row 1 Col 3', 'Row 1 Col 4'],
['Row 2 Col 1', 'Row 2 Col 2', 'Row 2 Col 3', 'Row 2 Col 4'],
['Row 3 Col 1', 'Row 3 Col 2', 'Row 3 Col 3', 'Row 3 Col 4'],
// 添加更多行以展示滚动效果
['Row 4 Col 1', 'Row 4 Col 2', 'Row 4 Col 3', 'Row 4 Col 4'],
['Row 5 Col 1', 'Row 5 Col 2', 'Row 5 Col 3', 'Row 5 Col 4'],
['Row 6 Col 1', 'Row 6 Col 2', 'Row 6 Col 3', 'Row 6 Col 4'],
['Row 7 Col 1', 'Row 7 Col 2', 'Row 7 Col 3', 'Row 7 Col 4'],
['Row 8 Col 1', 'Row 8 Col 2', 'Row 8 Col 3', 'Row 8 Col 4'],
['Row 9 Col 1', 'Row 9 Col 2', 'Row 9 Col 3', 'Row 9 Col 4'],
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Scrollable Table View Example'),
),
body: ScrollableTableView(
header: Container(
decoration: BoxDecoration(
color: Colors.grey[300],
),
padding: EdgeInsets.all(8.0),
child: Row(
children: tableData[0].map((String title) => Text(title)).toList(),
),
),
rows: tableData.skip(1).map((List<String> rowData) {
return Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(color: Colors.grey[300]),
),
),
child: Row(
children: rowData.map((String cellData) => Text(cellData)).toList(),
),
);
}).toList(),
),
);
}
}
代码解释
-
依赖项: 确保在
pubspec.yaml
中添加了scrollable_table_view
依赖项。 -
主应用:
MyApp
类定义了Flutter应用的主要结构,并使用MaterialApp
来创建应用。 -
主页:
MyHomePage
类包含了表格数据和一个ScrollableTableView
小部件。 -
表格数据:
tableData
是一个包含表格内容的二维列表。第一行是表头,其余行是表格数据。 -
ScrollableTableView
:header
参数定义了表头,这里使用了一个Container
包裹的Row
,每个单元格是一个Text
小部件。rows
参数定义了表格的行,使用skip(1)
跳过了表头,然后映射每一行数据到一个Container
包裹的Row
,每个单元格是一个Text
小部件。
注意事项
- 根据你的需要,你可以自定义每个单元格的样式和内容。
- 确保在实际应用中处理大量的数据时使用性能优化策略,例如分页加载数据。
希望这个示例能帮助你快速上手scrollable_table_view
插件的使用。如果有其他问题,欢迎继续提问!