Flutter, my inputs getting cleared when re-openning page with bottomnavbar - flutter

I have a bottomnavigation bar and 3 pages. I move between these 3 pages with Get.to() in bottomnavigationbar. I have some input fields in one of those pages. I write inputs then I go to another page with bottomnavigationbar and then I reopen that page again but every input I wrote is cleared. I want to save the state of that page or with other words, I don't want these inputs to get cleared when moving to another page. How can I achieve it?

Because you are using Get.to(), meaning you using get libbrary.
In this case you need to put your TextEditingController to your GetxController and binding in the page that contains your bottomnavigationbar. Now the parent page was hold the GetxController who will store your textfield state instead of widget state.
Or just adding permanent: true to your controller binding.

Related

Is it possible for a ValueListenableBuilder to still apply changes on a page that is not on top of stack?

I have two stateless views using ValueListenableBuilder with the same valueListenable, which is of type ValueListenable<List>. This listenable is on a repository class which is a singleton. When I navigate to page 2 from 1 I would like that the ValueListenableBuilder on page 1 would still rebuild even if the page is not top of stack. Is it possible?
Because right now, when I go back to page 1, the view is not reflecting the correct value of the listenable.

How to validate TextFormFields, that are at different pages inside a PageView

So lets say I have 3 Pages : [StartingPage, NamePage, AdressPage] inside a PageView.
Each of those children has a TextFormField inside it.
When I am at StartingPage I want to call .validate() on the keys of the TextFormFields on NamePage and AdressPage and I want the errorLabels to show, when the user navigates to the other pages. The problem is that currently, the validation only works for the TextFormFields that are on screen.
So basically there are two questions:
Is it possible to pre-build pages of a pageview? Otherwise when I call formFieldKey.currentState the state is null if I haven't navigated to the respective page.
Is there another way to validate offscreen TextFormField content and show the errorlabel, when they come into view?
Unfortunately Flutter Dosen't Support Multi-Page Forms So You Need To Implement Advanced State Management To Make This Happen.
I Used Riverpod To Create a 4 Page Form By Creating A Form Key For Every Page And Saving The State of the Forms, This Way Every Form Will keep Its State Until You Dispose Of it, Hope This Helped.

how to prevent PageView.builder building page I didnt enter yet or delete getxcontroller after I leave the page - flutter/GetXcontroller

sorry in advance if i didnt describe my question clearly:
I have several pages made by PageView.builder, while swiping these pages on main screen left and right, I found their initState is already working, and GetXController inside are also loading their data. I use Get.delete<Controller> while clicking back button and coming back to main screen to remove these data.
However, the state and data will bring back to main screen if I didnt activate Get.delete<Controller>, and therefore next time I come to this page, the state of page will be not default, for instance the button still keep pressed status.
So my question is how to prevent page.builder to build the widget before I come inside to this page, or how to delete getxcontroller if I am not in this page. Thanks a lot!
Try resetting the GetX by calling
Get.reset(); --> this will re-initialize controllers
Alternatively calling
Get.put(ControllerName()); in a build method will re-initialize the controller

Using bottom navigation with individual Navigators but single AppBar

I am trying to create an app that has the following basic setup for navigation:
tab1 -> subpage
tab2
tab3
with a bottom navigation bar for switching between the tabs and each tab having its own navigation stack. Furthermore I would like to add an AppBar with a drawer (as endDrawer) and the back button as leading widget in the AppBar.
The AppBar should be centrally managed so I put it in the main page of the app for maintenance purposes instead of having it on every page that is pushed. The drawer should also be updated later on when the user is logging in or out so it encouraged me more to place the AppBar only in the main page.
Now the issue is that I would like to update the AppBar to contain the back button when the app navigated to the subpage of tab1. Since the AppBar is in the parent I thought of RouteObservers as a possible solution by registering a RouteObserver on each NavigationState.
Yet I cannot get it to work because when I would like to subscribe on the RouteObserver in the HomePage in the didChangeDependencies() the currentState of the NavigatorState does not exist. Also I tried subscribing in the build method after the Scaffold with a flag for subscribing only once, but this didn't work either.
I created a github project with the basics of my current implementation:
https://github.com/S-Braeutigam/bottom-navbar-single-appbar
To get to the current state of code I used the following links:
https://codewithandrea.com/articles/multiple-navigators-bottom-navigation-bar/
https://api.flutter.dev/flutter/widgets/RouteObserver-class.html
Flutter 2.0 appbar back button disappeared if contains endDrawer (The solution gets close to what I would like to implement, but the AppBar should always be visible, not only on the sub page)
My main questions are:
Are RouteObservers the correct approach on how to track the navigation to subpages of tabs and display the back button based on it?
At what lifecycle hook in the main page should or could the subscription on the currentState of each NavigatorState be done? Wasn't able to find any where they are not null.

Flutter SetState is not refreshing UI

I'm building multiple form pages in one class, I create 4 widgets each widgets is represent a page with text fields or cards or radio buttons.
and I create a method to switch between pages when. I press next.
This works as expected ^^
My issue UI inside first page is not refreshed unless I press ctrl + S,,
inside the first page of the form, I have created custom radio buttons they're simply cards.
and when tap on any card, there's set state method, to highlight the selected card by changing its style..
This works perfectly! at the beginning when I call the first page 1 directly as a child inside the scaffold..
However, after I created the method to switch between pages.. The UI in page 1 does not refresh..
I will put a picture..
Picture of the method to switch pages
Picture of pages widgets
picture of scaffold and how I call the widget pages
this is a picture of custom radio button which has cards, the card color change if it's selected
I tried many ways! but it didn't work..
Is what I'm trying to do correct? I'm a beginner in flutter
& I have a project to submit :(
I couldn't find a solution in google,,
Try making the widgets StatefulWidget and call setState((){}) inside the widget when you tap on the cards.
So here's what I did ^^
because the issue was just in page 1, so I in the scaffold I added page 1 as it's child directly without Switch page method..
Then inside page1, a button that will navigate to another class,
the second class has exactly the same scaffold and app bar as previous page.. But, in it's child I added calledWidget which switch between pages from 2 to 4 in my case ^^
So short words, I put page 1 in a separate page and the rest of the pages in a switch method in separate class
it works, but I hope I don't find any difficulties When saving values & integrating API to post the values
what you think?