Fixed : Empty Rows shown at last page in Paginated data table (#132646)
Fixed empty rows at the last page in paginated data table BEFORE:  AFTER:  Issues resolves : #80421
This commit is contained in:
@@ -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<Color?>? 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<PaginatedDataTable> {
|
||||
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<PaginatedDataTable> {
|
||||
),
|
||||
),
|
||||
),
|
||||
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(
|
||||
|
||||
@@ -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 <int>[
|
||||
3, 6, 7, 8, 9,
|
||||
],
|
||||
onRowsPerPageChanged: (int? rowsPerPage) {
|
||||
},
|
||||
onPageChanged: (int rowIndex) {
|
||||
},
|
||||
columns: const <DataColumn>[
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user