diff --git a/packages/flutter/lib/src/material/paginated_data_table.dart b/packages/flutter/lib/src/material/paginated_data_table.dart index 45eb851d77..b3a2c7a73f 100644 --- a/packages/flutter/lib/src/material/paginated_data_table.dart +++ b/packages/flutter/lib/src/material/paginated_data_table.dart @@ -112,6 +112,7 @@ class PaginatedDataTable extends StatefulWidget { this.controller, this.primary, this.headingRowColor, + this.showEmptyRows = true, }) : assert(actions == null || (header != null)), assert(columns.isNotEmpty), assert(sortColumnIndex == null || (sortColumnIndex >= 0 && sortColumnIndex < columns.length)), @@ -289,6 +290,14 @@ class PaginatedDataTable extends StatefulWidget { /// {@macro flutter.material.dataTable.headingRowColor} final MaterialStateProperty? headingRowColor; + /// Controls the visibility of empty rows on the last page of a + /// [PaginatedDataTable]. + /// + /// Defaults to `true`, which means empty rows will be populated on the + /// last page of the table if there is not enough content. + /// When set to `false`, empty rows will not be created. + final bool showEmptyRows; + @override PaginatedDataTableState createState() => PaginatedDataTableState(); } @@ -407,8 +416,14 @@ class PaginatedDataTableState extends State { haveProgressIndicator = true; } } - row ??= _getBlankRowFor(index); - result.add(row); + + if (widget.showEmptyRows) { + row ??= _getBlankRowFor(index); + } + + if (row != null) { + result.add(row); + } } return result; } @@ -600,6 +615,9 @@ class PaginatedDataTableState extends State { ), ), ), + if (!widget.showEmptyRows) + SizedBox( + height: (widget.dataRowMaxHeight ?? kMinInteractiveDimension) * (widget.rowsPerPage - _rowCount + _firstRowIndex).clamp(0, widget.rowsPerPage)), // ignore_clamp_double_lint DefaultTextStyle( style: footerTextStyle!, child: IconTheme.merge( diff --git a/packages/flutter/test/material/paginated_data_table_test.dart b/packages/flutter/test/material/paginated_data_table_test.dart index 8004dc5312..3cf4452053 100644 --- a/packages/flutter/test/material/paginated_data_table_test.dart +++ b/packages/flutter/test/material/paginated_data_table_test.dart @@ -303,6 +303,83 @@ void main() { expect(find.text('497–500 of 500'), findsOneWidget); }); + testWidgetsWithLeakTracking('PaginatedDataTable Last Page Empty Space', (WidgetTester tester) async { + final TestDataSource source = TestDataSource(); + int rowsPerPage = 3; + final int rowCount = source.rowCount; + addTearDown(source.dispose); + + Widget buildTable(TestDataSource source, int rowsPerPage) { + return PaginatedDataTable( + header: const Text('Test table'), + source: source, + rowsPerPage: rowsPerPage, + showFirstLastButtons: true, + dataRowHeight: 46, + availableRowsPerPage: const [ + 3, 6, 7, 8, 9, + ], + onRowsPerPageChanged: (int? rowsPerPage) { + }, + onPageChanged: (int rowIndex) { + }, + columns: const [ + DataColumn(label: Text('Name')), + DataColumn(label: Text('Calories'), numeric: true), + DataColumn(label: Text('Generation')), + ], + showEmptyRows: false, + ); + } + + await tester.pumpWidget(MaterialApp( + home: Scaffold(body: Center(child: buildTable(source, rowsPerPage))) + )); + + expect(find.byWidgetPredicate((Widget widget) => widget is SizedBox && widget.height == 0), findsOneWidget); + await tester.tap(find.byIcon(Icons.skip_next)); + await tester.pump(); + expect(find.byWidgetPredicate((Widget widget) => widget is SizedBox && widget.height == (rowsPerPage - (rowCount % rowsPerPage)) * 46.0), findsOneWidget); + + rowsPerPage = 6; + await tester.pumpWidget(MaterialApp( + home: buildTable(source, rowsPerPage) + )); + + await tester.tap(find.byIcon(Icons.skip_previous)); + await tester.pump(); + expect(find.byWidgetPredicate((Widget widget) => widget is SizedBox && widget.height == 0), findsOneWidget); + await tester.tap(find.byIcon(Icons.skip_next)); + await tester.pump(); + expect(find.byWidgetPredicate((Widget widget) => widget is SizedBox && widget.height == (rowsPerPage - (rowCount % rowsPerPage)) * 46.0), findsOneWidget); + + rowsPerPage = 7; + + await tester.pumpWidget(MaterialApp( + home: buildTable(source, rowsPerPage) + )); + await tester.tap(find.byIcon(Icons.skip_previous)); + await tester.pump(); + + expect(find.byWidgetPredicate((Widget widget) => widget is SizedBox && widget.height == 0), findsOneWidget); + await tester.tap(find.byIcon(Icons.skip_next)); + await tester.pump(); + expect(find.byWidgetPredicate((Widget widget) => widget is SizedBox && widget.height == (rowsPerPage - (rowCount % rowsPerPage)) * 46.0), findsOneWidget); + + rowsPerPage = 8; + + await tester.pumpWidget(MaterialApp( + home: buildTable(source, rowsPerPage) + )); + await tester.tap(find.byIcon(Icons.skip_previous)); + await tester.pump(); + + expect(find.byWidgetPredicate((Widget widget) => widget is SizedBox && widget.height == 0), findsOneWidget); + await tester.tap(find.byIcon(Icons.skip_next)); + await tester.pump(); + expect(find.byWidgetPredicate((Widget widget) => widget is SizedBox && widget.height == (rowsPerPage - (rowCount % rowsPerPage)) * 46.0), findsOneWidget); + }); + testWidgetsWithLeakTracking('PaginatedDataTable control test', (WidgetTester tester) async { TestDataSource source = TestDataSource() ..generation = 42;