Use Scaffold key for showing snackbar.
class SignIn extends StatefulWidget {
@override
_SignInState createState() {
return _SignInState();
}
}
class _SignInState extends State<SignIn> {
final _formKey = GlobalKey<FormState>();
final _scaffoldKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Hello",
home: Scaffold(
key: _scaffoldKey,
body: Center(
child: ListView(
shrinkWrap: true,
children: <Widget>[
Center(
child: Form(
key: _formKey,
child: Column(children: <Widget>[
Container(
child: Column(
children: <Widget>[
Container(
child: Row(
children: <Widget>[
ElevatedButton(
child: Text("Login"),
onPressed: () async {
_scaffoldKey.currentState.showSnackBar(
SnackBar(
content: Text("Hello there!"),
),
);
})
],
),
)
],
),
)
]),
),
),
],
),
),
),
);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…