data_table_2 2.2.0
data_table_2: ^2.2.0 copied to clipboard
In-place substitute for Flutter's DataTable and PaginatedDataTable with fixed/sticky headers and few extra features
In-place substitute for Flutter's stock DataTable and PaginatedDataTable widgets with fixed header/sticky top row and other useful features missing in the originals. DataTable2 and PaginatedDataTable2 widgets are based on the sources of Flutter's originals, mimic the API and provide seamless integration.
If you've been using (or considered using) standard Flutter's widgets for displaying tables or data grids and missed the sticky headers (or vertical borders, 'No rows' placeholder, straightforward async data source API etc.) - you've come to the right place. No need to learn yet another API of a new control, just stick to well described DataTable and PaginatedDataTable.
LIVE DEMO #
- please check the example folder which demos various features of the widgets as well as contains few screens with original DataTable and PgaintedDataTable widgets for a reference. There's also a DataGrid Sample in separate repo which is based on DataTable2.
Extra Features #
- Sticky headers and paginator (when using
PabinatedDataTable2) - Vertically scrollable main area (with data rows)
autoRowsToHeightproperty on PaginatedDataTable2 allows to auto calculate page size depending on how much rows fit the height and makes vertical scrolling unnecessary
- All columns are fixed width, table automatically stretches horizontally, individual column's width is determined as (Width)/(Number of Columns)
- Should you want to adjust sizes of columns, you can replace
DataColumndefinitions withDataColumn2(which is a descendant of DataColumn). The class providessizeproperty which can be set to one of 3 relative sizes (S, M and L) - Width ratios between Small and Medium, Large and Medium columns are defined by
smRatioandlmRatioparams - You can limit the minimal width of the control and scroll it horizontally if the viewport is narrower (by setting
minWidthproperty) which is useful in portrait orientations with multiple columns not fitting the screen - You can add bottom margin (by setting
bottomMarginproperty) to allow slight overscroll - Fixed width columns are faster than default implementation of DataTable which does 2 passes to determine contents size and justify column widths
- Should you want to adjust sizes of columns, you can replace
- Data rows are wrapped in
FlexibleandSingleScrollViewwidgets to allow widget to fill parent container and be scrollable- Vertical scroller is exposed via table's
scrollControllerproperty. See example 'DataTable2 - Scroll-up' which shows 'up' button when scrolling down and allows to jump to the top of the table PaginatedDataTable2.fitproperty controls whether the paginator sticks to the bottom and leaves a gap to data rows above
- Vertical scroller is exposed via table's
- There's
DataRow2alternative to stockDataRowwhich provides row level tap events (including right clicks) emptyproperty which allows defining a placeholder widget to be displayed when data source is emptyborderallows drawing inner and outer vertical and horizontal borders (e.g. outlining individual cells) - stock widgets only allow drawing horizontal row splittersPaginatorControllerallows to externally controlPaginatedDataTable2state (e.g. switch pages, change page size etc.)- Experimental
AsynPaginatedDataTable2widget is built for asynchronous scenarios (such a requesting data from web service) and relies onAsyncDataTableSourcereturning rows in aFuture
Usage #
-
Add reference to pubspec.yaml.
-
Import:
import 'package:data_table_2/data_table_2.dart';
- Code:
import 'package:data_table_2/data_table_2.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
/// Example without a datasource
class DataTable2SimpleDemo extends StatelessWidget {
const DataTable2SimpleDemo();
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(16),
child: DataTable2(
columnSpacing: 12,
horizontalMargin: 12,
minWidth: 600,
columns: [
DataColumn2(
label: Text('Column A'),
size: ColumnSize.L,
),
DataColumn(
label: Text('Column B'),
),
DataColumn(
label: Text('Column C'),
),
DataColumn(
label: Text('Column D'),
),
DataColumn(
label: Text('Column NUMBERS'),
numeric: true,
),
],
rows: List<DataRow>.generate(
100,
(index) => DataRow(cells: [
DataCell(Text('A' * (10 - index % 10))),
DataCell(Text('B' * (10 - (index + 5) % 10))),
DataCell(Text('C' * (15 - (index + 5) % 10))),
DataCell(Text('D' * (15 - (index + 10) % 10))),
DataCell(Text(((index + 0.1) * 25.4).toString()))
]))),
);
}
}
If you're already using the standard widgets you can reference the package and add '2' to the names of stock widgets (making them DataTable2 or PaginatedDataTable2) and that is it.
Know issues/limitations #
- All data rows are fixed height, you can't control individual heights of arbitrary rows
- There's no capability to size data table cells to fit contents. Column width's adapt to available width (either to parent width or
minWidth), data rows width are predefined by constructor params. Content that doesn't fit a cell gets clipped - Using
borderproperties disables row coloring viaDataRow2(color: MaterialStateColor.resolveWith((states) => ... - When setting DataRow/DataRow2 color via
colorproperty to any material color other than transparent hover colors doesn't get applied. I.e. you can't have both striped rows with hovering working by using MaterialStateColor:color: MaterialStateProperty.resolveWith( (states) => states.contains(MaterialState.hovered) ? Colors.blue : index % 2 == 0 ? Colors.red : Colors.amber) - Cell tap events disabling any row level events. If both a row and a cell within that row happen to have tap events, tapping on a cell won't raise any row events
