Unverified Commit d907a262 authored by Ben Konyi's avatar Ben Konyi Committed by GitHub

Fixed issue where PaginatedDataTable would not fill the width of its containing Card (#48531)

parent 31f399f9
...@@ -426,6 +426,8 @@ class PaginatedDataTableState extends State<PaginatedDataTable> { ...@@ -426,6 +426,8 @@ class PaginatedDataTableState extends State<PaginatedDataTable> {
]); ]);
// CARD // CARD
return LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Card( return Card(
semanticContainer: false, semanticContainer: false,
child: Column( child: Column(
...@@ -460,6 +462,8 @@ class PaginatedDataTableState extends State<PaginatedDataTable> { ...@@ -460,6 +462,8 @@ class PaginatedDataTableState extends State<PaginatedDataTable> {
SingleChildScrollView( SingleChildScrollView(
scrollDirection: Axis.horizontal, scrollDirection: Axis.horizontal,
dragStartBehavior: widget.dragStartBehavior, dragStartBehavior: widget.dragStartBehavior,
child: ConstrainedBox(
constraints: BoxConstraints(minWidth: constraints.minWidth),
child: DataTable( child: DataTable(
key: _tableKey, key: _tableKey,
columns: widget.columns, columns: widget.columns,
...@@ -470,10 +474,10 @@ class PaginatedDataTableState extends State<PaginatedDataTable> { ...@@ -470,10 +474,10 @@ class PaginatedDataTableState extends State<PaginatedDataTable> {
headingRowHeight: widget.headingRowHeight, headingRowHeight: widget.headingRowHeight,
horizontalMargin: widget.horizontalMargin, horizontalMargin: widget.horizontalMargin,
columnSpacing: widget.columnSpacing, columnSpacing: widget.columnSpacing,
showCheckboxColumn: widget.showCheckboxColumn,
rows: _getRows(_firstRowIndex, widget.rowsPerPage), rows: _getRows(_firstRowIndex, widget.rowsPerPage),
), ),
), ),
),
DefaultTextStyle( DefaultTextStyle(
style: footerTextStyle, style: footerTextStyle,
child: IconTheme.merge( child: IconTheme.merge(
...@@ -481,6 +485,7 @@ class PaginatedDataTableState extends State<PaginatedDataTable> { ...@@ -481,6 +485,7 @@ class PaginatedDataTableState extends State<PaginatedDataTable> {
opacity: 0.54 opacity: 0.54
), ),
child: Container( child: Container(
// TODO(bkonyi): this won't handle text zoom correctly, https://github.com/flutter/flutter/issues/48522
height: 56.0, height: 56.0,
child: SingleChildScrollView( child: SingleChildScrollView(
dragStartBehavior: widget.dragStartBehavior, dragStartBehavior: widget.dragStartBehavior,
...@@ -496,5 +501,7 @@ class PaginatedDataTableState extends State<PaginatedDataTable> { ...@@ -496,5 +501,7 @@ class PaginatedDataTableState extends State<PaginatedDataTable> {
], ],
), ),
); );
},
);
} }
} }
...@@ -50,6 +50,9 @@ class TestDataSource extends DataTableSource { ...@@ -50,6 +50,9 @@ class TestDataSource extends DataTableSource {
} }
void main() { void main() {
final TestWidgetsFlutterBinding binding =
TestWidgetsFlutterBinding.ensureInitialized() as TestWidgetsFlutterBinding;
testWidgets('PaginatedDataTable paging', (WidgetTester tester) async { testWidgets('PaginatedDataTable paging', (WidgetTester tester) async {
final TestDataSource source = TestDataSource(); final TestDataSource source = TestDataSource();
...@@ -382,6 +385,16 @@ void main() { ...@@ -382,6 +385,16 @@ void main() {
const double _defaultColumnSpacing = 56.0; const double _defaultColumnSpacing = 56.0;
const double _customHorizontalMargin = 10.0; const double _customHorizontalMargin = 10.0;
const double _customColumnSpacing = 15.0; const double _customColumnSpacing = 15.0;
const double _width = 400;
const double _height = 400;
final Size originalSize = binding.renderView.size;
// Ensure the containing Card is small enough that we don't expand too
// much, resulting in our custom margin being ignored.
await binding.setSurfaceSize(const Size(_width, _height));
final TestDataSource source = TestDataSource( final TestDataSource source = TestDataSource(
onSelectChanged: (bool value) {}, onSelectChanged: (bool value) {},
); );
...@@ -527,6 +540,9 @@ void main() { ...@@ -527,6 +540,9 @@ void main() {
tester.getRect(padding).right - tester.getRect(cellContent).right, tester.getRect(padding).right - tester.getRect(cellContent).right,
_customHorizontalMargin, _customHorizontalMargin,
); );
// Reset the surface size.
await binding.setSurfaceSize(originalSize);
}); });
testWidgets('PaginatedDataTable custom horizontal padding - no checkbox', (WidgetTester tester) async { testWidgets('PaginatedDataTable custom horizontal padding - no checkbox', (WidgetTester tester) async {
...@@ -651,4 +667,63 @@ void main() { ...@@ -651,4 +667,63 @@ void main() {
_customHorizontalMargin, _customHorizontalMargin,
); );
}); });
testWidgets('PaginatedDataTable table fills Card width', (WidgetTester tester) async {
final TestDataSource source = TestDataSource();
// Note: 800 is wide enough to ensure that all of the columns fit in the
// Card. The DataTable can be larger than its containing Card, but this test
// is only concerned with ensuring the DataTable is at least as wide as the
// Card.
const double _originalWidth = 800;
const double _expandedWidth = 1600;
const double _height = 400;
final Size originalSize = binding.renderView.size;
Widget buildWidget() => MaterialApp(
home: PaginatedDataTable(
header: const Text('Test table'),
source: source,
rowsPerPage: 2,
availableRowsPerPage: const <int>[
2, 4, 8, 16,
],
onRowsPerPageChanged: (int rowsPerPage) {},
onPageChanged: (int rowIndex) {},
columns: const <DataColumn>[
DataColumn(label: Text('Name')),
DataColumn(label: Text('Calories'), numeric: true),
DataColumn(label: Text('Generation')),
],
),
);
await binding.setSurfaceSize(const Size(_originalWidth, _height));
await tester.pumpWidget(buildWidget());
// Widths should be equal before we resize...
expect(
tester.renderObject<RenderBox>(find.byType(DataTable).first).size.width,
moreOrLessEquals(
tester.renderObject<RenderBox>(find.byType(Card).first).size.width)
);
await binding.setSurfaceSize(const Size(_expandedWidth, _height));
await tester.pumpWidget(buildWidget());
final double cardWidth = tester.renderObject<RenderBox>(find.byType(Card).first).size.width;
// ... and should still be equal after the resize.
expect(
tester.renderObject<RenderBox>(find.byType(DataTable).first).size.width,
moreOrLessEquals(cardWidth)
);
// Double check to ensure we actually resized the surface properly.
expect(cardWidth, moreOrLessEquals(_expandedWidth));
// Reset the surface size.
await binding.setSurfaceSize(originalSize);
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment