在 Flutter 中创建 Table 的最佳方式
Best Way to Create Table In Flutter
在这里,我使用容器创建 table,并基于使用 flex number 扩展来制作 header 和 relatable 行。我认为这不是最好的方法,但我不知道如何创建这样的 table 的其他方法,任何人都可以给我一个建议吗?
代码如下:
_table栏目
Widget _bookingListSection(OnlineBookingListController controller) {
return Column(
children: [
_buildHeaderTable(),
Expanded(
child: ListView.builder(
padding: EdgeInsets.zero,
itemCount: controller.listBooking.length,
itemBuilder: (context, index) {
int lastIndex = controller.listBooking.length - 1;
return _buildContentTable(
index,
lastIndex,
context,
controller,
);
},
),
),
],
);
}
_buildHeaderTable(),
Widget _buildHeaderTable() {
return Container(
width: double.maxFinite,
height: AppSize.DIMEN_64.h,
padding: EdgeInsets.fromLTRB(
AppSize.DIMEN_22.h,
AppSize.DIMEN_16.h,
AppSize.DIMEN_22.h,
AppSize.DIMEN_16.h,
),
decoration: BoxDecoration(
color: AppColors.GREY_BLACK_BACKGROUND,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(AppSize.RADIUS_8.h),
topRight: Radius.circular(AppSize.RADIUS_8.h),
),
),
child: Row(children: [
_titleHeaderTable('Time', 3),
_titleHeaderTable('Phone Number', 3),
_titleHeaderTable('Customer Name', 3),
_titleHeaderTable('Room', 3),
_titleHeaderTable('Waitress', 3),
_titleHeaderTable('Status', 2),
_titleHeaderTable('Action', 4),
]),
);
}
Widget _titleHeaderTable(String title, int flexNum) {
return Expanded(
flex: flexNum,
child: Container(
child: Text(
title,
textAlign: TextAlign.left,
maxLines: 2,
style: textStyleW700S14.copyWith(
color: AppColors.WHITE,
),
),
),
);
}
然后对于我在行内使用 flex 的内容。您对此有什么建议吗?
Flutter 对此有 Table class(但您也可以使用简单的行 + 列组合来实现)。
这是 Table 文档的 link:Flutter Table
Container(
color: Colors.white,
padding: EdgeInsets.all(20.0),
child: Table(
border: TableBorder.all(color: Colors.black),
children: [
TableRow(children: [
Text('Cell 1'),
Text('Cell 2'),
Text('Cell 3'),
]),
TableRow(children: [
Text('Cell 4'),
Text('Cell 5'),
Text('Cell 6'),
])
],
),
)
您可以使用 Table
和 Data Table
[=25 在 Flutter 中创建 Table =]
- 使用Table Class:
参考Table
Classhere
Container(
margin: EdgeInsets.all(20),
child: Table(
defaultColumnWidth: FixedColumnWidth(120.0),
border: TableBorder.all(
color: Colors.black, style: BorderStyle.solid, width: 2),
children: [
TableRow(children: [
Column(
children: [
Text(
'Website',
style: TextStyle(fontSize: 20.0),
),
],
),
Column(
children: [
Text(
'Tutorial',
style: TextStyle(fontSize: 20.0),
),
],
),
Column(
children: [
Text(
'Review',
style: TextStyle(fontSize: 20.0),
),
],
),
]),
TableRow(children: [
Column(
children: [
Text('https://flutter.dev/'),
],
),
Column(
children: [
Text('Flutter'),
],
),
Column(
children: [
Text('5*'),
],
),
]),
TableRow(children: [
Column(
children: [
Text('https://dart.dev/'),
],
),
Column(
children: [
Text('Dart'),
],
),
Column(
children: [
Text('5*'),
],
),
]),
TableRow(children: [
Column(
children: [
Text('https://pub.dev/'),
],
),
Column(
children: [
Text('Flutter Packages'),
],
),
Column(
children: [
Text('5*'),
],
),
]),
],
),
),
结果屏幕使用 Table
Class->
- 使用
DataTable
Class:
参考DataTable
Classhere
DataTable(
columns: const <DataColumn>[
DataColumn(
label: Text(
'Sr.No',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
DataColumn(
label: Text(
'Website',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
DataColumn(
label: Text(
'Tutorial',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
DataColumn(
label: Text(
'Review',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
],
rows: const <DataRow>[
DataRow(
cells: <DataCell>[
DataCell(
Text('1'),
),
DataCell(
Text('https://flutter.dev/'),
),
DataCell(
Text('Flutter'),
),
DataCell(
Text('5*'),
),
],
),
DataRow(
cells: <DataCell>[
DataCell(
Text('2'),
),
DataCell(
Text('https://dart.dev/'),
),
DataCell(
Text('Dart'),
),
DataCell(
Text('5*'),
),
],
),
DataRow(
cells: <DataCell>[
DataCell(
Text('3'),
),
DataCell(
Text('https://pub.dev/'),
),
DataCell(
Text('Flutter Packages'),
),
DataCell(
Text('5*'),
),
],
),
],
),
结果屏幕使用 DataTable
Class->
您也可以参考 this 包 Table
在这里,我使用容器创建 table,并基于使用 flex number 扩展来制作 header 和 relatable 行。我认为这不是最好的方法,但我不知道如何创建这样的 table 的其他方法,任何人都可以给我一个建议吗?
代码如下:
_table栏目
Widget _bookingListSection(OnlineBookingListController controller) {
return Column(
children: [
_buildHeaderTable(),
Expanded(
child: ListView.builder(
padding: EdgeInsets.zero,
itemCount: controller.listBooking.length,
itemBuilder: (context, index) {
int lastIndex = controller.listBooking.length - 1;
return _buildContentTable(
index,
lastIndex,
context,
controller,
);
},
),
),
],
);
}
_buildHeaderTable(),
Widget _buildHeaderTable() {
return Container(
width: double.maxFinite,
height: AppSize.DIMEN_64.h,
padding: EdgeInsets.fromLTRB(
AppSize.DIMEN_22.h,
AppSize.DIMEN_16.h,
AppSize.DIMEN_22.h,
AppSize.DIMEN_16.h,
),
decoration: BoxDecoration(
color: AppColors.GREY_BLACK_BACKGROUND,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(AppSize.RADIUS_8.h),
topRight: Radius.circular(AppSize.RADIUS_8.h),
),
),
child: Row(children: [
_titleHeaderTable('Time', 3),
_titleHeaderTable('Phone Number', 3),
_titleHeaderTable('Customer Name', 3),
_titleHeaderTable('Room', 3),
_titleHeaderTable('Waitress', 3),
_titleHeaderTable('Status', 2),
_titleHeaderTable('Action', 4),
]),
);
}
Widget _titleHeaderTable(String title, int flexNum) {
return Expanded(
flex: flexNum,
child: Container(
child: Text(
title,
textAlign: TextAlign.left,
maxLines: 2,
style: textStyleW700S14.copyWith(
color: AppColors.WHITE,
),
),
),
);
}
然后对于我在行内使用 flex 的内容。您对此有什么建议吗?
Flutter 对此有 Table class(但您也可以使用简单的行 + 列组合来实现)。
这是 Table 文档的 link:Flutter Table
Container(
color: Colors.white,
padding: EdgeInsets.all(20.0),
child: Table(
border: TableBorder.all(color: Colors.black),
children: [
TableRow(children: [
Text('Cell 1'),
Text('Cell 2'),
Text('Cell 3'),
]),
TableRow(children: [
Text('Cell 4'),
Text('Cell 5'),
Text('Cell 6'),
])
],
),
)
您可以使用 Table
和 Data Table
[=25 在 Flutter 中创建 Table =]
- 使用Table Class:
参考Table
Classhere
Container(
margin: EdgeInsets.all(20),
child: Table(
defaultColumnWidth: FixedColumnWidth(120.0),
border: TableBorder.all(
color: Colors.black, style: BorderStyle.solid, width: 2),
children: [
TableRow(children: [
Column(
children: [
Text(
'Website',
style: TextStyle(fontSize: 20.0),
),
],
),
Column(
children: [
Text(
'Tutorial',
style: TextStyle(fontSize: 20.0),
),
],
),
Column(
children: [
Text(
'Review',
style: TextStyle(fontSize: 20.0),
),
],
),
]),
TableRow(children: [
Column(
children: [
Text('https://flutter.dev/'),
],
),
Column(
children: [
Text('Flutter'),
],
),
Column(
children: [
Text('5*'),
],
),
]),
TableRow(children: [
Column(
children: [
Text('https://dart.dev/'),
],
),
Column(
children: [
Text('Dart'),
],
),
Column(
children: [
Text('5*'),
],
),
]),
TableRow(children: [
Column(
children: [
Text('https://pub.dev/'),
],
),
Column(
children: [
Text('Flutter Packages'),
],
),
Column(
children: [
Text('5*'),
],
),
]),
],
),
),
结果屏幕使用 Table
Class->
- 使用
DataTable
Class:
参考DataTable
Classhere
DataTable(
columns: const <DataColumn>[
DataColumn(
label: Text(
'Sr.No',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
DataColumn(
label: Text(
'Website',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
DataColumn(
label: Text(
'Tutorial',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
DataColumn(
label: Text(
'Review',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
],
rows: const <DataRow>[
DataRow(
cells: <DataCell>[
DataCell(
Text('1'),
),
DataCell(
Text('https://flutter.dev/'),
),
DataCell(
Text('Flutter'),
),
DataCell(
Text('5*'),
),
],
),
DataRow(
cells: <DataCell>[
DataCell(
Text('2'),
),
DataCell(
Text('https://dart.dev/'),
),
DataCell(
Text('Dart'),
),
DataCell(
Text('5*'),
),
],
),
DataRow(
cells: <DataCell>[
DataCell(
Text('3'),
),
DataCell(
Text('https://pub.dev/'),
),
DataCell(
Text('Flutter Packages'),
),
DataCell(
Text('5*'),
),
],
),
],
),
结果屏幕使用 DataTable
Class->
您也可以参考 this 包 Table