Articles in this section
Category / Section

How to add column in multi column header of Flutter DataGrid?

4 mins read

The Syncfusion® Flutter DataTable widget provides the support to add multiple column headers is known as stacked header rows. You can add the stacked header rows by using the stackedHeaderRows property.

 

The following steps explains how to add row in stacked header collection at run time,

 

STEP 1: Initialize the SfDataGrid widget with all the required properties and assign the collection to stackedHeaderRows property.

 

  StackedHeaderRow stackedHeaderRow = StackedHeaderRow(cells: [
    StackedHeaderCell(
        columnNames: ['customerName', 'city'],
        child: Container(
            color: const Color(0xFFF1F1F1),
            child: Center(child: Text('Customer Details')))),
    StackedHeaderCell(
        columnNames: ['productId', 'product'],
        child: Container(
            color: const Color(0xFFF1F1F1),
            child: Center(child: Text('Product Details'))))
  ]);
 
 
 
  List<StackedHeaderRow> _getStackedHeaderRows() {
    final List<StackedHeaderRow> stackedHeaderCollection = [];
    stackedHeaderCollection.add(stackedHeaderRow);
    if (orderRow != null) {
      stackedHeaderCollection.insert(0, orderRow!);
    }
    return stackedHeaderCollection;
  }
 
 
  SfDataGridTheme _buildDataGrid() {
    return SfDataGridTheme(
        data: SfDataGridThemeData(
          headerColor: _getHeaderCellBackgroundColor(),
        ),
        child: SfDataGrid(
          gridLinesVisibility: GridLinesVisibility.vertical,
          headerGridLinesVisibility: GridLinesVisibility.both,
          source: stackedHeaderDataGridSource,
          columns: [
            GridTextColumn(
                columnName: 'customerName',
                width: 140,
                label: Container(
                  alignment: Alignment.centerLeft,
                  padding: EdgeInsets.all(8.0),
                  child: Text(
                    'Customer Name',
                    overflow: TextOverflow.ellipsis,
                  ),
                  color: _getHeaderCellBackgroundColor(),
                )),
            GridTextColumn(
                columnName: 'city',
                width: 100,
                label: Container(
                  alignment: Alignment.centerLeft,
                  padding: EdgeInsets.all(8.0),
                  child: Text(
                    'City',
                    overflow: TextOverflow.ellipsis,
                  ),
                  color: _getHeaderCellBackgroundColor(),
                )),
            GridTextColumn(
                columnName: 'product',
                width: 100,
                label: Container(
                  alignment: Alignment.centerLeft,
                  padding: EdgeInsets.all(8.0),
                  child: Text(
                    'Product',
                    overflow: TextOverflow.ellipsis,
                  ),
                  color: _getHeaderCellBackgroundColor(),
                )),
            GridTextColumn(
                columnName: 'productId',
                width: 100,
                label: Container(
                  alignment: Alignment.centerRight,
                  padding: EdgeInsets.all(8.0),
                  child: Text(
                    'Product ID',
                    overflow: TextOverflow.ellipsis,
                  ),
                  color: _getHeaderCellBackgroundColor(),
                )),
          ],
          stackedHeaderRows: _getStackedHeaderRows(),
        ));
  }

 

 

STEP 2: Add another stacked header row to collection from onPressed callback of the MaterialButton. In this callback, we are just creating the new stacked header row and calling the setState method. When calling the setState method, the created stacked header row is added to the existing collection. 

 

 

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Stacked Header Demo')),
      body: Column(
        children: [
          MaterialButton (
              color: Colors.blue[200],
              onPressed: () {
                orderRow = StackedHeaderRow(cells: [
                  StackedHeaderCell(
                      columnNames: [
                        'customerName',
                        'city',
                        'product',
                        'productId',
                      ],
                      child: Container(
                          color: const Color(0xFFF1F1F1),
                          child: Center(child: Text('Order Shipment Details'))))
                ]);
                setState(() {});
              },
              child: Text('Add Stacked Header Row')),
          Expanded(child: _buildDataGrid()),
        ],
      ),
    );
  }

 

TableDescription automatically generated

 

View the GitHub sample here.


Conclusion

I hope you enjoyed learning about how to add column in multi column header of Flutter DataGrid.

You can refer to our Flutter DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our Flutter DataGrid documentation 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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied