如何在刷新指示器隐藏后正确显示Snackbar
我有RefresHindicator
及其内部listView
,因此RefresHindicator
在list> listView 上工作,我也设置了<代码> globalkey&lt; refreshindicatorState&gt;()
to felreshindicator
,以便我可以在按钮点击上显示指示器。
一切都是拳头
final _refreshIndicatorKey = GlobalKey<RefreshIndicatorState>();
@override
Widget build(BuildContext context) {
return RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: _onRefresh,
child: buidlListView(snapshot),
);
}
Widget buidlListView(AsyncSnapshot<List<Model>> snapshot) {
if (snapshot.hasData && snapshot.data!.isNotEmpty) {
return buildModelList(snapshot);
}
return buildNoModelList();
}
Widget buildNoModelList() {
return ListView(
children: [
ElevatedButton(
onPressed: _onRefresh,
child: const SizedBox(
width: double.infinity,
child: Text('Refresh', textAlign: TextAlign.center),
),
],
);
}
Widget buildModelList(
AsyncSnapshot<List<Model>> snapshot,
) {
final modelList = snapshot.data!;
return ListView.separated(
padding: const EdgeInsets.all(16),
itemCount: modelList.length,
separatorBuilder: (context, index) => const SizedBox(height: 16),
itemBuilder: (context, index) {
final model = modelList[index];
return ModelCard(
key: ValueKey(model),
model: model,
);
},
);
}
Future<void> _onRefresh() async {
// ignore: unawaited_futures
_refreshIndicatorKey.currentState?.show();
final isModelExist = await loadModels();
// not returning this because i want to show snack bar after indicator hides
Future<void>.delayed(const Duration(seconds: 3));
if (!isModelExist) {
showSnackBar();
}
return Future.value();
}
void showSnackBar() {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
behavior: SnackBarBehavior.floating,
content: Text('No Model Found'),
),
);
}
Future<bool> loadModels() async {
final snapshot = await modelCollectionRef().get();
if (snapshot.size > 0) {
//...
return true;
}
return false;
}
i have RefreshIndicator
and inside it ListView
so that RefreshIndicator
work swipe down gesture on ListView
, also i have set GlobalKey<RefreshIndicatorState>()
to RefreshIndicator
so that i can show indicator on button tap.
all is wokring but in _onRefresh
i am calling showSnackBar
but snakbar showing twice i dont know why
final _refreshIndicatorKey = GlobalKey<RefreshIndicatorState>();
@override
Widget build(BuildContext context) {
return RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: _onRefresh,
child: buidlListView(snapshot),
);
}
Widget buidlListView(AsyncSnapshot<List<Model>> snapshot) {
if (snapshot.hasData && snapshot.data!.isNotEmpty) {
return buildModelList(snapshot);
}
return buildNoModelList();
}
Widget buildNoModelList() {
return ListView(
children: [
ElevatedButton(
onPressed: _onRefresh,
child: const SizedBox(
width: double.infinity,
child: Text('Refresh', textAlign: TextAlign.center),
),
],
);
}
Widget buildModelList(
AsyncSnapshot<List<Model>> snapshot,
) {
final modelList = snapshot.data!;
return ListView.separated(
padding: const EdgeInsets.all(16),
itemCount: modelList.length,
separatorBuilder: (context, index) => const SizedBox(height: 16),
itemBuilder: (context, index) {
final model = modelList[index];
return ModelCard(
key: ValueKey(model),
model: model,
);
},
);
}
Future<void> _onRefresh() async {
// ignore: unawaited_futures
_refreshIndicatorKey.currentState?.show();
final isModelExist = await loadModels();
// not returning this because i want to show snack bar after indicator hides
Future<void>.delayed(const Duration(seconds: 3));
if (!isModelExist) {
showSnackBar();
}
return Future.value();
}
void showSnackBar() {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
behavior: SnackBarBehavior.floating,
content: Text('No Model Found'),
),
);
}
Future<bool> loadModels() async {
final snapshot = await modelCollectionRef().get();
if (snapshot.size > 0) {
//...
return true;
}
return false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅以下示例,
我在下面使用的插件中使用了刷新指示器
pull_to_refresh: ^2.0.0
Please refer to below example
I have used below mentioned plugin for refresh indicator
pull_to_refresh: ^2.0.0
解决了该问题是
_refreshindicatorKey.currentState?.show();
我在_onrefresh()
上调用的问题刷新过程,然后在_ONREFRESH()
中,我们正在调用_refreshindicatorKey.currentState?.show();
哪个告诉Code> RefreshIndicator 运行回电运行
on按钮点击,然后onrefresh
也反对_onrefresh()
,这就是为什么_ONREFRESH()
触发了两次,而Snakcbar显示了两次_refreshindicatorKey.currentState?.show();refreshindicator
它自动运行_ONREFRESH()
callback。Solved the problem in is
_refreshIndicatorKey.currentState?.show();
which i was calling on_onRefresh()
and using this method this on button tap so when its first trigger ths refresh process but then in_onRefresh()
we are calling_refreshIndicatorKey.currentState?.show();
which tellRefreshIndicator
run call backonRefresh
which also refrenced to_onRefresh()
so thats why_onRefresh()
triggred two times and snakcbar showed two times so to solve this i just call_refreshIndicatorKey.currentState?.show();
on button tap and thenRefreshIndicator
it self run_onRefresh()
callback.