Skip to content

This repository contains the examples about how to dynamically show and hide the columns on runtime in Flutter DataTable (SfDataGrid).

Notifications You must be signed in to change notification settings

SyncfusionExamples/How-to-dynamically-show-and-hide-columns-on-runtime-in-Flutter-DataTable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to dynamically show and hide columns on runtime with button click in Flutter DataTable?

In this article, we will show you how to dynamically show and hide columns on runtime with button click in Flutter DataTable.

Initialize the SfDataGrid widget with all the required properties. You can hide and show columns using the GridColumn.visible property. Set this property to false to hide the desired columns. By default, the visible property is set to true. Inside the onChanged callback of the switch 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.

  // To change the column's visibility.
  bool isVisible = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Syncfusion Flutter DataGrid'),
      ),
      body: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Text('ID column'),
              Switch(
                  value: !isVisible,
                  onChanged: (value) {
                    // Update the isVisible to change the column's visibility.
                    // When toggle the switch button.
                    setState(() {
                      isVisible = !value;
                    });
                  }),
            ],
          ),
          const Padding(padding: EdgeInsets.all(5)),
          Expanded(
            child: SfDataGrid(
              source: employeeDataSource,
              columnWidthMode: ColumnWidthMode.fill,
              columns: <GridColumn>[
                GridColumn(
                    visible: isVisible,
                    columnName: 'id',
                    label: Container(
                        padding: const EdgeInsets.all(16.0),
                        alignment: Alignment.center,
                        child: const Text(
                          'ID',
                        ))),
                GridColumn(
                    columnName: 'name',
                    label: Container(
                        padding: const EdgeInsets.all(8.0),
                        alignment: Alignment.center,
                        child: const Text('Name'))),
                GridColumn(
                    columnName: 'designation',
                    label: Container(
                        padding: const EdgeInsets.all(8.0),
                        alignment: Alignment.center,
                        child: const Text(
                          'Designation',
                          overflow: TextOverflow.ellipsis,
                        ))),
                GridColumn(
                    columnName: 'salary',
                    label: Container(
                        padding: const EdgeInsets.all(8.0),
                        alignment: Alignment.center,
                        child: const Text('Salary'))),
              ],
            ),
          ),
        ],
      ),
    );
  }

You can download this example on GitHub.

About

This repository contains the examples about how to dynamically show and hide the columns on runtime in Flutter DataTable (SfDataGrid).

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •