Introducing Pagination in the Syncfusion Flutter DataGrid | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (172).NET Core  (29).NET MAUI  (192)Angular  (107)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (40)Black Friday Deal  (1)Blazor  (209)BoldSign  (12)DocIO  (24)Essential JS 2  (106)Essential Studio  (200)File Formats  (63)Flutter  (131)JavaScript  (219)Microsoft  (118)PDF  (80)Python  (1)React  (98)Streamlit  (1)Succinctly series  (131)Syncfusion  (882)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (49)Windows Forms  (61)WinUI  (68)WPF  (157)Xamarin  (161)XlsIO  (35)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (146)Chart  (125)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (62)Development  (613)Doc  (7)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (37)Extensions  (22)File Manager  (6)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (488)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (41)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (368)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (30)Visual Studio Code  (17)Web  (577)What's new  (313)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Introducing Pagination in the Syncfusion Flutter DataGrid

Introducing Pagination in the Syncfusion Flutter DataGrid

Paging is an important feature for loading large amounts of data and displaying it instantly in the DataGrid widget. It also provides easy navigation through the data. We at Syncfusion have developed the Syncfusion Flutter Data Pager widget to achieve pagination in the Flutter DataGrid widget. You can get this Data Pager in our 2020 Volume 3 release.

Let’s discuss in this blog how to integrate the SfDataPager with SfDataGrid and the customization options available in the Data Pager widget.

Integrating SfDataPager into SfDataGrid

Step 1: Include the Syncfusion Flutter DataGrid package dependency in the pubspec.yaml file of your project with the following code.

syncfusion_flutter_datagrid: ^18.3.35-beta

Step 2: Import the DataGrid package in the main.dart file using the following code example.

import 'package:syncfusion_flutter_datagrid/datagrid.dart';

Step 3: Create a common delegate for both the SfDataPager and SfDataGrid and do the following. Please note that, by default, DataGridSource is extended with the DataPagerDelegate.

  1. Set the SfDataGrid.DataGridSource to the SfDataPager.delegate property.
  2. Set the number of items that should be displayed in view by setting the SfDataPager.visibleItemsCount property.
  3. Override the SfDataPager.delegate.rowCount property and SfDataPager.delegate.handlePageChanges method in the SfDataGrid.DataGridSource.
  4. You can also load data for a specific page using the handlePageChanges method. This method is called for every page navigation in the Data Pager.
    Refer to the following code example.
class OrderInfoDataSource extends DataGridSource<OrderInfo> {
  @override
  List<OrderInfo> get dataSource => paginatedDataSource;
 
  @override
  Object getValue(OrderInfo orderInfos, String columnName) {
    switch (columnName) {
      case 'orderID':
        return orderInfos.orderID;
        break;
      case 'customerID':
        return orderInfos.customerID;
        break;
      case 'freight':
        return orderInfos.freight;
        break;
      case 'orderDate':
        return orderInfos.orderData;
        break;
      default:
        return '';
        break;
    }
  }
 
  @override
  int get rowCount => orders.length;
 
  @override
  Future<bool> handlePageChange(int oldPageIndex, int newPageIndex,
      int startRowIndex, int rowsPerPage) async {
    int endIndex = startRowIndex + rowsPerPage;
    if (endIndex > orders.length) {
      endIndex = orders.length - 1;
    }
 
    paginatedDataSource = List.from(
        orders.getRange(startRowIndex, endIndex).toList(growable: false));
    notifyListeners();
    return true;
  }
}

Step 4: Create an instance of OrderInfoDataSource and assign it to DataGrid’s source and Data Pager’s delegate properties.
Refer to the following code example.

List<OrderInfo> orders = [];
List<OrderInfo> paginatedDataSource = [];
 
static const double dataPagerHeight = 60;
 
final _OrderInfoRepository _repository = _OrderInfoRepository();
final OrderInfoDataSource _orderInfoDataSource = OrderInfoDataSource();
 
@override
void initState() {
  super.initState();
  orders = _repository.getOrderDetails(300);
}
  
@override
Widget build(BuildContext context) {
  return MaterialApp(
      title: 'Paginated SfDataGrid',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Paginated SfDataGrid'),
        ),
        body: LayoutBuilder(builder: (context, constraint) {
          return Column(
            children: [
              SizedBox(
                  height: constraint.maxHeight - dataPagerHeight,
                  width: constraint.maxWidth,
                  child: SfDataGrid(
                      source: _orderInfoDataSource,
                      columnWidthMode: ColumnWidthMode.fill,
                      columns: <GridColumn>[
                        GridNumericColumn(
                            mappingName: 'orderID', headerText: 'Order ID'),
                        GridTextColumn(
                            mappingName: 'customerID',
                            headerText: 'Customer Name'),
                        GridDateTimeColumn(
                            mappingName: 'orderDate',
                            headerText: 'Order Date'),
                        GridNumericColumn(
                            mappingName: 'freight', headerText: 'Freight'),
                      ])),
              Container(
                height: dataPagerHeight,
                color: Colors.white,
                child: SfDataPager(
                  delegate: _orderInfoDataSource,
                  rowsPerPage: 20,
                  direction: Axis.horizontal,
                ),
              )
            ],
          );
        }),
      ));
}

After executing this code example, we will get output like in the following GIF image.

Pagination feature in Flutter DataGrid
Pagination feature in Flutter DataGrid

Appearance customization of Data Pager

Data Pager allows you to customize the appearance of its elements using SfDataPagerThemeData in the SfDataPagerTheme property. To do this, the SfDataPager should be wrapped inside the SfDataPagerTheme.

Follow these steps to customize the Data Pager:

Step 1: Import the theme package in the main.dart file using the following code.

import 'package:syncfusion_flutter_core/theme.dart';

Step 2: Add the SfDataPager widget inside the SfDataGridTheme like in the following code example.

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: SfDataPagerTheme(
    data: SfDataPagerThemeData(
      itemColor: Colors.white,
      selectedItemColor: Colors.lightGreen,
      itemBorderRadius: BorderRadius.circular(5),
      backgroundColor: Colors.teal,
    ),
    child: SfDataPager(
      delegate: _orderInfoDataSource,
      rowsPerPage: 20,
      direction: Axis.horizontal,
    ),
  ));
}

After executing this code example, we will get output like in the following screenshot.

Customized Data Pager widget
Customized Data Pager widget

Conclusion

In this blog, we have seen the steps to integrate the Syncfusion Flutter DataGrid with the new Data Pager. This feature helps you to easily navigate to required data. You can get the complete source code for the example used in this blog in this GitHub repository.

So, try it out and leave your feedback in the comments section below!

Syncfusion Flutter widgets offer fast, fluid, and flexible widgets for creating high-quality apps for iOS, Android, and the web. Use them to enhance your productivity!

For existing customers, the new version is available for download from the License and Downloads page. If you are not yet a Syncfusion customer, you can try our 30-day free trial to check out our available features. Also, try our samples from this GitHub location.

You can also reach us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

googleplay.png

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed
Scroll To Top