Flutter GetX State Management on button click - flutter

Am fairly new to flutter using GetX. I have MainDrawer stateful widget which has two widgets for two accounts personal and business, in my HomePage() i have TextWidget displaying the name of the account selected..
i want to use controller or any feature of GetX to update the displayed name..
NoTE: i have a MainPage() that hold the drawer and bottom Navigation in which HomePage() is one of the pages

Have a look at the State Management section of the official GetX documentation. With a simple obs and obx you should be able to achieve what you describe.

Related

How to determine if a Widget is Stateful or Stateless in Flutter?

I understand the basics here. If my data is going to change it should be stateful so I can update it otherwise stateless.
My question is more oriented in a bottomnavigation scenario. So, I have 3 tabs in my bottomnavigator (Profile, Home, Settings). The entry point is Home which is a stateful Widget.
The other two Widgets I want to basically populate the information in them with the api data.
The Problem
If I am in the Home screen and I click in the Profile icon in the bottomnavigator it does not load the information from the api. Even if it does, If I click in the Home screen and click again in the Profile screen to go back, the information does not refresh.
Solution
So what would be the way to handle this? I expect that if I click in each of the bottomnavigation items the information is refreshed with the api data. Is this because the Widgets are Stateful or Stateless or I am missing something else?
By default, the pages in Bottom Navigation bar refreshes/rebuild when they are are tapped on.
Make sure a few things in your app-
You are not using a "indexed stack" or any similar widget for these pages. These widgets preserves the state of your page
The navigation is taking place on its own. You have not defined any Navigator.Push etc for tap's in navigation bar.
You should call the API inside the initState method of that particular page. Like if you need profile details, call the api for profile information inside the initState method of Profile Tab and not the "Scaffold" page which has the navigation bar.

When using a ListView in flutter to retrieve a list of json objects from an API. Is it the right way to have an Stateful or Stateless Widget?

So say I have a simple app with 2 routes. The home route is profile and the second is items. So the idea is that when the user clicks in the item icon the app should move to that route and display all the user's items. The question is should this item Widget be Stateless or Stateful? Take in consideration that I expect the user to pull to refresh this Route and reload the api in the Listview.
It should definitely be a Stateful widget since you are expecting data from an API. A stateless widget can't change, you should only use a Stateless widget if the data on the page is static.
I know you already have an accepted answer which is correct, but wanted to add that generally if you use a state management solution like Provider, Bloc, GetX etc...they all provide widgets that will refresh a ListView without needing to be inside a stateful widget.
That will be a cleaner and more scalable solution than calling setState from within a stateful widget.
Its definitely an stateful widget because stateless widget can't change their state.

Flutter: When to create Stateless or Stateful page?

I am new to Flutter. Everything in Flutter is a widget, and there are two types of widgets, which are Stateless and Stateful. Understood that stateless widgets are widgets that will not change or user can't interact with (texts, icons, etc) while stateful widgets are widgets that will change its state for example because of user interactions.
When we want to create a new custom page we usually extends the page from StatelesWidget or StatefulWidget. Since a StatelesWidget can have StatefulWidget as its children and vice-versa, then when should we extend a page as a StatefulWidget or as a StatelessWidget?
Thank you.
If the page itself has some kind of status, then it should be a stateful widget. For example you want to load something remotely, and display a progress indicator while data is being fetched. When loading is complete, the state of the page is changed, and instead of the progress indicator you display whatever you want.
But it is also possible that the page itself is a stateless widget, and has a child widget, a container for example, and this container is stateful, managing the above mentioned remote loading or depends on some kind of user interaction.
State management is a central issue in Flutter, you have many options, and it is not always easy to find the best. You can easily get into fighting the framework instead of letting it to do the job for you. If you are new to this, I would suggest watching some videos, they helped me a lot, for example this or this.

Flutter subclass Scaffold so I can ensure a Drawer and BottomNavigationBar are always present

I have a Flutter app with sections that I can access from both a BottomNavigationBar and a Drawer. I have this working in a Scaffold at the root level of my app, and I then use Navigator widgets to create separate routes and stacks within each section. The BottomNavigationBar persists as I push new screens onto the stack, and these new screens will have their own AppBar and actions specific to those user journeys.
The problem is that I need the Drawer to be present on all of my screens, and setting a new AppBar in a Scaffold on individual screens removes the Drawer that I had set in my root level Scaffold.
How can I pass that Drawer through to all my screen AppBar instances? I don't want to create new Drawer instances over multiple widgets, so I should create a Singleton of the Drawer or even the entire Scaffold? What's the best practise here?
You can use a state management package. I suggest use provider packages or flutter_bloc so that you can get more out of flutter UI but be careful about their name and here is a doc link that is made by flutter team and will help you with all the state management package:
https://flutter.dev/docs/development/data-and-backend/state-mgmt/options
This one is for bloc_flutter doc I couldn't find it on the previous link document: https://bloclibrary.dev

designing an app with multiple pages with different app bar and a single drawer

I am trying to build an app in Flutter in which I have multiple pages, each with their specific actions in the app bar. I would like to add a Drawer to the app which contains a list of page names that take them to the respective pages. I learnt that both AppBar and Drawer widgets have to be part of a Scaffold widget. Currently, all my pages are basically StatefulWidgets with the build method returning a complete Scaffold widget. If I want to include a Drawer, I think I will have to add the drawer object in each of my page. Is there a better way/pattern to do this?
Write the code for the drawer once and enclose it in a function. Then just call the function each time for that drawer.
eg)
drawer: myDrawer()
In one of my Apps, I have something similar. My Drawer is in the app class which is called in the main. This app class calls the home classes. My app class is also Stateful.
Each home class has his own Appbar and Search function.
The Drawer is called only in the app class.
Tell me if this explanation is okay for you or if you need a sample code.