flutter - 刷新按钮的正确实现
问题描述
这似乎是一个简单的问题(也许确实如此),但我是 Flutter 的新手,我找不到实现此功能的正确方法。
我在CupertinoPageScaffold
. 脚手架位于CupertinoNavigationBar
具有CupertinoButton
“刷新”按钮的内部。
小部件具有FutureBuilder
异步构建内容的功能。
通过按下这个按钮,我想刷新脚手架内小部件的状态。
按钮当然有onPressed()
闭包,但我不明白按钮如何与小部件的状态交互(实际上,setState()
应该只在小部件的状态本身内调用)。
+---------------------------+
| Refresh |
| Button+-----------------------------+
+---------------------------+ |
| | |
| +--Stateful Widget+---+ | |
| | | | |
| | +----------------+ | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | State | | | |
| | | & | | | |
| | | Future <------------------+
| | | Builder | | |
| | | | | |
| | | | | |
| | | | | |
| | +----------------+ | |
| | | |
| +---------------------+ |
| |
+---------------------------+
如何实现此刷新按钮的正确方法?
编辑
目前这就是我所拥有的:
class PostListScaffold extends StatelessWidget {
final CategoryMetadata category;
PostListScaffold({this.category});
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: Image(image: AssetImage("assets/logo.png")),
heroTag: "post_list",
transitionBetweenRoutes: false,
actionsForegroundColor: MyColors.mainColor,
),
child: PostsTableList(category)
);
}
}
class PostsTableList extends StatefulWidget {
final CategoryMetadata category;
PostsTableList(this.category);
@override
PostsTableListState createState() => PostsTableListState(category: category);
}
以及使用 a 构建列表的状态FutureBuilder
:
class PostsTableListState extends State<PostsTableList> {
final CategoryMetadata category;
int _currentPaginationIndex = 1;
final _apiPosts = APIPost.standard();
PostsTableListState({this.category});
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
future: _getCall(_currentPaginationIndex),
builder: (context, AsyncSnapshot<List<PostMetadata>> snapshot) =>
_buildListWhenAvailable(context, snapshot)
)
);
}
Widget _buildListWhenAvailable(BuildContext context, AsyncSnapshot<List<PostMetadata>> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.active: {
return CupertinoActivityIndicator();
} break;
case ConnectionState.done: {
if (snapshot.data == null || snapshot.data.length == 0) {
return EmptyDataRefresh("No post available. Try to refresh",
onRefresh: () {
this.setState(() {});
});
} else {
return ListView(children: _getListData(context, snapshot));
}
} break;
case ConnectionState.none: {
return EmptyDataRefresh("No post available. Try to refresh",
onRefresh: () {
this.setState(() {});
});
} break;
case ConnectionState.waiting: {
return CupertinoActivityIndicator();
}
}
return Text("Errore sconosciuto. Provare di nuovo");
}
Future<List<PostMetadata>> _getCall(int paginationIndex) {
if (category == null) {
return _apiPosts.fetchPostMetadata(_currentPaginationIndex);
} else {
return _apiPosts.fetchPostsWithCategoryID(
this.category.id); // For now this call doesn't support pagination
}
}
List<Widget> _getListData(
BuildContext context, AsyncSnapshot<List<PostMetadata>> snapshot) {
List<Widget> widgets = [];
for (var post in snapshot.data) {
widgets.add(PostRow(post: post));
}
return widgets;
}
}
解决方案
你应该使用流
当您按下刷新按钮时,它应该添加到您的接收器中。并且所有订阅流的小部件都将被刷新。
我认为您应该更多地了解流。尝试学习bloc 模式,它依赖于流的概念。
这是一个帮助您入门的链接:https ://medium.com/flutterpub/architecting-your-flutter-project-bd04e144a8f1
推荐阅读
- javascript - Eclipse 2020 Javascript 编辑器默认不工作
- angular - Angular 10/Ionic 5 - 将输入数据从模态传递到父组件
- regex - 正则表达式获取两个识别文本块之间的文本
- javascript - 在 Mapbox GL JS 中添加 WMS v 1.3.0
- php - 我想在 WPBakery 元素简码中更改 ::before 伪元素的样式
- angular - Autodesk forge 查看器文档加载失败
- java - 模块的运行时访问
- css - 如何防止我的引导警报在显示时阻止我的导航中的项目?
- io - 如何加快 PyPy3 中的文件 I/O?
- discord.js - 不和谐没有在嵌入上打印自定义表情符号?