How to change FAB icon within a StatelessWidget - flutter

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)

Related

why th Scaffold widget is a StatefulWidget?

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.

Can I store a widget from widget and later use it in another page without rebuilding it from scratch? - Flutter

I have a Stateful widget called Widget1 shown in one page. When I open my app the widget1 gets build from scratch and shown in the page. Let's assume the widget1 is quite heavy with lot's of other widget's inside it.
Now I want to display the same widget in another page but I don't want to build the same widget1 from scratch like I did in the first page. I want to reuse the widget which was built in the Previous page to be shown in this page.
Is that possible in flutter? I know flutter must build a widget inorder to make changes in Ui. But is this really possible? that is , to store a widget and use it, whenever we want without having to rebuild it, again from scratch?.

Make Stateful Widget inside a Stateless Class

Is it possible to make a Text Widget Stateful so that the Text can be changed when i press a Button without having my Class to be Stateful?
example:
class Rangliste extends StatelessWidget{.... Text(Test), Text(Test2)....}
Test and Test2 are Strings.
So when i press a Button the values inside the Text Widgets should change. Is there any Way to achieve this without changing my Class to Stateful? Or if not can somebody explain how i can change the Class to Stateful without messing up the rest of my Code
No, you can't, a stateless widget is an immutable widget that cannot react to state changes and rerender, you will have to use some form of state management.
If your button and your text and your data are all within the same widget, then it is better to make your widget Stateful and use setState(() {}) to change your text and rerender.
You can use the Flutter plugin in order to convert your widget from a Stateless widget to a StatefulWidget, the plugin will handle 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 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.