I'm trying to control the scrolling of an SfDataGrid using a NestedScrollView, but it's not working.

I want to control the scrolling of SfDataGrid using NestedScrollView, but it doesn't work. Here's my code. Can you tell me how to make it work?

I might need to add another TabBarView, but it doesn't seem to work either.

import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// TRY THIS: Try running your application with "flutter run". You'll see
// the application has a purple toolbar. Then, without quitting the app,
// try changing the seedColor in the colorScheme below to Colors.green
// and then invoke "hot reload" (save your changes or press the "hot
// reload" button in a Flutter-supported IDE, or press "r" if you used
// the command line to start the app).
//
// Notice that the counter didn't reset back to zero; the application
// state is not lost during the reload. To reset the state, use hot
// restart instead.
//
// This works for code too, not just values: Most code changes can be
// tested with just a hot reload.
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});

// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.

// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".

final String title;

@override
State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
int _counter = 0;

late TabController controller;

void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}

@override
void initState() {
super.initState();
controller = TabController(length: 3, vsync: this);
}

@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// TRY THIS: Try changing the color here to a specific color (to
// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
// change color while the other colors stay the same.
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverToBoxAdapter(
child: Container(
height: 60,
child: Text('bar'),
),
),
SliverPersistentHeader(
pinned: true,
delegate: _SliverPersistentHeaderDelegate(
height: 60,
child: SizedBox(
width: 100,
child: TabBar(
controller: controller,
isScrollable: true,
tabs: [
Tab(
child: Text('1'),
),
Tab(
child: Text('2'),
),
Tab(
child: Text('3'),
),
],
),
),
),
),
];
},
body: _buildTokenList(),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}

_buildTokenList() {
return SfDataGrid(
columns: [
GridColumn(
columnName: 'name',
label: Container(child: Text('Name')),
),
GridColumn(
columnName: 'price',
label: Container(child: Text('Price')),
),
GridColumn(
columnName: 'marketCap',
label: Container(child: Text('Market Cap')),
),
],
source: DataSource(
data: List.generate(
1000,
(index) => {
'name': 'John',
'price': '100',
'marketCap': '1000',
}),
),
);
}
}

class _SliverPersistentHeaderDelegate extends SliverPersistentHeaderDelegate {
final double height;
final Widget child;
_SliverPersistentHeaderDelegate({
required this.height,
required this.child,
});
@override
Widget build(
BuildContext context, double shrinkOffset, bool overlapsContent) {
return SizedBox.expand(child: child);
}

@override
double get maxExtent => height;

@override
double get minExtent => height;

@override
bool shouldRebuild(covariant _SliverPersistentHeaderDelegate oldDelegate) {
return height != oldDelegate.height || child != oldDelegate.child;
}
}

class DataSource extends DataGridSource {
DataSource({required List<Map<String, dynamic>> data}) {
_data = data;
}

List<Map<String, dynamic>> _data = [];

@override
List<DataGridRow> get rows => _data
.map((e) => DataGridRow(cells: [
DataGridCell(columnName: 'name', value: e['name']),
DataGridCell(columnName: 'price', value: e['price']),
DataGridCell(columnName: 'marketCap', value: e['marketCap']),
]))
.toList();

@override
DataGridRowAdapter buildRow(DataGridRow row) {
return DataGridRowAdapter(
cells: row.getCells().map((cell) => Text(cell.value.toString())).toList(),
);
}
}






3 Replies

AP Abinesh Palanisamy Syncfusion Team March 6, 2025 07:05 AM UTC

Hi up float,

We noticed that you had created a ticket under your account in our ticketing system, we kindly request you to follow up on the issue on the respective ticket. Feel free to open a new forum thread for any new issue that may arise. 

 

Regards,

Abinesh P



TS Terrance Sellers April 10, 2025 04:31 AM UTC

To control SfDataGrid scrolling inside a NestedScrollView, set shrinkWrapRows: true and physics: NeverScrollableScrollPhysics() on the DataGrid. This lets the NestedScrollView handle all scrolling.




AP Abinesh Palanisamy Syncfusion Team April 10, 2025 06:11 AM UTC

Hi Terrance,

Similar to your response, we have also created a knowledge base on our side for inheriting scrolling from a Nested Scroll View to Flutter DataTable. Additionally, we have created knowledge base articles for similar custom view approaches on our end. You can customize the provided code to suit your specific requirements.

KB links:
 


Regards,

Abinesh P


Loader.
Up arrow icon