How to add column in multi column header of Flutter DataTable (SfDataGrid) at run time?
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()), ], ), ); }
You can download the example in GitHub.