|
1 | | -# how-to-maintain-the-state-of-a-DataGrid-when-switching-between-tabs-in-Flutter-DataTable |
2 | | -How to maintain the state of a DataGrid when switching between tabs in Flutter DataTable (SfDataGrid)? |
| 1 | +# How to maintain the state of a DataGrid when switching between tabs in Flutter DataTable (SfDataGrid) |
| 2 | + |
| 3 | +When switching between tabs, all the widgets undergo re-creation, causing the [Flutter DataGrid](https://www.syncfusion.com/flutter-widgets/flutter-datagrid) to lose its previous state, including sorting, filtering, and scroll position settings. |
| 4 | + |
| 5 | +## STEP 1: |
| 6 | +Initialize the [SfDataGrid](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid-class.html) widget with all the necessary properties. In order to maintain the state of the [Flutter DataGrid](https://www.syncfusion.com/flutter-widgets/flutter-datagrid), it is important to use the [AutomaticKeepAliveClientMixin](https://api.flutter.dev/flutter/widgets/AutomaticKeepAliveClientMixin-mixin.html). By using this mixin, the Flutter DataGrid can be preserved in its current state. Override the method [wantKeepAlive](https://api.flutter.dev/flutter/widgets/AutomaticKeepAliveClientMixin/wantKeepAlive.html) to return true, ensuring that the state is retained. |
| 7 | + |
| 8 | +```dart |
| 9 | +class DataGrid extends StatefulWidget { |
| 10 | + const DataGrid({ |
| 11 | + super.key, |
| 12 | + }); |
| 13 | +
|
| 14 | + @override |
| 15 | + State<DataGrid> createState() => DataGridState(); |
| 16 | +} |
| 17 | +
|
| 18 | +class DataGridState extends State<DataGrid> |
| 19 | + with AutomaticKeepAliveClientMixin<DataGrid> { |
| 20 | + List<Employee> employees = <Employee>[]; |
| 21 | + late EmployeeDataSource employeeDataSource; |
| 22 | +
|
| 23 | +
|
| 24 | + @override |
| 25 | + void initState() { |
| 26 | + super.initState(); |
| 27 | + employees = getEmployeeData(); |
| 28 | + employeeDataSource = EmployeeDataSource(employeeData: employees); |
| 29 | + } |
| 30 | +
|
| 31 | + @override |
| 32 | + bool get wantKeepAlive => true; |
| 33 | +
|
| 34 | + @override |
| 35 | + Widget build(BuildContext context) { |
| 36 | + super.build(context); |
| 37 | + return SfDataGrid( |
| 38 | + source: employeeDataSource, |
| 39 | + allowSorting: true, |
| 40 | + allowFiltering: true, |
| 41 | + columnWidthMode: ColumnWidthMode.fill, |
| 42 | + columns: getColumns, |
| 43 | + ); |
| 44 | + } |
| 45 | +} |
| 46 | +
|
| 47 | +``` |
| 48 | +## STEP 2: |
| 49 | +Wrap your DataGrid widget in a [TabBarView](https://api.flutter.dev/flutter/material/TabBarView-class.html). |
| 50 | + |
| 51 | +```dart |
| 52 | +import 'package:flutter/material.dart'; |
| 53 | +import 'package:syncfusion_flutter_datagrid/datagrid.dart'; |
| 54 | +
|
| 55 | +class MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin { |
| 56 | + late TabController _tabController; |
| 57 | +
|
| 58 | + @override |
| 59 | + void initState() { |
| 60 | + super.initState(); |
| 61 | + _tabController = TabController(length: 2, vsync: this); |
| 62 | + } |
| 63 | +
|
| 64 | + @override |
| 65 | + void dispose() { |
| 66 | + _tabController.dispose(); |
| 67 | + super.dispose(); |
| 68 | + } |
| 69 | +
|
| 70 | + @override |
| 71 | + Widget build(BuildContext context) { |
| 72 | + return Scaffold( |
| 73 | + appBar: AppBar( |
| 74 | + title: const Text('Syncfusion Flutter DataGrid'), |
| 75 | + bottom: TabBar( |
| 76 | + controller: _tabController, |
| 77 | + tabs: const [ |
| 78 | + Tab(text: 'Employees'), |
| 79 | + Tab(text: 'Empty View'), |
| 80 | + ], |
| 81 | + ), |
| 82 | + ), |
| 83 | + body: TabBarView(controller: _tabController, children: const [ |
| 84 | + DataGrid(), |
| 85 | + Center( |
| 86 | + child: Text('Empty View'), |
| 87 | + ), |
| 88 | + ]), |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
| 92 | +
|
| 93 | +``` |
0 commit comments