-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Description
Use case
Hi,
I want to override the scrolling functionality for a PaginatedDataTable. I'm using flutter web and currently it is not possible to scroll a table horizontally on desktop devices without a scroll pad. So I was trying to enable horizontal scrolling via the arrow left and arrow right keys using a RawKeyboardListener that tells my ScrollController to scroll. Unfortunately, one cannot simply wrap the PaginatedDataTable in a SingleChildScrollView, since it destroys the layout, giving the PaginatedDataTable an unbounded width it cannot handle, since it is trying to fill out the space it has available.
The following code throws an error:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SizedBox(
width: 200,
// A `PaginatedDataTable` cannot be wrapped by a
// `SingleChildScrollView`, because it provides its child
// with an unbounded width.
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: PaginatedDataTable(
columns: <DataColumn>[
DataColumn(
label: Text("1"),
),
DataColumn(
label: Text("2"),
),
],
source: CustomDataSource(),
),
),
),
),
);
}
}
class CustomDataSource extends DataTableSource {
@override
bool get isRowCountApproximate => false;
@override
int get rowCount => 1;
@override
int get selectedRowCount => 0;
@override
DataRow? getRow(int index) {
return DataRow(
cells: <DataCell>[
DataCell(
Text(
"Some very long cell that is wider than 200 logical pixels, the size available to the table",
),
),
DataCell(
Text(
"Same as the first cell, again some very long cell definetly forcing the table to be wider than 200 logical pixels",
),
),
],
);
}
}The error:
══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
The following assertion was thrown during performLayout():
BoxConstraints forces an infinite width.
The offending constraints were:
BoxConstraints(w=Infinity, 0.0<=h<=Infinity)
The relevant error-causing widget was:
PaginatedDataTable
PaginatedDataTable:file:///home/masterusr/src/flutter_issues/pdt_horizontal_scrolling/lib/main.dart:21:20
Proposal
The content of a PaginatedDataTable is wrapped in a SingleChildScrollView (see here).
I propose to add a ScrollController? scrollController parameter to the PaginatedDataTable constructor that is then provided to the SingleChildScrollView. This should allow me and others to override the horizontal scrolling behavior of the contents of a PaginatedDataTable.