It was my last day of work and I'm happy I finished my remaining tasks on time. And now, it's time for me to move on to my next journey. The only reason I took the job was for me to get enough experience to build my own app. So I already planned it that I will quit my job after 3 years since I signed the 3-year bond contract. Didn't know that it was tough having one source of income but I think I'm good since I'm still living with my parents... There are times that I compare my income and life to my coworkers, same age brackets and shit... which resulted for me to lose my focus and my mind shifted a lot of times, and I think it's a waste of time to repeat it again. But... at the end of the day... I'm just too lazy I guess and I think that a day job is not suited for me. I'm more of the build one app and make it rain type of guy... Don't even know what to do now!!! I guess I'll try playing stocks and multiplying my money while trying to build my ...
There are 3rd party libraries out there that offer alerts or popups in Flutter.
But, what I'll be teaching you today is how to use the built in
SnackBar widget to produce an error popup or notification easily.
For the video version of the tutorial go here: How to use snackbar in flutter as error alert or popup notification
To use the SnackBar widget and make a nice Snack Bar that you see in
most google apps like youtube, all you need to do is wrap your entire app with
the Scaffold Widget. So let's make create a new file page_wrapper.dart
and create a new Widget named PageWrapper that accepts a widget as
children.
After that all we need to do is call the PageWrapper widget in all of our
pages so that we have a base widget that uses the Scaffold in every page. This
means we can now use the SnackBar on every page. In the main.dart all you need
to do is this:
Then now we can create the home page and create a snackbar. Before that we
need a trigger so we'll be using the RaisedButton as the trigger and onTap
we'll call the showSnackBar method to create a snackbar. Also, We'll be adding
a SnackBarAction named dismiss which when called will hide the snackbar so
that we have a way for the user to hide it whenever she wants.
On the onTap place the following:
Let's get the scaffold state by doing this:
ScaffoldState sState = Scaffold.of(context);
Then after we got the scaffold state we can call the showNackBar like this:
sState.showSnackBar(
SnackBar(
content: Text("Hi Im snackbar!!!"),
action: SnackBarAction(
label: 'Dismiss',
onPressed: () {
sState.hideCurrentSnackBar();
}
)
)
);
As you can see the snackbar accepts a SnackBarAction as param which is a
button that can be triggered by pressing. In this case we just want to hide
the current snack bar by pressing the dissmiss text.
It should look like this:
SnackBar |
And there it is we can now safely use the snackbar on every page without
seeing the error:
Scaffold.of() called with a context that does not contain a Scaffold.
Here is the complete code of the home page:
And this is the complete demo of the app (see it in action!):
See the Pen Flutter: How to create an error alert or popup notification with SnackBar by doppelgunner (@doppelgunner) on CodePen.
A man's errors are his portals of discovery. - James Joyce
Thanks for sharing such an astounding set of information with us. To begin freelancing as a career, it is important to get the right opportunity. I got this opportunity via Eiliana.com, which helped me become the Best coder in India. And, now I feel I am practically commencing to ace my skills and approach my dream.
ReplyDelete