我正在尝试获取当前的小吃店(显示一个),以确定它是否是试图显示的相同的小吃;换句话说,我不想复制该小吃,但我无法获得它。
我试着在谷歌上搜索它,但没有显示出来,真正让我生气的是,当我多次调用Scaffold.of(context).showSnackBar(sb);时,他们会一次显示一个,直到它们结束。
import 'package:flutter/material.dart';
const sb = SnackBar(
content: Text('the snack bar'),
);
void main() => runApp(MaterialApp(
title: 'Snack bar test',
home: Scaffold(
appBar: AppBar(
title: Text('snack bar demo'),
),
body: Center(
child: MyApp(),
),
),
));
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return RaisedButton(
child: Text('push to test'),
onPressed: () {
// Scaffold.of(context).
Scaffold.of(context).showSnackBar(sb);
},
);
}
}我想要实现的东西是这样的:
if (sb.isShowing()){
//hide or remove it
}提前谢谢你。
发布于 2019-07-26 10:31:07
只需调用removeCurrentSnackBar()删除当前的snackbar即可。
import 'package:flutter/material.dart';
const sb = SnackBar(
content: Text('the snack bar'),
);
void main() => runApp(MaterialApp(
title: 'Snack bar test',
home: Scaffold(
appBar: AppBar(
title: Text('snack bar demo'),
),
body: Center(
child: MyApp(),
),
),
));
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text('push to test'),
onPressed: () {
// Scaffold.of(context).
Scaffold.of(context).showSnackBar(sb);
},
),
RaisedButton(
child: Text('Remove snackbar'),
onPressed: () {
// Scaffold.of(context).
Scaffold.of(context).removeCurrentSnackBar();
},
),
],
);
}
}https://stackoverflow.com/questions/57210423
复制相似问题