Category / Section
How to enable scrolling with shrinkWrapColumns and shrinkWrapRows enabled in Flutter DataTable (SfDataGrid)?
2 mins read
In this article, we will show you how to enable Horizontal and Vertical scrolling with shrinkWrapColumns and shrinkWrapRows enabled in Flutter DataTable.
Initialize the SfDataGrid widget with all necessary properties. When shrinkWrapColumns and shrinkWrapRows are enabled, the DataGrid sets its maximum width and height based on the available columns and rows, causing all columns and rows to be laid out. To enable scrolling, wrap the DataGrid in a SingleChildScrollView and set the respective scrollDirection. Additionally, add a ScrollConfiguration widget as the parent and set the ScrollConfiguration.behavior property to ensure the scrollbar remains in view.
class DataGridScrollBehavior extends MaterialScrollBehavior {
@override
Widget buildScrollbar(
BuildContext context, Widget child, ScrollableDetails details) {
return Scrollbar(
controller: details.controller,
child: child,
);
}
}
class _MyHomePageState extends State<MyHomePage> {
….
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Syncfusion Flutter DataGrid'),
),
body: ScrollConfiguration(
behavior: DataGridScrollBehavior(),
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: SfDataGrid(
source: employeeDataSource,
shrinkWrapColumns: true,
shrinkWrapRows: true,
columns: getColumns,
),
),
),
),
);
}
}
You can download this example in GitHub.