How to add multiple Flutter DataGrid (SfDataGrid) to a page with pagination?
In this article, we will show you how to add multiple DataGrid to a page with pagination in Flutter DataGrid.
Initialize the SfDataGrid widget with all the required properties. You can add multiple SfDataGrid widgets to a page with pagination using the PageView widget, which allows users to swipe between different pages. The PageController manages the currently visible page and facilitates switching between pages that contain multiple grids.
Step 1:
@override
Widget build(BuildContext context) {
return Stack(
alignment: Alignment.bottomCenter,
children: <Widget>[
PageView(
controller: _pageViewController,
onPageChanged: _handlePageViewChanged,
children: <Widget>[
_buildPage(employeeDataSource, _currentPageIndex),
_buildPage(employeeDataSource, _currentPageIndex),
],
),
PageIndicator(
tabController: _tabController,
currentPageIndex: _currentPageIndex,
onUpdateCurrentPageIndex: _updateCurrentPageIndex,
isOnDesktopAndWeb: _isOnDesktopAndWeb,
),
],
);
}
Widget _buildPage(EmployeeDataSource dataSource, int pageTitle) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Center(
child: Container(
padding: const EdgeInsets.all(16.0),
child: Text('Page ${pageTitle + 1}',
style: const TextStyle(fontWeight: FontWeight.w500)),
),
),
Expanded(
child: Column(
children: [
Flexible(
child: _buildDataGrid(dataSource, 'DataGrid 1'),
),
Flexible(
child: _buildDataGrid(dataSource, 'DataGrid 2'),
),
],
),
),
],
);
}
Step 2
For visual page indication, the PageIndicator includes a TabPageSelector, along with arrow buttons for navigating between pages.
/// Page indicator for desktop and web platforms.
///
/// On Desktop and Web, drag gesture for horizontal scrolling in a PageView is disabled by default.
class PageIndicator extends StatelessWidget {
const PageIndicator({
super.key,
required this.tabController,
required this.currentPageIndex,
required this.onUpdateCurrentPageIndex,
required this.isOnDesktopAndWeb,
});
final int currentPageIndex;
final TabController tabController;
final void Function(int) onUpdateCurrentPageIndex;
final bool isOnDesktopAndWeb;
@override
Widget build(BuildContext context) {
if (!isOnDesktopAndWeb) {
return const SizedBox.shrink();
}
final ColorScheme colorScheme = Theme.of(context).colorScheme;
return Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
IconButton(
splashRadius: 16.0,
padding: EdgeInsets.zero,
onPressed: () {
if (currentPageIndex == 0) {
return;
}
onUpdateCurrentPageIndex(currentPageIndex - 1);
},
icon: const Icon(
Icons.arrow_left_rounded,
size: 32.0,
),
),
TabPageSelector(
controller: tabController,
color: colorScheme.surface,
selectedColor: colorScheme.primary,
),
IconButton(
splashRadius: 16.0,
padding: EdgeInsets.zero,
onPressed: () {
if (currentPageIndex == 1) {
return;
}
onUpdateCurrentPageIndex(currentPageIndex + 1);
},
icon: const Icon(
Icons.arrow_right_rounded,
size: 32.0,
),
),
],
),
);
}
}
You can download this example on GitHub
Conclusion
I hope you enjoyed learning how to add multiple Flutter DataGrid to a page with pagination.
You can refer to our Flutter DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Flatter DataGrid example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!