|
1 | | -# How-to-add-multiple-widgets-in-Flutter-DataTable |
2 | | -This demo shows how to add multiple widgets in Flutter DataTable |
| 1 | +# How to add multiple widgets in Flutter DataTable (SfDataGrid)?. |
| 2 | + |
| 3 | +In this article, we will show how to add multiple widgets in [Flutter DataTable](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 the necessary properties. To display multiple images or widgets in a cell, use the [buildRow](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource/buildRow.html) method within the [DataGridSource](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource-class.html). This method allows you to customize the widget displayed in each cell. Inside buildRow, you can add conditional logic for each column or cell to load the appropriate widgets or images. |
| 6 | + |
| 7 | +```dart |
| 8 | +@override |
| 9 | + DataGridRowAdapter buildRow(DataGridRow row) { |
| 10 | + return DataGridRowAdapter( |
| 11 | + cells: |
| 12 | + row.getCells().map<Widget>((cell) { |
| 13 | + switch (cell.columnName) { |
| 14 | + case 'id': |
| 15 | + return Container( |
| 16 | + padding: EdgeInsets.all(8.0), |
| 17 | + alignment: Alignment.center, |
| 18 | + child: Row( |
| 19 | + mainAxisAlignment: MainAxisAlignment.center, |
| 20 | + children: [ |
| 21 | + Icon(Icons.badge, color: Colors.blue), |
| 22 | + SizedBox(width: 4), |
| 23 | + Text(cell.value.toString()), |
| 24 | + ], |
| 25 | + ), |
| 26 | + ); |
| 27 | +
|
| 28 | + case 'name': |
| 29 | + return Container( |
| 30 | + padding: EdgeInsets.all(8.0), |
| 31 | + alignment: Alignment.center, |
| 32 | + child: Row( |
| 33 | + mainAxisAlignment: MainAxisAlignment.center, |
| 34 | + children: [ |
| 35 | + CircleAvatar( |
| 36 | + radius: 12, |
| 37 | + backgroundImage: AssetImage( |
| 38 | + 'images/GoldenState.png', |
| 39 | + ), |
| 40 | + ), |
| 41 | + SizedBox(width: 6), |
| 42 | + Text(cell.value.toString()), |
| 43 | + ], |
| 44 | + ), |
| 45 | + ); |
| 46 | +
|
| 47 | + case 'designation': |
| 48 | + return Container( |
| 49 | + padding: EdgeInsets.all(4), |
| 50 | + alignment: Alignment.center, |
| 51 | + child: Row( |
| 52 | + mainAxisAlignment: MainAxisAlignment.center, |
| 53 | + children: [ |
| 54 | + Image.asset( |
| 55 | + 'images/DenverNuggets.png', |
| 56 | + width: 20, |
| 57 | + height: 20, |
| 58 | + ), |
| 59 | + SizedBox(width: 4), |
| 60 | + Image.asset('images/Memphis.png', width: 20, height: 20), |
| 61 | + SizedBox(width: 4), |
| 62 | + Image.asset('images/Hornets.png', width: 20, height: 20), |
| 63 | + ], |
| 64 | + ), |
| 65 | + ); |
| 66 | +
|
| 67 | + case 'salary': |
| 68 | + return Container( |
| 69 | + padding: EdgeInsets.all(8.0), |
| 70 | + alignment: Alignment.center, |
| 71 | + child: Row( |
| 72 | + mainAxisAlignment: MainAxisAlignment.center, |
| 73 | + children: [ |
| 74 | + Icon(Icons.monetization_on, color: Colors.green), |
| 75 | + Text(cell.value.toString()), |
| 76 | + ], |
| 77 | + ), |
| 78 | + ); |
| 79 | +
|
| 80 | + default: |
| 81 | + return Container( |
| 82 | + padding: EdgeInsets.all(8.0), |
| 83 | + alignment: Alignment.center, |
| 84 | + child: Text(cell.value.toString()), |
| 85 | + ); |
| 86 | + } |
| 87 | + }).toList(), |
| 88 | + ); |
| 89 | + } |
| 90 | +``` |
| 91 | + |
| 92 | +You can download this example on [GitHub](https://github.com/SyncfusionExamples/How-to-add-multiple-widgets-in-Flutter-DataTable). |
0 commit comments