Flutter : Lazy Loading preloaded items on scrolling pagination ListView - flutter

We have implemented this functionality of preloading in which when a user is on a photo album the pictures load as per the pages, the first-page load certain picture items once a user has scrolled down all the way through the first page then after the other pictures of another page loads, now we want pictures to load constantly as the user scrolls down the page and not as per the particular pages with fixed picture items. Would really appreciate the suggested approach. Implemented code is down below, please suggest the manipulation of it to achieve the requirement.
scrollController.addListener(() {
if (isloading) {
return;
}
double maxScroll = scrollController.position.maxScrollExtent;
double currentScroll = scrollController.position.pixels;
if (currentScroll == maxScroll && hasMore!) {
fetchPhotos();
// hasMoreAndMaxScroll = true;
update();
// print("call function update");
// print("call function");
} else {
// scrollController.jumpTo(0);
// print("call function");
}
});
callAnalysis(Appstring.resourcestring[Appstring.albumphotolist].toString());
super.onInit();
}

Related

Flutter- App content disappear when NotificationListener sends notification for first time

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

Image Picker Flutter and Progress Indicator

I have implmented image picker, it is working great on both ios and android.
However, after i have captured an image, it is taking quite long for it to appear on my Image.file(image) widget.
I cannot decrease the quality of image, as it matters in the app.
So i need something like CachedNetworkImage but to load from file, not from internet, any ideas?
I just want to show an progress bar or indicator, just to let the user know that there is a process.
File? image;
Future pickImage(ImageSource source, BuildContext context) async {
try {
final image = await ImagePicker().pickImage(source: source);
if (image == null) return;
var imageTemporary = File(image.path);
if (source == ImageSource.camera) {
final img.Image? capturedImage =
img.decodeImage(await File(image.path).readAsBytes());
final img.Image orientedImage = img.bakeOrientation(capturedImage!);
imageTemporary =
await File(image.path).writeAsBytes(img.encodeJpg(orientedImage));
}
setState(() {
this.image = imageTemporary;
});
} on PlatformException catch (e) {print(e.toString());}
}
Thats my function to call image picker.
And passing it to ElevatedButton like:

How to add transition delay to a GIF?

I'm using import 'package:image/image.dart' as imgs; this package
I can perfectly create a GIF but i cannot set the delay transition for every frame, Here's my code:
List<int>? generateGIF(Iterable<imgs.Image> images) {
final imgs.Animation animation = imgs.Animation();
for (imgs.Image image in images) {
animation.addFrame(image);
}
return imgs.encodeGifAnimation(animation);
}
My question is, how to put a delay for each frame?.
for example, 1000 milliseconds to each frame transition.
try this,
List<int>? generateGIF(Iterable<imgs.Image> images) {
final imgs.Animation animation = imgs.Animation();
for (imgs.Image image in images) {
image.duration = 1;
animation.addFrame(image);
}
return imgs.encodeGifAnimation(animation);
}

how to detect middle or three quarters of scroll position on ListView on Flutter to lazy loading data?

how to detect middle or three-quarters of scroll position on ListView? I want to get data after reach to middle or three-quarters of the scroll position. How to do it?
Currently, I get data on the end of listView.
_scrollController.addListener(scrollListener);
void scrollListener(){
if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent) {
getMoreData();
}
}
I think this would work..
_scrollController.addListener(scrollListener);
void scrollListener(){
if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent * 0.75) {
getMoreData();
}
}

Flutter firebase_admob banner is still shown on every screen

I'm using firebase_admob package in a flutter project, and when I load and show a banner ad on the first screen it's loaded and displayed correctly, but when I navigate to a new screen that doesn't contain a banner, the old banner is still shown on the new screen,
this is the creation code of the banner called in the initState()
BannerAd createBannerAd() {
return BannerAd(
adUnitId: BannerAd.testAdUnitId,
//Change BannerAd adUnitId with Admob ID
size: AdSize.smartBanner,
targetingInfo: AdsUtil.targetingInfo,
listener: (MobileAdEvent event) {
if (event == MobileAdEvent.loaded) {
// dispose after you received the loaded event seems okay.
if (mounted) {
_bannerAd..show();
setState(() {
isBannerShown = true;
});
} else {
_bannerAd = null;
setState(() {
isBannerShown = false;
});
}
} else if (event == MobileAdEvent.failedToLoad) {
_bannerAd = null;
setState(() {
isBannerShown = false;
});
}
// print("********** ********** BannerAd $event");
});
}
#override
void dispose() {
_bannerAd.dispose();
super.dispose();
}
Navigation to the second screen code :
IconButton(
icon: Icon(Icons.settings),
onPressed: () {
Navigator.of(context).pushNamed(SettingsPage.routeName);
},
),
I want to find a solution that when I navigate to a new screen the old banner get destroyed and not displayed in the new screen I navigated to.
Thanks
When you push a new page then dispose method is not called the native adView remains on the next screen as an overlay widget because it's not part of the widget tree. You need to keep track of pages and then load the banner and dispose it as per page change and reload the ad when the page reloads.
So instead of pushing the page, replacing it with the new page will fix the issue.
You can create RouteObserver and keep track of the page change. Check this answer.