Created
June 19, 2022 09:13
-
-
Save TasnuvaOshin/67c6d902dbe6eaa5720bdac7848d0bc2 to your computer and use it in GitHub Desktop.
Easy Infinite Scroll in ListView Flutter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import '../../../../utils/app_asset.dart'; | |
import '../../../../utils/app_style.dart'; | |
class Bill extends StatefulWidget { | |
const Bill({Key? key}) : super(key: key); | |
@override | |
State<Bill> createState() => _BillState(); | |
} | |
class _BillState extends State<Bill> { | |
late bool _isLoading; | |
@override | |
void initState() { | |
_isLoading = false; | |
super.initState(); | |
} | |
List<int> _list = [ | |
1, | |
2, | |
3, | |
4, | |
5, | |
6, | |
7, | |
8, | |
9, | |
10, | |
]; | |
@override | |
Widget build(BuildContext context) { | |
return NotificationListener<ScrollNotification>( | |
onNotification: (ScrollNotification notification) { | |
if (notification.metrics.pixels == | |
notification.metrics.maxScrollExtent) { | |
print('Reached Bottom'); | |
setState(() => _isLoading = true); | |
_getData(); | |
} | |
return true; | |
}, | |
child: ListView.builder( | |
itemCount: _list.length + 1, | |
shrinkWrap: true, | |
itemBuilder: (BuildContext context, index) { | |
return index < _list.length | |
? Card( | |
child: Padding( | |
padding: const EdgeInsets.all(28.0), | |
child: Text( | |
"We are Checking Infinite Scrolling " + | |
_list[index].toString() + | |
"_________________________________________________________________________________________ INDEX : \n" + | |
index.toString(), | |
style: TextStyle( | |
fontSize: 15, | |
), | |
), | |
)) | |
: Center( | |
child: Opacity( | |
opacity: _isLoading ? 1.0 : 0.0, | |
child: const SizedBox( | |
width: 32.0, | |
height: 32.0, | |
child: CircularProgressIndicator()), | |
)); | |
})); | |
} | |
void _getData() { | |
_list.addAll([ | |
01, | |
02, | |
03, | |
04, | |
]); | |
Future.delayed(const Duration(seconds: 2), () { | |
setState(() => _isLoading = false); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment