why th Scaffold widget is a StatefulWidget? - flutter

by coincidence, I looked at the Scaffold widget implementation source code, then I noticed that it's a StateFulWidget
why it's a StatefullWidget?
what parts or things need it to be StatefullWidget?

Scaffold widget has a feature called drawer, which has a state for itself, so when you need to open it, Scaffold changes its state and open it. Other feature like showing snackBars, BottomSheet and FloatingActionButton do the same.

Related

Can I show snackbar in a widget which directly has no scaffold but has in a parent widget?

I want to use snack bar in a widget but the scaffold is in a different widget. I have used ScaffoldMessenger but not showing and no errors.[ScaffoldMessenger used in a widget that is two widget inside of parent widget which has a scaffold]
If you pass the BuildContext (of the different widget which holds the Scaffold) into the widget from where you'd like to show the Snackbar, you should be able to use ScaffoldMessenger.of(passedContext).showSnackBar()
If this doesn't answer your question. Please provide a minimal code snippet of what you've tried.
also, you can show SnackBar using GET,
you have to add get in your pubspec.yml in dependencies section
code : Get.snackbar("Hi..", "How are you !");

What is the difference between a data type widget and a stateless widget in Flutter?

I'm quite new to flutter and I'm trying to figure out the best way to build my widgets in the aspect of good performance while I have three choices:
1- Stateful Wigdet.
2- Stateless Widget.
3- Widget function(){} with a StatefulBuilder if needed.
Thanks.
A StatefulWidget has State, which means that you can change stuff inside the widget without needing to destroy it and create a new version. They work slower than StatelessWidget but are better for checkboxes or screens which need to be frequently updated.
A StatelessWidget is locked, and you cannot change values once it is initiated. In order to change things inside the widget, you need to re-call the build function to build an updated version. They are quite fast but are best used for static screens.
Widget function(){} widgets are custom-made. Essentially, you can take any existing widget and modify it, adding childs and other widgets to it. However, the widget type normally inherits from a StatefulWidget or StatelessWidget. These can cover basically everything.

In Flutter while building your app how do we decide when to use a StatelessWidget or StatefulWidget?

The core concept of StatelessWidget and StatefulWidget is confusing to me.
According to flutter documentary:
A widget is either stateful or stateless.
If a widget can change—when a user interacts with it, for example—it’s stateful.
A stateless widget never changes. Icon, IconButton, and Text are examples of stateless widgets. Stateless widgets subclass StatelessWidget.
so mainly if you have something on the screen that changes when user interacts with it, you should use stateful widget for it and otherwise, you should use stateless widget.
for example if you have a plus button on the screen and a a number on the screen which should be increased every time the user press it, you should use stateful widget to notify flutter that the text on the screen should be changed and rerendered.
for more information you can check here.

How to change FAB icon within a StatelessWidget

I started with the default Statfull Widget HomePage and a FAB.
The main widget in the page is a ListBuilder that is updated from a stream using Provider.
When an item on the list is clicked, I need to change the icon on the FAB.
All this works fine.
Now I wonder, If I would like to change the home page as a StatelessWidget. So I don't need to build the entire page just to change the icon on the FAB.
How can I update the Icon if I don't have setState() to rebuild the page?
the best answer should be use some sort of state management, but here's a quick and easy way to achieve your goal
create a function in your parent widget
Function callback(){
setState((){
//change Icon
});
}
Then pass it on to your stateless widget
CustomFabWidget(callback)

How to make a Dialog to be a child of a widget in Flutter?

I have struggled to access data from a Provider in a Dialog widget. But i got a general ERROR from Provider.
After I took a look at the widget tree I saw that the Dialog widget was the child of Material widget not the child of the widget from I am showing it.
So the error is wright: I don't have any Provider above my SilverList Widget.
My question is: Can I make somehow the Dialog widget to be the child of my widget, where (ofcourse) the provided information is accessible?
EDIT 1:
I am already passing the context of the widget parent to the showDialog() builder method:
The problem was that I didn't understand well how the widget tree build the drawer. A drawer is build as a new screen so it will be at the same level with the screen where I pushed from the drawer.
The provider can be seen just down in widget tree, and not at the same level.
So the solution was to move Provider widget in the main.dart right after MaterialApp.