跟随this来检测何时到达列表末尾,但似乎ScrollListener无法正常工作,原因是我调试的打印功能也没有输出
使用该按钮可以成功扩展列表,但我的计划是摆脱该按钮并在到达底部时自动添加
class _AllProjectsState extends State<AllProjects> {
ScrollController sc;
void initstate() {
sc = ScrollController();
super.initState();
sc.addListener(scListener);
}
scListener() {
print("Changed");
if( sc.offset >= sc.position.maxScrollExtent && !sc.position.outOfRange) {
setState(() {
print("Reached");
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Project Info'),
),
backgroundColor: Colors.white,
body:FutureBuilder<Data>(
future: getData(),
builder: (context, snapshot) {
if(snapshot.hasData)
return ListView.builder(
controller: sc,
itemCount: snapshot.data.projects.length,
itemBuilder: (context,index) {
return ListTile(
title: Text('${snapshot.data.projects[index].attributes.name}',style: TextStyle(fontSize: 30),),
);
}
);
else
return Text('NotFound');
}
),
floatingActionButton: FloatingActionButton(
onPressed: () {
if(!full) {
setState(() {
page+=1;
});
}
},
child: Icon(Icons.add),
),
);
}
}
答案 0 :(得分:1)
您可以在下面复制粘贴运行完整代码
您有错字void initstate()
,需要@override
代码段
@override
void initState() {
工作演示
完整代码
import 'package:flutter/material.dart';
class Attributes {
String name;
Attributes({this.name});
}
class Project {
Attributes attributes;
Project({this.attributes});
}
class Data {
List<Project> projects;
Data({this.projects});
}
class AllProjects extends StatefulWidget {
@override
_AllProjectsState createState() => _AllProjectsState();
}
class _AllProjectsState extends State<AllProjects> {
ScrollController sc;
Project project = Project(attributes: Attributes(name: "test"));
List<Project> projectList = [];
@override
void initState() {
projectList.add(project);
projectList.add(project);
projectList.add(project);
projectList.add(project);
projectList.add(project);
projectList.add(project);
projectList.add(project);
projectList.add(project);
projectList.add(project);
projectList.add(project);
projectList.add(project);
projectList.add(project);
sc = ScrollController();
super.initState();
sc.addListener(scListener);
}
scListener() {
print("Changed");
if (sc.offset >= sc.position.maxScrollExtent && !sc.position.outOfRange) {
setState(() {
print("Reached");
});
}
}
Future<Data> getData() {
Project project = Project(attributes: Attributes(name: "123"));
projectList.add(project);
Data data = Data(projects: projectList);
return Future.value(data);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Project Info'),
),
backgroundColor: Colors.white,
body: FutureBuilder<Data>(
future: getData(),
builder: (context, snapshot) {
if (snapshot.hasData)
return ListView.builder(
controller: sc,
itemCount: snapshot.data.projects.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(
'${snapshot.data.projects[index].attributes.name}',
style: TextStyle(fontSize: 30),
),
);
});
else
return Text('NotFound');
}),
floatingActionButton: FloatingActionButton(
onPressed: () {
/*if (!full) {
setState(() {
page += 1;
});
}*/
},
child: Icon(Icons.add),
),
);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: AllProjects(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}