底部弹出,然后在指定时间后消失。
注意:
build(BuildContext context)在 Scaffold.of(context)之前时,会报错,解决办法:
通过build widget来解决,如下代码。
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('BottomNavigationBar Sample'),
),
body: Center(
child: new ListView(
children: <Widget>[
new FlatButton(
onPressed: null,
child: new Text('我是按钮'),
),
new Builder(builder: (BuildContext context) {
return new Center(
child: new GestureDetector(
onTap: () {
final mySnackBar = SnackBar(
content: new Text('我是SnackBar'),
backgroundColor: Colors.red,
duration: Duration(seconds: 1),
action: new SnackBarAction(label: '我是scackbar按钮', onPressed: () {
print('点击了snackbar按钮');
}),
);
Scaffold.of(context).showSnackBar(mySnackBar);
},
child: new Text('点我显示SnackBar'),
),
);
}),
],
),
),
);
}
}
结果: