|
1 | | -# How-to-dynamically-show-and-hide-columns-on-runtime-in-Flutter-DataTable- |
| 1 | +## How to dynamically show and hide columns on runtime with button click in Flutter DataTable? |
| 2 | + |
| 3 | +In this article, we will show you how to dynamically show and hide columns on runtime with button click 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 all the required properties. You can hide and show columns using the [GridColumn.visible](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/GridColumn/visible.html) property. Set this property to false to hide the desired columns. By default, the visible property is set to true. Inside the [onChanged](https://api.flutter.dev/flutter/material/Switch/onChanged.html) callback of the [switch](https://api.flutter.dev/flutter/material/Switch-class.html) widget, update the visibility value within the setState function. This change causes the build method to run again, updating the visibility of the specified columns in the SfDataGrid. |
| 6 | + |
| 7 | +```dart |
| 8 | + // To change the column's visibility. |
| 9 | + bool isVisible = true; |
| 10 | +
|
| 11 | + @override |
| 12 | + Widget build(BuildContext context) { |
| 13 | + return Scaffold( |
| 14 | + appBar: AppBar( |
| 15 | + title: const Text('Syncfusion Flutter DataGrid'), |
| 16 | + ), |
| 17 | + body: Column( |
| 18 | + children: [ |
| 19 | + Row( |
| 20 | + mainAxisAlignment: MainAxisAlignment.center, |
| 21 | + children: [ |
| 22 | + const Text('ID column'), |
| 23 | + Switch( |
| 24 | + value: !isVisible, |
| 25 | + onChanged: (value) { |
| 26 | + // Update the isVisible to change the column's visibility. |
| 27 | + // When toggle the switch button. |
| 28 | + setState(() { |
| 29 | + isVisible = !value; |
| 30 | + }); |
| 31 | + }), |
| 32 | + ], |
| 33 | + ), |
| 34 | + const Padding(padding: EdgeInsets.all(5)), |
| 35 | + Expanded( |
| 36 | + child: SfDataGrid( |
| 37 | + source: employeeDataSource, |
| 38 | + columnWidthMode: ColumnWidthMode.fill, |
| 39 | + columns: <GridColumn>[ |
| 40 | + GridColumn( |
| 41 | + visible: isVisible, |
| 42 | + columnName: 'id', |
| 43 | + label: Container( |
| 44 | + padding: const EdgeInsets.all(16.0), |
| 45 | + alignment: Alignment.center, |
| 46 | + child: const Text( |
| 47 | + 'ID', |
| 48 | + ))), |
| 49 | + GridColumn( |
| 50 | + columnName: 'name', |
| 51 | + label: Container( |
| 52 | + padding: const EdgeInsets.all(8.0), |
| 53 | + alignment: Alignment.center, |
| 54 | + child: const Text('Name'))), |
| 55 | + GridColumn( |
| 56 | + columnName: 'designation', |
| 57 | + label: Container( |
| 58 | + padding: const EdgeInsets.all(8.0), |
| 59 | + alignment: Alignment.center, |
| 60 | + child: const Text( |
| 61 | + 'Designation', |
| 62 | + overflow: TextOverflow.ellipsis, |
| 63 | + ))), |
| 64 | + GridColumn( |
| 65 | + columnName: 'salary', |
| 66 | + label: Container( |
| 67 | + padding: const EdgeInsets.all(8.0), |
| 68 | + alignment: Alignment.center, |
| 69 | + child: const Text('Salary'))), |
| 70 | + ], |
| 71 | + ), |
| 72 | + ), |
| 73 | + ], |
| 74 | + ), |
| 75 | + ); |
| 76 | + } |
| 77 | +``` |
| 78 | + |
| 79 | +You can download this example on [GitHub](https://github.com/SyncfusionExamples/How-to-dynamically-show-and-hide-columns-on-runtime-in-Flutter-DataTable). |
0 commit comments