Trying to figure out this flutter problem. The below code has the showSnackbar as deprecated, and am trying to figure out with the fix is. The second code is my attempt to fix the problem. A new problem comes up with "The getter 'ScaffoldMessenger' isn't defined for the type 'ScaffoldState'.". The error tells me to import material.dart file but it is already imported.
Any help is appreciated.
Padding(
padding: const EdgeInsets.all(10.0),
child: GestureDetector(
onTap: ()async{
if(!await authProvider.signIn()){
_key.currentState.showSnackBar(
SnackBar(content: Text("Login failed"))
);
}
},
Padding(
padding: const EdgeInsets.all(10.0),
child: GestureDetector(
onTap: ()async{
if(!await authProvider.signIn()){
_key.currentState.ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Login failed"))
);
}
},
showSnackBar shows a SnackBar at the bottom of the scaffold. This method should not be used, and will be deprecated in the near future..
You can hide the current SnackBar before the end of its duration by using: ScaffoldMessenger. of(ctx). hideCurrentSnackBar();
you probably have a parent widget that contains Scaffold as well. Create a scaffoldKey for that and pass it to your child widget that have to show the Snakcbar . Note that you can't use Sanckbar without Scaffold widget.
You can do this by placing a container inside the snackbar. Since snackbar can take any widget and you can change its background color to transparent, you can use a container with custom padding and borders to give an illusion of positioning.
Based on the documentation here, it looks like the new ScaffoldMessenger handles all SnackBars below it. If you don't have multiple ScaffoldMessengers, you should just be able to call:
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("Login failed")));
This is the new way to add snackBars to the scaffold.
ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text("Incremented"), duration: Duration(milliseconds: 300), ), );
Using _key.currentState.ShowSnackBar(snackbar)
Is deprecated now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With