我正在尝试使用RefreshIndicator
重新加载我在主屏幕上显示的列表。该代码类似于以下内容:
class Home extends StatefulWidget {
@override
_StartHomeState createState() => _StartHomeState();
}
class _StartHomeState extends State<Home> {
EventsList events;
@override
void initState() {
super.initState();
events = EventsList();
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
body: RefreshIndicator(
onRefresh: resfreshEventList,
child: SingleChildScrollView(
physics: AlwaysScrollableScrollPhysics(),
child: Column(
children: [
HomeTopBar(),
events,
],
),
),
),
);
}
Future<Null> resfreshEventList() async {
await Future.delayed(Duration(seconds: 2));
setState(() {
events = EventsList();
});
return null;
}
}
EventsList
是另一个有状态的小部件,它将调用API并将响应映射到小部件列表。我已经尝试设置physics
的{{1}}属性,如下所述:https://github.com/flutter/flutter/issues/22180,但是没有运气。使用SingleChildScrollView
代替ListView
也不起作用。
答案 0 :(得分:0)
在此示例中,它似乎运行良好,当我进行刷新时,resfreshEventList
被解雇,并且setState
也在正常工作。
这是我正在使用的代码:
import 'package:flutter/material.dart';
class Home extends StatefulWidget {
@override
_StartHomeState createState() => _StartHomeState();
}
class _StartHomeState extends State<Home> {
// EventsList events;
int number = 0;
@override
// void initState() {
// super.initState();
// events = EventsList();
// }
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("RefreshIndicator Example"),
),
resizeToAvoidBottomPadding: false,
body: RefreshIndicator(
onRefresh: resfreshEventList,
child: SingleChildScrollView(
physics: AlwaysScrollableScrollPhysics(),
child: Column(
children: [
// HomeTopBar(),
// events,
Container(
height: 200,
width: 200,
color: Colors.red,
child: Center(
child: Text(number.toString()),
),
),
Divider(),
Container(
height: 200,
width: 200,
color: Colors.red,
child: Center(
child: Text(number.toString()),
),
),
Divider(),
Container(
height: 200,
width: 200,
color: Colors.red,
child: Center(
child: Text(number.toString()),
),
),
Divider(),
Container(
height: 200,
width: 200,
color: Colors.red,
child: Center(
child: Text(number.toString()),
),
),
Divider(),
Container(
height: 200,
width: 200,
color: Colors.red,
child: Center(
child: Text(number.toString()),
),
)
],
),
),
),
));
}
Future<Null> resfreshEventList() async {
// await Future.delayed(Duration(seconds: 2));
// setState(() {
// events = EventsList();
// });
setState(() {
number = number + 1;
});
print("Refresh Pressed");
return null;
}
}
输出: