ListView with items that can be scrolled infinitely in both directions. The Point is you can customize listItem 、empty and loading widget.
There are four options for constructing a ListView:
-
The required custom listener event listview.onrequest, triggered when the super_infinite_listview widget is initialized or when the List slides to the bottom, can then request the server data and return the data as a List.
-
The required listview.itembuilder is generated by customizing the list, which provides three parameters' item ', 'index' and 'list', respectively corresponding to the current list item, index and all list array. You can return custom widget to generate lists.
-
Custom template emptyWidget, if the list is empty can show custom widget, otherwise will show 'no more'.
-
Custom template loadingWidget, you can customize the loading widget when requesting a list of services.
Add the following dependencies in your pubspec.yaml
file:
dependencies:
super_infinite_listview: ^${latestVersion}
Import package without init.
import 'package:super_infinit_listview/super_infinit_listview.dart';
import 'package:flutter/material.dart';
import 'package:super_infinit_listview/super_infinit_listview.dart';
//For test
import 'dart:math';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Super infinite list view Demo',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
num total = 100;//Show up to 100
String generateRandomString () {
String alphabet = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';
int strlenght = 30; /// 生成的字符串固定长度
String left = '';
for (var i = 0; i < strlenght; i++) {
left = left + alphabet[Random().nextInt(alphabet.length)];
}
return left;
}
//onRequest
Map getList () {
List list = [];
for (var i = 0; i < 10; i++) {
list.add(generateRandomString());
}
return { "list": list, "totalNumber": total };
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Super infinite list view Demo'),
),
body: Center(
child: SuperInfiniteListView(
itemBuilder: (item, index, list) {
return ListTile(title: Text("$index"));
},
onRequest: getList,
),
)
);
}
}