注意:
build(BuildContext context)在 Scaffold.of(context)之前时,会报错,解决办法:
通过build widget来解决,如下代码。
import 'package:flutter/material.dart'; main() => runApp(MaterialApp(
home: MyHome(),
)); class MyHome extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return MyHomePageState();
}
} class MyHomePageState extends State<MyHome> {
Widget sdtSnack = SnackBar(content: Text('sdt')); @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('BottomNavigationBar Sample'),
),
body: Center(
child: new ListView(
children: <Widget>[ new FlatButton(
onPressed: (){
Scaffold.of(context).showSnackBar(sdtSnack);
},
child: new Text('我是按钮'),
), Builder(builder: (BuildContext context) {
return new Center(
child: new GestureDetector(
onTap: () { final mySnackBar = SnackBar(
content: new Text('我是SnackBar'),
duration: Duration(seconds: 4),
action: new SnackBarAction(
label: '我是scackbar按钮',
onPressed: () {
print('点击了snackbar按钮');
}),
); Scaffold.of(context).showSnackBar(mySnackBar);
},
child: new Text('点我显示SnackBar'),
),
);
}), ],
),
),
);
}
}
========================================================
taskRun(){
...
Widget mySnack = SnackBar(content: Text('There is a task alreay running.'));
Scaffold.of(context).showSnackBar(mySnack);
....
}
body:
...
Container(
margin:
EdgeInsets.only(left: 10.0),
width: 30,
child: Builder(builder: (context){
return InkWell(
child: statusWidget(record['status']),
onTap: (){
taskRun(data, index, context);
},
);
}),
),
....