How to stop page reloading in flutter after using setState in Checkbox - flutter

I use flutter web I put a checkbox for users. The problem is that the page contains a different TextField.So, in the event that the user selects one of the Checkbox options, a page refresh occurs.Then all TextField data is reloaded and all data that was previously inserted is deleted.The problem is caused by setState(()).But I can't delete it if the setState checkbox is deleted. The checkbox doesn't work.
bool _checkbox = false;
Checkbox(
value: _checkbox,
onChanged: (value) {
setState(() {
_checkbox = !_checkbox;
});
},
);
How can this problem be solved without deleting the data from the fields?
Thank you

wrap in StatefulBuilder to reload only the checkbox

Related

Flutter. How to correctly update state of nested widgets?

I'm new in flutter and I'm trying to implement something like radio group with custom buttons in Android. For this I created StatefulWidget, which hold list of selectable buttons. For every button I was set press listener where I do something like this:
setState(() {
buttons.forEach((button) => button.isSelected = false);
buttons[selectedButtonIndex].isSelected = true;
});
And then my CustomButtonWidget changes color depending on the parameter isSelected .
All this works well. However, I have an additional requirement. I need my RadioGroupWidget to return the selected button type. For this I created a callback :
final ValueChanged<ButtonType> onChanged;
And now my button press listener looking like this:
onTap: () {
setState(() {
buttons.forEach((button) => button.isSelected = false);
buttons[selectedButtonIndex].isSelected = true;
});
onChanged(buttons[selectedButtonIndex].type);
}
Next I get this type of button in my other widget which is using RadioGroupWidget:
CustomRadioGroup(
onChanged: (value) {
setState(() {
buttonType= value;
});
}),
)
As you can see I call again setState. This is what leads to the problem. But I need to do this, because I need to update the state of another widget (for example let's call it InfoWidget) depending on the selected button.
After all these manipulations, the state of the InfoWidget is updated correctly, but the state of the selected button in the RadioGroupWidget does not change. I tried to debug this and I see that at first the parameter isSelected is set to true for the desired button, but after the state of the button I selected is not updated, because its parameter isSelected becomes false. And I don't understand why this is happening.
Please help, I am completely confused.

Flutter - how do you update the value of textfield without reopening the widget?

I have multiple TextFormField, all have their own value from a file.
My problem:
-when I change the value of the TextFormField, it changes the value of it's own parameter, but it only changes the other TextFormField(based on the same parameter) when I reopen that page.
I tried use setState, but I don't know what parameter should it have.
How should I use setState / what would be a better option?
This is how I change the values:
onChanged: (value) =>
{ class.data.minimum = int.parse(value),
setState(() {
class.data.minimum=int.parse(value);
})
},
by default for text you can use ValueKey, but in your case use globalKey

Flutter dropdownButton setState on change not updating dropdown text after selection to show selected item

I created a dropdownButton to allow users to select from a dropdown list which will be populated from an API. For now I am populating using a list I created.
Currently the button is displaying the items from my list but after a selection has been made the list doesnt show the selected item and still shows the hint text. What I would like to happen is after a selection has been made then the dropdownButton shows the item that was selected instead of the hint text.
in the onChanged method I added a setState in hopes of updating the _selectedValue variable to the value that was selected and displaying it in the dropdownButton.
I also added a print statement in my setState method and that does trigger and show me the value within the value variable.
Here is my code.
List<DropdownMenuItem<int>> listItems = [DropdownMenuItem(child: Text("2016"), value: 2016,), DropdownMenuItem(child: Text("2021"), value: 2021,)];
int _selectedValue;
body: DropdownButton(
value: _selectedValue,
items: listItems,
hint: Text("Select Year"),
onChanged: (int value){
setState(() {
_selectedValue = value;
print(value);
});
},
),
Your code is fine, but the problem is maybe you are initializing the _selectedValue inside the build() method. So that whenever you call set state the widget rebuilds and initialize again with the default value.
int _selectedValue;
#override
Widget build(BuildContext context) {
return DropdownButton(
value: _selectedValue,
items: listItems,
hint: Text("Select Year"),
onChanged: (int value){
setState(() {
_selectedValue = value;
print(value);
});
},
),
In case anyone else is having this issue, I had the same issue and it was driving me nuts and I was using setState() appropriately etc., It was 100% where I was defining the "initialValue" property.
I was defining it right inside the build method. That did not work, even though I had the same setup in a sister module and it indeed did work. Not sure why that is.
Once I changed the definition of that variable to the state class, it worked like a charm. Hope it saves some, some cycles.

How to keep the state of a PageView that is used inside ScreenTypeLayout? (Flutter Web)

I am using a package called responsive_builder to create a web responsive UI.
My issue is that this package rebuilds the widget that corresponds to the constraints after changing window size.
This causes the loss of user data if inserted in a TextField for example. Also this causes PageView to lose its state meaning losing the current page index if the page was change earlier before adjusting window size.
Any suggestions?
I have had a very similar issue happen with a closing/opening container with a Textfield.
You need to use Shared Preferences to store that data into the memory. Shared Preferences effectively keeps that typed data in a map - which is stored in memory and can be used anytime.
Also I highly recommend using Flutter Form Builder for your text fields as it allows you to have a initialValue in your TextField and has built in Validation.
Because there is no code to go off, your Textfield should look something like:
SharedPreferences prefs = await SharedPreferences.getInstance();
String firstName = '';
_persistantText(String value) async {
firstName = await prefs.setString(value);
}
FormBuilderTextField(
attribute: "firstName",
decoration: InputDecoration(labelText: "First Name"),
validators: [
FormBuilderValidators.required(),
FormBuilderValidators.max(70),
],
initialValue: firstName
onChanged: (value){
_persistantText(value);
}
),

Flutter: Send data to parent page & Reload

I'm new to flutter but I'm in love with it! :D
I'm doing a small application. It requires reloading the parent page when coming back from the child page.
I'm using Navigator.push() method to move between pages. I want to reload the parent page after calling Navigator.pop() method.
How can I do that?
Thanks,
-Naveed
On first widget
onPressed:() async {
var value = await Navigator.pushNamed(context,"/second");
setState(){
print(value);
}
}
on second widget
Navigator.pop(context, param); //pass parameter here