Skip to content

Instantly share code, notes, and snippets.

@IkhwanSI13
Last active October 4, 2024 13:05
Show Gist options
  • Save IkhwanSI13/eb09a588096db4c88f96ad9f2faf91d1 to your computer and use it in GitHub Desktop.
Save IkhwanSI13/eb09a588096db4c88f96ad9f2faf91d1 to your computer and use it in GitHub Desktop.
load more data in NestedScrollView with NotificationListener
import 'package:cartenz_djp/style/colors.dart';
import 'package:cartenz_djp/widget/override/customTabs.dart' as Tabs;
import 'package:flutter/material.dart';
class ExampleWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() => ExampleState();
}
class ExampleState extends State<ExampleWidget> {
onLoadMore() {
//Load More, do something
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: ColorWhite,
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
child: Builder(builder: (BuildContext context) {
return NestedScrollView(
//controller: scrollController,
headerSliverBuilder:
(BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverAppBar(
bottom: getTabHeader(),
)
];
}, body: Builder(builder: (BuildContext context) {
return Stack(children: <Widget>[
getTabBody(),
]);
}));
}),
)
],
),
);
}
Widget getTabHeader() {
return Tabs.TabBar(tabs: [
Tabs.Tab(child: Text("Tab 1")),
Tabs.Tab(child: Text("Tab 2")),
Tabs.Tab(child: Text("Tab 3")),
]);
}
Widget getTabBody() {
return Column(
children: <Widget>[
Expanded(
child: Tabs.TabBarView(children: [
Container(
child:
//Solved with this
NotificationListener<ScrollNotification>(
child: MediaQuery.removePadding(
context: context,
removeTop: true,
child: ListView.builder(
shrinkWrap: true,
itemCount: 3,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (BuildContext context, int index) {
return Container();
})),
onNotification: (ScrollNotification scrollInfo) {
if (scrollInfo.metrics.pixels == scrollInfo.metrics.maxScrollExtent) {
//load more here
onLoadMore();
}
return false;
},
);
Container(child: Container()),
Container(child: Container())
]))
],
);
}
}
@fahadadnaan
Copy link

@IkhwanSI13 Thanks for replying, Notifications Listener not working with multi ListView like my case, So i resolved this problem by add listener function instead of Notifications Listener.

@prateek601
Copy link

Thanks for the code.It is working well 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment