gpt4 book ai didi

flutter - RefreshIndicator 不适用于 SingleChildScrollView 作为子项

转载 作者:行者123 更新时间:2023-12-04 12:35:39 29 4
gpt4 key购买 nike

我正在尝试使用 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 SingleChildScrollView 的属性(property)如此处所述: https://github.com/flutter/flutter/issues/22180但没有运气。使用 ListView而不是 SingleChildScrollView也不起作用。

最佳答案

在这个例子中它似乎工作正常当我拉动刷新然后 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;
}
}


输出:

enter image description here

关于flutter - RefreshIndicator 不适用于 SingleChildScrollView 作为子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62096268/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com