i want to add lazy loading for my app but when i put the new controller. the controller didn't work with the NestedScrollView that i've made.
my new controller:
final controller = ScrollController();
in my initstate:
controller.addListener(() {
if (controller.position.maxScrollExtent == controller.offset) {
getStoryUser();
}
and i put the controller into list view but its didn't work with the NestedScrollView that i've mentioned earlier. any solution for this matter? any help will be valuable thank you.
Attach this _scrollController with your scrollview
if ((_scrollController.position.pixels >
_scrollController.position.maxScrollExtent - 20)) {
if (_isLoading || _loadMore || ((_media.media?.length ?? 0) < 30)) {
return;
}
_pageNumber++;
_loadMore = true;
if (mounted) setState(() {});
await _loadData();
_loadMore = false;
if (mounted) setState(() {});
}
Related
I made an automatic scrolling bar in flutter, the way I made it scroll automatically by using a combination of a timer and scroll controller, and then I linked the scroll controller to a regular ListView.builder, blew is the implementation of my way to do it
#override
void initState() {
super.initState();
int _length = widget.items.length;
currentItems = widget.items;
// scroll smothly to the end of the list then reverse the scroll
bool isAscending = true;
_timer = Timer.periodic(const Duration(milliseconds: 3000), (timer) {
if (isAscending) {
_currentIndex++;
if (_currentIndex == currentItems.length) {
isAscending = !isAscending;
}
}
if (!isAscending) {
_currentIndex--;
if (_currentIndex == 0) {
isAscending = !isAscending;
}
}
_scrollController.animateTo(_currentIndex * 304.0,
duration: const Duration(milliseconds: 3000), curve: Curves.linear);
});
}
my problem here is when I navigate to another screen or press the phone's home button and stay away from this widget for a while and then come back, I'd find it scrolling so fast to catch the currentIndex ( which is far away from where I left it ) in a period of time that I specified for the .animateTo method, I'm guessing the solution is to somehow pause the timer when I'm not in the same route, but I didn't find any reference of how to implement that
I want to check the internet on every page in my flutter app how I can do that without repeating the same code on every screen?
here is my code
bool internet = true;
#override
void initState() {
countryController.text = "+961";
Connectivity().onConnectivityChanged.listen((connectionState) {
if (connectionState == ConnectivityResult.none) {
setState(() {
internet = false;
});
// valueNotifierBook.incrementNotifier();
} else {
setState(() {
internet = true;
});
}
});
super.initState();
}
I am creating an app where i am using NotificationListener<UserScrollNotification> to change the boolean value on the basis of scroll direction... but when i scroll page for very first time all the content gets disappear for 2-3 seconds...I am using Provider.... Please help me...
Here i am trying to change boolean variable
NotificationListener<UserScrollNotification>(
onNotification: (notification) {
final ScrollDirection direction =
notification.direction;
if (direction == ScrollDirection.reverse) {
value.changeVisiblity(true);
} else if (direction ==
ScrollDirection.forward) {
value.changeVisiblity(false);
}
return false;
},
here is my provider class where boolean is defined
bool isVisible = false;
changeVisiblity(bool value) {
isVisible = value;
notifyListeners();
}
here is a link to 14 seconds short video
enter link description here
I am using NotificationListener<UserScrollNotification> to change the boolean value on the basis of scroll direction... but when i scroll page for very first time when app starts all the content gets disappear for some seconds...I am using Provider,Please help me... It only happens for the very first time
Here i am trying to change boolean variable
NotificationListener<UserScrollNotification>(
onNotification: (notification) {
final ScrollDirection direction =
notification.direction;
if (direction == ScrollDirection.reverse) {
value.changeVisiblity(true);
} else if (direction ==
ScrollDirection.forward) {
value.changeVisiblity(false);
}
return false;
},
here is my provider class where boolean is defined
bool isVisible = false;
changeVisiblity(bool value) {
isVisible = value;
notifyListeners();
}
here is a video
Video
Hello I'm trying to fetch data when screen is scrolled to bottom but the new data that needs to be fetched is not loaded. So my screen looks like this:
I fetch data in initState:
bool isNewDocumentsLoading = false;
bool hasMore = true;
int page = 1;
final controller = ScrollController();
void initState() {
super.initState();
//Should fetch data when scrolled to bottom
controller.addListener(() {
if (controller.position.maxScrollExtent == controller.offset) {
fetch();
}
});
//Initial fetch when app is runned
Future.delayed(Duration.zero, () async { fetch(); }
and here is my fetch method:
if (isNewDocumentsLoading) return;
isNewDocumentsLoading = true;
const limit = 4;
Future.delayed(Duration.zero, () async {
var dokumenti = await _dokumentiService!.getSharedDocuments(limit, page);
setState(() {
page++;
isNewDocumentsLoading = false;
_dokumenti.addAll(dokumenti);
//_dokumenti.addAll(_dokumentiRefresh);
if (dokumenti.length < limit) {
hasMore = false;
}
isLoading = false;
});
});
}
I really don't know what am I doing wrong here and what is the problem
The problem was that my list is inside SingleChildScrollView so i needed to put controller inside SingleChildScrollView and not the ListView, so the functions were working just fine.
return SingleChildScrollView(
controller: controller,...
//controller was in ListView
ListView.builder(
//controller: controller,
scrollDirection: Axis.vertical,
shrinkWrap: true,
physics: const ScrollPhysics(),
itemCount: params.length + 1, ...