Reputation: 1
A RaisedButton will call a Snackbar and have setState(). However, the screen is tapable before Setstate change Below, is the part of code:
child: RaisedButton(
child: Text('Click Me'),
onPressed: () {
theMessage = getMessage();
Scaffold.of(context).showSnackBar(SnackBar(content: Text(theMessage), duration: Duration(seconds: 3),
));
_timer = new Timer(const Duration(seconds: 3), () {
setState(() {
n++;
});
});
}),
Upvotes: 0
Views: 1990
Reputation: 2341
You can use the Future
returned by showSnackBar
to control the behavior. To disable tapping you can use widget AbsorbPointer
. Here is an example using both of them together:
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _absorbing = false;
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
void _onButtonPress() {
setState(() {
_absorbing = true;
});
print("Do your work here");
snackBarWithCallback("Hello", () {
print("Done");
setState(() {
_absorbing = false;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: Text(widget.title),
),
body: AbsorbPointer(
absorbing: _absorbing,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text('Pres Me'),
onPressed: _onButtonPress,
),
],
),
),
),
);
}
void snackBarWithCallback(String content, VoidCallback callback) {
final controller = _scaffoldKey.currentState.showSnackBar(
SnackBar(
content: Text(content),
),
);
controller.closed.whenComplete(callback);
}
}
Upvotes: 1