From 42d8383c9e54327286d16d65993d915863370e1e Mon Sep 17 00:00:00 2001 From: Shi-Hao Hong Date: Wed, 19 Jun 2019 12:59:41 -0700 Subject: [PATCH] DataTable Custom Horizontal Padding (#33628) * Add optional horizontalMargin and columnSpacing properties to DataTable * Add horizontalMargin and columnSpacing tests for DataTable and PaginatedDataTable --- .../flutter/lib/src/material/data_table.dart | 45 +- .../src/material/paginated_data_table.dart | 20 + .../test/material/data_table_test.dart | 416 ++++++++++++++++++ .../material/paginated_data_table_test.dart | 282 ++++++++++++ 4 files changed, 757 insertions(+), 6 deletions(-) diff --git a/packages/flutter/lib/src/material/data_table.dart b/packages/flutter/lib/src/material/data_table.dart index 7b02d2371a..25394b233a 100644 --- a/packages/flutter/lib/src/material/data_table.dart +++ b/packages/flutter/lib/src/material/data_table.dart @@ -262,11 +262,15 @@ class DataTable extends StatelessWidget { this.sortColumnIndex, this.sortAscending = true, this.onSelectAll, + this.horizontalMargin = 24.0, + this.columnSpacing = 56.0, @required this.rows, }) : assert(columns != null), assert(columns.isNotEmpty), assert(sortColumnIndex == null || (sortColumnIndex >= 0 && sortColumnIndex < columns.length)), assert(sortAscending != null), + assert(horizontalMargin != null), + assert(columnSpacing != null), assert(rows != null), assert(!rows.any((DataRow row) => row.cells.length != columns.length)), _onlyTextColumn = _initOnlyTextColumn(columns), @@ -311,6 +315,20 @@ class DataTable extends StatelessWidget { /// row is selectable. final ValueSetter onSelectAll; + /// The horizontal margin between the edges of the table and the content + /// in the first and last cells of each row. + /// + /// When a checkbox is displayed, it is also the margin between the checkbox + /// the content in the first data column. + /// + /// This value defaults to 24.0 to adhere to the Material Design specifications. + final double horizontalMargin; + + /// The horizontal margin between the contents of each data column. + /// + /// This value defaults to 56.0 to adhere to the Material Design specifications. + final double columnSpacing; + /// The data to show in each row (excluding the row that contains /// the column headings). Must be non-null, but may be empty. final List rows; @@ -351,8 +369,6 @@ class DataTable extends StatelessWidget { static const double _headingRowHeight = 56.0; static const double _dataRowHeight = 48.0; - static const double _tablePadding = 24.0; - static const double _columnSpacing = 56.0; static const double _sortArrowPadding = 2.0; static const double _headingFontSize = 12.0; static const Duration _sortArrowAnimationDuration = Duration(milliseconds: 150); @@ -368,7 +384,7 @@ class DataTable extends StatelessWidget { Widget contents = Semantics( container: true, child: Padding( - padding: const EdgeInsetsDirectional.only(start: _tablePadding, end: _tablePadding / 2.0), + padding: EdgeInsetsDirectional.only(start: horizontalMargin, end: horizontalMargin / 2.0), child: Center( child: Checkbox( activeColor: color, @@ -533,7 +549,7 @@ class DataTable extends StatelessWidget { int displayColumnIndex = 0; if (showCheckboxColumn) { - tableColumns[0] = const FixedColumnWidth(_tablePadding + Checkbox.width + _tablePadding / 2.0); + tableColumns[0] = FixedColumnWidth(horizontalMargin + Checkbox.width + horizontalMargin / 2.0); tableRows[0].children[0] = _buildCheckbox( color: theme.accentColor, checked: allChecked, @@ -554,9 +570,26 @@ class DataTable extends StatelessWidget { for (int dataColumnIndex = 0; dataColumnIndex < columns.length; dataColumnIndex += 1) { final DataColumn column = columns[dataColumnIndex]; + + double paddingStart; + if (dataColumnIndex == 0 && showCheckboxColumn) { + paddingStart = horizontalMargin / 2.0; + } else if (dataColumnIndex == 0 && !showCheckboxColumn) { + paddingStart = horizontalMargin; + } else { + paddingStart = columnSpacing / 2.0; + } + + double paddingEnd; + if (dataColumnIndex == columns.length - 1) { + paddingEnd = horizontalMargin; + } else { + paddingEnd = columnSpacing / 2.0; + } + final EdgeInsetsDirectional padding = EdgeInsetsDirectional.only( - start: dataColumnIndex == 0 ? showCheckboxColumn ? _tablePadding / 2.0 : _tablePadding : _columnSpacing / 2.0, - end: dataColumnIndex == columns.length - 1 ? _tablePadding : _columnSpacing / 2.0, + start: paddingStart, + end: paddingEnd, ); if (dataColumnIndex == _onlyTextColumn) { tableColumns[displayColumnIndex] = const IntrinsicColumnWidth(flex: 1.0); diff --git a/packages/flutter/lib/src/material/paginated_data_table.dart b/packages/flutter/lib/src/material/paginated_data_table.dart index 46fa1348f1..ca1f8c0396 100644 --- a/packages/flutter/lib/src/material/paginated_data_table.dart +++ b/packages/flutter/lib/src/material/paginated_data_table.dart @@ -70,6 +70,8 @@ class PaginatedDataTable extends StatefulWidget { this.sortColumnIndex, this.sortAscending = true, this.onSelectAll, + this.horizontalMargin = 24.0, + this.columnSpacing = 56.0, this.initialFirstRowIndex = 0, this.onPageChanged, this.rowsPerPage = defaultRowsPerPage, @@ -83,6 +85,8 @@ class PaginatedDataTable extends StatefulWidget { assert(columns.isNotEmpty), assert(sortColumnIndex == null || (sortColumnIndex >= 0 && sortColumnIndex < columns.length)), assert(sortAscending != null), + assert(horizontalMargin != null), + assert(columnSpacing != null), assert(rowsPerPage != null), assert(rowsPerPage > 0), assert(() { @@ -131,6 +135,20 @@ class PaginatedDataTable extends StatefulWidget { /// See [DataTable.onSelectAll]. final ValueSetter onSelectAll; + /// The horizontal margin between the edges of the table and the content + /// in the first and last cells of each row. + /// + /// When a checkbox is displayed, it is also the margin between the checkbox + /// the content in the first data column. + /// + /// This value defaults to 24.0 to adhere to the Material Design specifications. + final double horizontalMargin; + + /// The horizontal margin between the contents of each data column. + /// + /// This value defaults to 56.0 to adhere to the Material Design specifications. + final double columnSpacing; + /// The index of the first row to display when the widget is first created. final int initialFirstRowIndex; @@ -430,6 +448,8 @@ class PaginatedDataTableState extends State { sortColumnIndex: widget.sortColumnIndex, sortAscending: widget.sortAscending, onSelectAll: widget.onSelectAll, + horizontalMargin: widget.horizontalMargin, + columnSpacing: widget.columnSpacing, rows: _getRows(_firstRowIndex, widget.rowsPerPage), ), ), diff --git a/packages/flutter/test/material/data_table_test.dart b/packages/flutter/test/material/data_table_test.dart index 5b3fb034b9..bacc9671de 100644 --- a/packages/flutter/test/material/data_table_test.dart +++ b/packages/flutter/test/material/data_table_test.dart @@ -267,4 +267,420 @@ void main() { expect(tester.takeException(), isNull); }); + testWidgets('DataTable custom horizontal padding - checkbox', (WidgetTester tester) async { + const double _defaultHorizontalMargin = 24.0; + const double _defaultColumnSpacing = 56.0; + const double _customHorizontalMargin = 10.0; + const double _customColumnSpacing = 15.0; + Finder cellContent; + Finder checkbox; + Finder padding; + + Widget buildDefaultTable({ + int sortColumnIndex, + bool sortAscending = true, + }) { + return DataTable( + sortColumnIndex: sortColumnIndex, + sortAscending: sortAscending, + onSelectAll: (bool value) {}, + columns: [ + const DataColumn( + label: Text('Name'), + tooltip: 'Name', + ), + DataColumn( + label: const Text('Calories'), + tooltip: 'Calories', + numeric: true, + onSort: (int columnIndex, bool ascending) {}, + ), + DataColumn( + label: const Text('Fat'), + tooltip: 'Fat', + numeric: true, + onSort: (int columnIndex, bool ascending) {}, + ), + ], + rows: kDesserts.map((Dessert dessert) { + return DataRow( + key: Key(dessert.name), + onSelectChanged: (bool selected) {}, + cells: [ + DataCell( + Text(dessert.name), + ), + DataCell( + Text('${dessert.calories}'), + showEditIcon: true, + onTap: () {}, + ), + DataCell( + Text('${dessert.fat}'), + showEditIcon: true, + onTap: () {}, + ), + ], + ); + }).toList(), + ); + } + + // DEFAULT VALUES + await tester.pumpWidget(MaterialApp( + home: Material(child: buildDefaultTable()), + )); + + // default checkbox padding + checkbox = find.byType(Checkbox).first; + padding = find.ancestor(of: checkbox, matching: find.byType(Padding)); + expect( + tester.getRect(checkbox).left - tester.getRect(padding).left, + _defaultHorizontalMargin, + ); + expect( + tester.getRect(padding).right - tester.getRect(checkbox).right, + _defaultHorizontalMargin / 2, + ); + + // default first column padding + padding = find.widgetWithText(Padding, 'Frozen yogurt'); + cellContent = find.widgetWithText(Align, 'Frozen yogurt'); // DataTable wraps its DataCells in an Align widget + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _defaultHorizontalMargin / 2, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _defaultColumnSpacing / 2, + ); + + // default middle column padding + padding = find.widgetWithText(Padding, '159'); + cellContent = find.widgetWithText(Align, '159'); + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _defaultColumnSpacing / 2, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _defaultColumnSpacing / 2, + ); + + // default last column padding + padding = find.widgetWithText(Padding, '6.0'); + cellContent = find.widgetWithText(Align, '6.0'); + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _defaultColumnSpacing / 2, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _defaultHorizontalMargin, + ); + + Widget buildCustomTable({ + int sortColumnIndex, + bool sortAscending = true, + double horizontalMargin, + double columnSpacing, + }) { + return DataTable( + sortColumnIndex: sortColumnIndex, + sortAscending: sortAscending, + onSelectAll: (bool value) {}, + horizontalMargin: horizontalMargin, + columnSpacing: columnSpacing, + columns: [ + const DataColumn( + label: Text('Name'), + tooltip: 'Name', + ), + DataColumn( + label: const Text('Calories'), + tooltip: 'Calories', + numeric: true, + onSort: (int columnIndex, bool ascending) {}, + ), + DataColumn( + label: const Text('Fat'), + tooltip: 'Fat', + numeric: true, + onSort: (int columnIndex, bool ascending) {}, + ), + ], + rows: kDesserts.map((Dessert dessert) { + return DataRow( + key: Key(dessert.name), + onSelectChanged: (bool selected) {}, + cells: [ + DataCell( + Text(dessert.name), + ), + DataCell( + Text('${dessert.calories}'), + showEditIcon: true, + onTap: () {}, + ), + DataCell( + Text('${dessert.fat}'), + showEditIcon: true, + onTap: () {}, + ), + ], + ); + }).toList(), + ); + } + + // CUSTOM VALUES + await tester.pumpWidget(MaterialApp( + home: Material(child: buildCustomTable( + horizontalMargin: _customHorizontalMargin, + columnSpacing: _customColumnSpacing, + )), + )); + + // custom checkbox padding + checkbox = find.byType(Checkbox).first; + padding = find.ancestor(of: checkbox, matching: find.byType(Padding)); + expect( + tester.getRect(checkbox).left - tester.getRect(padding).left, + _customHorizontalMargin, + ); + expect( + tester.getRect(padding).right - tester.getRect(checkbox).right, + _customHorizontalMargin / 2, + ); + + // custom first column padding + padding = find.widgetWithText(Padding, 'Frozen yogurt'); + cellContent = find.widgetWithText(Align, 'Frozen yogurt'); // DataTable wraps its DataCells in an Align widget + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _customHorizontalMargin / 2, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _customColumnSpacing / 2, + ); + + // custom middle column padding + padding = find.widgetWithText(Padding, '159'); + cellContent = find.widgetWithText(Align, '159'); + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _customColumnSpacing / 2, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _customColumnSpacing / 2, + ); + + // custom last column padding + padding = find.widgetWithText(Padding, '6.0'); + cellContent = find.widgetWithText(Align, '6.0'); + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _customColumnSpacing / 2, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _customHorizontalMargin, + ); + }); + + testWidgets('DataTable custom horizontal padding - no checkbox', (WidgetTester tester) async { + const double _defaultHorizontalMargin = 24.0; + const double _defaultColumnSpacing = 56.0; + const double _customHorizontalMargin = 10.0; + const double _customColumnSpacing = 15.0; + Finder cellContent; + Finder padding; + + Widget buildDefaultTable({ + int sortColumnIndex, + bool sortAscending = true, + }) { + return DataTable( + sortColumnIndex: sortColumnIndex, + sortAscending: sortAscending, + columns: [ + const DataColumn( + label: Text('Name'), + tooltip: 'Name', + ), + DataColumn( + label: const Text('Calories'), + tooltip: 'Calories', + numeric: true, + onSort: (int columnIndex, bool ascending) {}, + ), + DataColumn( + label: const Text('Fat'), + tooltip: 'Fat', + numeric: true, + onSort: (int columnIndex, bool ascending) {}, + ), + ], + rows: kDesserts.map((Dessert dessert) { + return DataRow( + key: Key(dessert.name), + cells: [ + DataCell( + Text(dessert.name), + ), + DataCell( + Text('${dessert.calories}'), + showEditIcon: true, + onTap: () {}, + ), + DataCell( + Text('${dessert.fat}'), + showEditIcon: true, + onTap: () {}, + ), + ], + ); + }).toList(), + ); + } + + // DEFAULT VALUES + await tester.pumpWidget(MaterialApp( + home: Material(child: buildDefaultTable()), + )); + + // default first column padding + padding = find.widgetWithText(Padding, 'Frozen yogurt'); + cellContent = find.widgetWithText(Align, 'Frozen yogurt'); // DataTable wraps its DataCells in an Align widget + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _defaultHorizontalMargin, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _defaultColumnSpacing / 2, + ); + + // default middle column padding + padding = find.widgetWithText(Padding, '159'); + cellContent = find.widgetWithText(Align, '159'); + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _defaultColumnSpacing / 2, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _defaultColumnSpacing / 2, + ); + + // default last column padding + padding = find.widgetWithText(Padding, '6.0'); + cellContent = find.widgetWithText(Align, '6.0'); + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _defaultColumnSpacing / 2, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _defaultHorizontalMargin, + ); + + Widget buildCustomTable({ + int sortColumnIndex, + bool sortAscending = true, + double horizontalMargin, + double columnSpacing, + }) { + return DataTable( + sortColumnIndex: sortColumnIndex, + sortAscending: sortAscending, + horizontalMargin: horizontalMargin, + columnSpacing: columnSpacing, + columns: [ + const DataColumn( + label: Text('Name'), + tooltip: 'Name', + ), + DataColumn( + label: const Text('Calories'), + tooltip: 'Calories', + numeric: true, + onSort: (int columnIndex, bool ascending) {}, + ), + DataColumn( + label: const Text('Fat'), + tooltip: 'Fat', + numeric: true, + onSort: (int columnIndex, bool ascending) {}, + ), + ], + rows: kDesserts.map((Dessert dessert) { + return DataRow( + key: Key(dessert.name), + cells: [ + DataCell( + Text(dessert.name), + ), + DataCell( + Text('${dessert.calories}'), + showEditIcon: true, + onTap: () {}, + ), + DataCell( + Text('${dessert.fat}'), + showEditIcon: true, + onTap: () {}, + ), + ], + ); + }).toList(), + ); + } + + // CUSTOM VALUES + await tester.pumpWidget(MaterialApp( + home: Material(child: buildCustomTable( + horizontalMargin: _customHorizontalMargin, + columnSpacing: _customColumnSpacing, + )), + )); + + // custom first column padding + padding = find.widgetWithText(Padding, 'Frozen yogurt'); + cellContent = find.widgetWithText(Align, 'Frozen yogurt'); // DataTable wraps its DataCells in an Align widget + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _customHorizontalMargin, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _customColumnSpacing / 2, + ); + + // custom middle column padding + padding = find.widgetWithText(Padding, '159'); + cellContent = find.widgetWithText(Align, '159'); + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _customColumnSpacing / 2, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _customColumnSpacing / 2, + ); + + // custom last column padding + padding = find.widgetWithText(Padding, '6.0'); + cellContent = find.widgetWithText(Align, '6.0'); + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _customColumnSpacing / 2, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _customHorizontalMargin, + ); + }); } diff --git a/packages/flutter/test/material/paginated_data_table_test.dart b/packages/flutter/test/material/paginated_data_table_test.dart index bc9211e055..43c84b4522 100644 --- a/packages/flutter/test/material/paginated_data_table_test.dart +++ b/packages/flutter/test/material/paginated_data_table_test.dart @@ -9,6 +9,12 @@ import 'package:flutter/gestures.dart' show DragStartBehavior; import 'data_table_test_utils.dart'; class TestDataSource extends DataTableSource { + TestDataSource({ + this.onSelectChanged, + }); + + final Function onSelectChanged; + int get generation => _generation; int _generation = 0; set generation(int value) { @@ -29,6 +35,7 @@ class TestDataSource extends DataTableSource { DataCell(Text('${dessert.calories}')), DataCell(Text('$generation')), ], + onSelectChanged: onSelectChanged, ); } @@ -289,4 +296,279 @@ void main() { expect(find.text('Rows per page:'), findsOneWidget); expect(tester.getTopLeft(find.text('Rows per page:')).dx, 18.0); // 14 padding in the footer row, 4 padding from the card }); + + testWidgets('PaginatedDataTable custom horizontal padding - checkbox', (WidgetTester tester) async { + const double _defaultHorizontalMargin = 24.0; + const double _defaultColumnSpacing = 56.0; + const double _customHorizontalMargin = 10.0; + const double _customColumnSpacing = 15.0; + final TestDataSource source = TestDataSource( + onSelectChanged: (bool value) {}, + ); + Finder cellContent; + Finder checkbox; + Finder padding; + + await tester.pumpWidget(MaterialApp( + home: PaginatedDataTable( + header: const Text('Test table'), + source: source, + rowsPerPage: 2, + availableRowsPerPage: const [ + 2, 4, + ], + onRowsPerPageChanged: (int rowsPerPage) {}, + onPageChanged: (int rowIndex) {}, + onSelectAll: (bool value) {}, + columns: const [ + DataColumn(label: Text('Name')), + DataColumn(label: Text('Calories'), numeric: true), + DataColumn(label: Text('Generation')), + ], + ), + )); + + // default checkbox padding + checkbox = find.byType(Checkbox).first; + padding = find.ancestor(of: checkbox, matching: find.byType(Padding)).first; + expect( + tester.getRect(checkbox).left - tester.getRect(padding).left, + _defaultHorizontalMargin, + ); + expect( + tester.getRect(padding).right - tester.getRect(checkbox).right, + _defaultHorizontalMargin / 2, + ); + + // default first column padding + padding = find.widgetWithText(Padding, 'Frozen yogurt (0)').first; + cellContent = find.widgetWithText(Align, 'Frozen yogurt (0)'); // DataTable wraps its DataCells in an Align widget + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _defaultHorizontalMargin / 2, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _defaultColumnSpacing / 2, + ); + + // default middle column padding + padding = find.widgetWithText(Padding, '159').first; + cellContent = find.widgetWithText(Align, '159'); + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _defaultColumnSpacing / 2, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _defaultColumnSpacing / 2, + ); + + // default last column padding + padding = find.widgetWithText(Padding, '0').first; + cellContent = find.widgetWithText(Align, '0').first; + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _defaultColumnSpacing / 2, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _defaultHorizontalMargin, + ); + + // CUSTOM VALUES + await tester.pumpWidget(MaterialApp( + home: Material( + child: PaginatedDataTable( + header: const Text('Test table'), + source: source, + rowsPerPage: 2, + availableRowsPerPage: const [ + 2, 4, + ], + onRowsPerPageChanged: (int rowsPerPage) {}, + onPageChanged: (int rowIndex) {}, + onSelectAll: (bool value) {}, + columns: const [ + DataColumn(label: Text('Name')), + DataColumn(label: Text('Calories'), numeric: true), + DataColumn(label: Text('Generation')), + ], + horizontalMargin: _customHorizontalMargin, + columnSpacing: _customColumnSpacing, + ), + ), + )); + + // custom checkbox padding + checkbox = find.byType(Checkbox).first; + padding = find.ancestor(of: checkbox, matching: find.byType(Padding)).first; + expect( + tester.getRect(checkbox).left - tester.getRect(padding).left, + _customHorizontalMargin, + ); + expect( + tester.getRect(padding).right - tester.getRect(checkbox).right, + _customHorizontalMargin / 2, + ); + + // custom first column padding + padding = find.widgetWithText(Padding, 'Frozen yogurt (0)').first; + cellContent = find.widgetWithText(Align, 'Frozen yogurt (0)'); // DataTable wraps its DataCells in an Align widget + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _customHorizontalMargin / 2, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _customColumnSpacing / 2, + ); + + // custom middle column padding + padding = find.widgetWithText(Padding, '159').first; + cellContent = find.widgetWithText(Align, '159'); + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _customColumnSpacing / 2, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _customColumnSpacing / 2, + ); + + // custom last column padding + padding = find.widgetWithText(Padding, '0').first; + cellContent = find.widgetWithText(Align, '0').first; + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _customColumnSpacing / 2, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _customHorizontalMargin, + ); + }); + + testWidgets('PaginatedDataTable custom horizontal padding - no checkbox', (WidgetTester tester) async { + const double _defaultHorizontalMargin = 24.0; + const double _defaultColumnSpacing = 56.0; + const double _customHorizontalMargin = 10.0; + const double _customColumnSpacing = 15.0; + final TestDataSource source = TestDataSource(); + Finder cellContent; + Finder padding; + + await tester.pumpWidget(MaterialApp( + home: PaginatedDataTable( + header: const Text('Test table'), + source: source, + rowsPerPage: 2, + availableRowsPerPage: const [ + 2, 4, 8, 16, + ], + onRowsPerPageChanged: (int rowsPerPage) {}, + onPageChanged: (int rowIndex) {}, + columns: const [ + DataColumn(label: Text('Name')), + DataColumn(label: Text('Calories'), numeric: true), + DataColumn(label: Text('Generation')), + ], + ), + )); + + // default first column padding + padding = find.widgetWithText(Padding, 'Frozen yogurt (0)').first; + cellContent = find.widgetWithText(Align, 'Frozen yogurt (0)'); // DataTable wraps its DataCells in an Align widget + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _defaultHorizontalMargin, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _defaultColumnSpacing / 2, + ); + + // default middle column padding + padding = find.widgetWithText(Padding, '159').first; + cellContent = find.widgetWithText(Align, '159'); + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _defaultColumnSpacing / 2, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _defaultColumnSpacing / 2, + ); + + // default last column padding + padding = find.widgetWithText(Padding, '0').first; + cellContent = find.widgetWithText(Align, '0').first; + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _defaultColumnSpacing / 2, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _defaultHorizontalMargin, + ); + + // CUSTOM VALUES + await tester.pumpWidget(MaterialApp( + home: Material( + child: PaginatedDataTable( + header: const Text('Test table'), + source: source, + rowsPerPage: 2, + availableRowsPerPage: const [ + 2, 4, 8, 16, + ], + onRowsPerPageChanged: (int rowsPerPage) {}, + onPageChanged: (int rowIndex) {}, + columns: const [ + DataColumn(label: Text('Name')), + DataColumn(label: Text('Calories'), numeric: true), + DataColumn(label: Text('Generation')), + ], + horizontalMargin: _customHorizontalMargin, + columnSpacing: _customColumnSpacing, + ), + ), + )); + + // custom first column padding + padding = find.widgetWithText(Padding, 'Frozen yogurt (0)').first; + cellContent = find.widgetWithText(Align, 'Frozen yogurt (0)'); + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _customHorizontalMargin, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _customColumnSpacing / 2, + ); + + // custom middle column padding + padding = find.widgetWithText(Padding, '159').first; + cellContent = find.widgetWithText(Align, '159'); + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _customColumnSpacing / 2, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _customColumnSpacing / 2, + ); + + // custom last column padding + padding = find.widgetWithText(Padding, '0').first; + cellContent = find.widgetWithText(Align, '0').first; + expect( + tester.getRect(cellContent).left - tester.getRect(padding).left, + _customColumnSpacing / 2, + ); + expect( + tester.getRect(padding).right - tester.getRect(cellContent).right, + _customHorizontalMargin, + ); + }); }