|
1 |
| -# How-to-retrieve-additional-row-details-by-expanding-a-cell-in-Flutter-DataTable |
2 |
| -How to retrieve additional row details by expanding a cell in Flutter DataTable |
| 1 | +# How to retrieve additional row details by expanding a cell in Flutter DataGrid. |
| 2 | + |
| 3 | +In this article, we will show you how to retrieve additional row details by expanding a cell in [Flutter DataGrid](https://www.syncfusion.com/flutter-widgets/flutter-datagrid). |
| 4 | + |
| 5 | +Initialize the [SfDataGrid](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid-class.html) widget with all the required properties. In the [buildRow](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource/buildRow.html) method, load the cell value along with the icon button, and call [showDialog](https://api.flutter.dev/flutter/material/showDialog.html). The [onPressed](https://api.flutter.dev/flutter/material/IconButton/onPressed.html) event of the IconButton in the buildRow method is where the dialog is triggered. This dialog displays the expanded details. |
| 6 | + |
| 7 | +```dart |
| 8 | +class EmployeeDataSource extends DataGridSource { |
| 9 | +
|
| 10 | +….. |
| 11 | +
|
| 12 | + @override |
| 13 | + DataGridRowAdapter buildRow(DataGridRow row) { |
| 14 | + return DataGridRowAdapter( |
| 15 | + cells: row.getCells().map<Widget>((DataGridCell cell) { |
| 16 | + return Container( |
| 17 | + alignment: Alignment.center, |
| 18 | + child: cell.columnName == 'Name' |
| 19 | + ? Row( |
| 20 | + children: [ |
| 21 | + Expanded( |
| 22 | + child: Container( |
| 23 | + alignment: Alignment.centerLeft, |
| 24 | + child: Text( |
| 25 | + cell.value.toString(), |
| 26 | + ), |
| 27 | + ), |
| 28 | + ), |
| 29 | + SizedBox( |
| 30 | + width: 40, |
| 31 | + child: Builder(builder: (context) { |
| 32 | + datagridRows.indexOf(row); |
| 33 | + return IconButton( |
| 34 | + hoverColor: Colors.transparent, |
| 35 | + color: Colors.blue, |
| 36 | + icon: const Icon(Icons.expand_more), |
| 37 | + onPressed: () => _showDetailsDialog(context, row), |
| 38 | + ); |
| 39 | + }), |
| 40 | + ) |
| 41 | + ], |
| 42 | + ) |
| 43 | + : Container( |
| 44 | + padding: const EdgeInsets.symmetric(horizontal: 8.0), |
| 45 | + alignment: |
| 46 | + cell.columnName == 'ID' || cell.columnName == 'Salary' |
| 47 | + ? Alignment.centerRight |
| 48 | + : Alignment.centerLeft, |
| 49 | + child: Text( |
| 50 | + cell.value.toString(), |
| 51 | + ), |
| 52 | + ), |
| 53 | + ); |
| 54 | + }).toList()); |
| 55 | + } |
| 56 | +
|
| 57 | + void _showDetailsDialog(BuildContext context, DataGridRow row) { |
| 58 | + int rowIndex = datagridRows.indexOf(row); |
| 59 | + showDialog<String>( |
| 60 | + context: context, |
| 61 | + builder: (BuildContext context) => AlertDialog( |
| 62 | + scrollable: true, |
| 63 | + titleTextStyle: const TextStyle( |
| 64 | + color: Colors.black, |
| 65 | + fontWeight: FontWeight.bold, |
| 66 | + fontSize: 16, |
| 67 | + ), |
| 68 | + title: const Text('Personal Details'), |
| 69 | + actions: [ |
| 70 | + TextButton( |
| 71 | + onPressed: () => Navigator.pop(context), |
| 72 | + child: const Text('Close'), |
| 73 | + ), |
| 74 | + ], |
| 75 | + content: Padding( |
| 76 | + padding: const EdgeInsets.all(20.0), |
| 77 | + child: Column( |
| 78 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 79 | + children: [ |
| 80 | + Text('Employee address: ${employees[rowIndex].address}'), |
| 81 | + const SizedBox(height: 10), |
| 82 | + Text('City: ${employees[rowIndex].city}'), |
| 83 | + const SizedBox(height: 10), |
| 84 | + Text('Country: ${employees[rowIndex].country}'), |
| 85 | + ], |
| 86 | + ), |
| 87 | + ), |
| 88 | + ), |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +You can download this example on [GitHub](https://github.com/SyncfusionExamples/How-to-retrieve-additional-row-details-by-expanding-a-cell-in-Flutter-DataTable). |
0 commit comments