Skip to content

Commit 2b7c69c

Browse files
Merge pull request #1 from abineshPalanisamy/master
How to maintain the state of a DataGrid when switching between tabs in Flutter DataTable (SfDataGrid)
2 parents 938e5a4 + 94664af commit 2b7c69c

File tree

125 files changed

+4949
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+4949
-2
lines changed

.gitignore

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
migrate_working_dir/
12+
13+
# IntelliJ related
14+
*.iml
15+
*.ipr
16+
*.iws
17+
.idea/
18+
19+
# The .vscode folder contains launch configuration and tasks you configure in
20+
# VS Code which you may wish to be included in version control, so this line
21+
# is commented out by default.
22+
#.vscode/
23+
24+
# Flutter/Dart/Pub related
25+
**/doc/api/
26+
**/ios/Flutter/.last_build_id
27+
.dart_tool/
28+
.flutter-plugins
29+
.flutter-plugins-dependencies
30+
.packages
31+
.pub-cache/
32+
.pub/
33+
/build/
34+
35+
# Symbolication related
36+
app.*.symbols
37+
38+
# Obfuscation related
39+
app.*.map.json
40+
41+
# Android Studio will place build artifacts here
42+
/android/app/debug
43+
/android/app/profile
44+
/android/app/release

README.md

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,93 @@
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+
```

android/.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
gradle-wrapper.jar
2+
/.gradle
3+
/captures/
4+
/gradlew
5+
/gradlew.bat
6+
/local.properties
7+
GeneratedPluginRegistrant.java
8+
9+
# Remember to never publicly share your keystore.
10+
# See https://flutter.dev/docs/deployment/android#reference-the-keystore-from-the-app
11+
key.properties
12+
**/*.keystore
13+
**/*.jks

android/app/build.gradle

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
def localProperties = new Properties()
2+
def localPropertiesFile = rootProject.file('local.properties')
3+
if (localPropertiesFile.exists()) {
4+
localPropertiesFile.withReader('UTF-8') { reader ->
5+
localProperties.load(reader)
6+
}
7+
}
8+
9+
def flutterRoot = localProperties.getProperty('flutter.sdk')
10+
if (flutterRoot == null) {
11+
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
12+
}
13+
14+
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
15+
if (flutterVersionCode == null) {
16+
flutterVersionCode = '1'
17+
}
18+
19+
def flutterVersionName = localProperties.getProperty('flutter.versionName')
20+
if (flutterVersionName == null) {
21+
flutterVersionName = '1.0'
22+
}
23+
24+
apply plugin: 'com.android.application'
25+
apply plugin: 'kotlin-android'
26+
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
27+
28+
android {
29+
namespace "com.example.flutter_application"
30+
compileSdkVersion flutter.compileSdkVersion
31+
ndkVersion flutter.ndkVersion
32+
33+
compileOptions {
34+
sourceCompatibility JavaVersion.VERSION_1_8
35+
targetCompatibility JavaVersion.VERSION_1_8
36+
}
37+
38+
kotlinOptions {
39+
jvmTarget = '1.8'
40+
}
41+
42+
sourceSets {
43+
main.java.srcDirs += 'src/main/kotlin'
44+
}
45+
46+
defaultConfig {
47+
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
48+
applicationId "com.example.flutter_application"
49+
// You can update the following values to match your application needs.
50+
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
51+
minSdkVersion flutter.minSdkVersion
52+
targetSdkVersion flutter.targetSdkVersion
53+
versionCode flutterVersionCode.toInteger()
54+
versionName flutterVersionName
55+
}
56+
57+
buildTypes {
58+
release {
59+
// TODO: Add your own signing config for the release build.
60+
// Signing with the debug keys for now, so `flutter run --release` works.
61+
signingConfig signingConfigs.debug
62+
}
63+
}
64+
}
65+
66+
flutter {
67+
source '../..'
68+
}
69+
70+
dependencies {
71+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
72+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
2+
<!-- The INTERNET permission is required for development. Specifically,
3+
the Flutter tool needs it to communicate with the running application
4+
to allow setting breakpoints, to provide hot reload, etc.
5+
-->
6+
<uses-permission android:name="android.permission.INTERNET"/>
7+
</manifest>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
2+
<application
3+
android:label="flutter_application"
4+
android:name="${applicationName}"
5+
android:icon="@mipmap/ic_launcher">
6+
<activity
7+
android:name=".MainActivity"
8+
android:exported="true"
9+
android:launchMode="singleTop"
10+
android:theme="@style/LaunchTheme"
11+
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
12+
android:hardwareAccelerated="true"
13+
android:windowSoftInputMode="adjustResize">
14+
<!-- Specifies an Android theme to apply to this Activity as soon as
15+
the Android process has started. This theme is visible to the user
16+
while the Flutter UI initializes. After that, this theme continues
17+
to determine the Window background behind the Flutter UI. -->
18+
<meta-data
19+
android:name="io.flutter.embedding.android.NormalTheme"
20+
android:resource="@style/NormalTheme"
21+
/>
22+
<intent-filter>
23+
<action android:name="android.intent.action.MAIN"/>
24+
<category android:name="android.intent.category.LAUNCHER"/>
25+
</intent-filter>
26+
</activity>
27+
<!-- Don't delete the meta-data below.
28+
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
29+
<meta-data
30+
android:name="flutterEmbedding"
31+
android:value="2" />
32+
</application>
33+
</manifest>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.example.flutter_application
2+
3+
import io.flutter.embedding.android.FlutterActivity
4+
5+
class MainActivity: FlutterActivity() {
6+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Modify this file to customize your launch splash screen -->
3+
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
4+
<item android:drawable="?android:colorBackground" />
5+
6+
<!-- You can insert your own image assets here -->
7+
<!-- <item>
8+
<bitmap
9+
android:gravity="center"
10+
android:src="@mipmap/launch_image" />
11+
</item> -->
12+
</layer-list>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Modify this file to customize your launch splash screen -->
3+
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
4+
<item android:drawable="@android:color/white" />
5+
6+
<!-- You can insert your own image assets here -->
7+
<!-- <item>
8+
<bitmap
9+
android:gravity="center"
10+
android:src="@mipmap/launch_image" />
11+
</item> -->
12+
</layer-list>
544 Bytes
Loading

0 commit comments

Comments
 (0)