How to update Inner Widget in Flutter - flutter

Hi I am having an issue with radio and checkbox on flutter. I have created my own widget on the purpose of having different return widget depending on what is needed as on the picture bellow since I selected change pin the return will be a checkbox but the return widget is a checkbox while the save button is stagnant or didn't change but I am having an issue when i used radio or check box even Visibility. When I try to update the setState() it is not updating or reloading the widget. The code below is just the first return of the widget which will be a check box. When i try to unchecked the button the value becomes falls but the UI is not unchecked.
sample Image
Widget getInputType(val){
if(val == 'Pin Lock'){
return CheckboxListTile(
title: Text('Pin Login'),
value: isSwitchPin,
onChanged: (value){
setState(() {
print(value);
this.isSwitchPin = value;
});
});
}
}

You can try:
1, Define a field
ValueChanged<bool> onEnventChange;
2, On initState()
onEnventChange = (value){
setState(() {
print(value);
this.isSwitchPin = value;
});
});
3, Define your function
Widget getInputType(val){
if(val == 'Pin Lock'){
return CheckboxListTile(
title: Text('Pin Login'),
value: isSwitchPin,
onChanged: onEnventChange);
}
}

Related

How does the onPressed voidcallback change the boolean in my function?

Situation:
I have a checkbox in one place and i am sending the callback etc. up the widget tree to run a setState and run the function applyFilters().
The NeededChecked is also routed up to the checkbox-value.
Question:
What i am struggling to understand is why this works.
Specifically how the onPressed callback is able to set the value of the bool isNeededState to true/false?
Here is the code that is run. The only important part is the passing of the bool isNeededState to the neededCheked.
void neededFilterCalled(bool isNeededState) {
setState(() {
NeededChecked = isNeededState;
applyFilters();
});
}
And here is the checkbox widget:
Widget build(BuildContext context) {
return Checkbox(
value: isNeededChecked,
onChanged: neededFilterCalled,
);
}
Writing
onChanged: neededFilterCalled,
is shorthand for
onChanged: (value) => neededFilterCalled(value),
onChanged provide nullable bool, defined as
required void Function(bool?)? onChanged
You can accept null value and provide false on null case like
void neededFilterCalled(bool? isNeededState) {
setState(() {
isNeededChecked = isNeededState ?? false;
applyFilters();
});
}
return Checkbox(
value: isNeededChecked,
onChanged: neededFilterCalled,
);

How to access dropdownlist selected value in flutter and use it in another widget

This is my drop down list code
String dropdownValue = "a";
#override
Widget build(BuildContext context) {
return DropdownButton<String>(
value: dropdownValue
),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: <String>[
'a','b','c'
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
);
}
}
I want to use the selected value here
uploadDatatoFirebase() async {
*firebase connection code*
await FirebaseFirestore.instance
.collection(**selected value in drop down**) // the value from the box goes here
}
I can't seem to find a solution please help i want to create a database depending on the select box value any other techniques will also be welcomed
https://pub.dev/packages/get_it This is the package I use so I can can access a single instance of a class anywhere in the app, it is very popular and very well maintained. You could do this
setState(() {
dropdownValue = newValue!;
GetIt.I.get<ControllerWhatever>().selectedValueInDropDown = newValue!;
})
Just register the 'global controller instance' just as described in the get_it page and you will be able to get the value from any attribute you want anywhere in your app. It will make you life a lot easier

how to update selected radio button in flutter using getx

I am using Flutter with GetX plugin and I have three radio buttons inside statelesswidget but the radio button doesn't changed to selected when user click on it so my question is how I can update the selected radio using GetX plugin. here is my code
activeColor: mainColor1,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
// checkColor: Colors.white,
value: isSelected,
onChanged: (value) {
print('=====');
setState(() {
isSelected = value;
widget.onPress(value);
});
})
),
and here is my controller side
changeStatus(String select, bool condition) {
if (condition) {
selected.add(select);
} else {
selected.remove(select);
}
print(selected.length);
update();
}
you have to initialize your controller like this:
final Controller _controller = Get.put(Controller());
call your method like this in onChanged:
_controller.your method
& put the whole dropdown widget in:
Obx(()=>DropdownButton)

Flutter: Cursor Position Shift to left in TextField after SetState is called

I have a textField in which if there is no value then border color changes to red after the user put value I have called the setState method to change the border color to grey again.
but as soon as the user put a value and setState is called the cursor position shift to the beginning or left, I have tried many approaches to correct it but nothing works.
Code to my textField :-
TextField(
controller: textEditingController,
onChanged: (text) {
//print(textEditingController.selection);
if (text.length == 0) {
addbuttonDisable = true;
firstTime = true;
setState(() {
_validate = true;
currentIndexCheck = index;
});
}
if (text.length >= 1) {
addbuttonDisable = false;
}
if (text.length >= 1 && firstTime) { <<== this is the condition after which cursor shift to left.
setState(() {
_validate = false;
firstTime = false;
});
textEditingController.value.copyWith( <<== approach i have tried to change cursor positon.
selection: TextSelection.fromPosition(
TextPosition(offset: text.length+2)));
}
if (lstOfStrings.length > index) {
lstOfStrings.removeAt(index);
lstOfStrings.insert(index, text);
} else {
lstOfStrings.add(text);
}
},
decoration: InputDecoration(
//errorText: _validate? "value can't be null": null,
border: InputBorder.none,
contentPadding: EdgeInsets.only(left: 2, right: 2, bottom: 2.5),
isDense: true,
),
maxLines: 1,
),
if the condition which causes the problem is commented everything works. Please suggest me a solution or if I am doing something wrong here. What is want is that the cursor remains to end of user inputs words.
Since copyWith doest set the value of the object
You have to
textEditingController.value=textEditingController.value.copyWith( <<== approach i have tried to change cursor positon.
selection: TextSelection.fromPosition(
TextPosition(offset: text.length+2)));
The problem here is setState(). It repopulates the widget in memory. If your widget is loosely coupled with widget tree. It tends to re-build the widget. Hence, the cursor goes at the start.
So the best way is to store the state of TextField is by using GlobalKeys.
GlobalKey _orderFormKey = GlobalKey();
Widget someFunction(){
return TextField(
key: _orderFormKey,
....
}
If this doesn't work you could use
GlobalKey _orderFormKey = GlobalKey();
Widget someFunction(){
if(_orderFormKey.currentWidget!=null){
return _orderFormKey.currentWidget;
}else{
return TextField(
key: _orderFormKey,
....
}
Use this with precaution as it would not let setState() make any changes to its state through regular methods.

Flutter DropdownButtonFormField not showing selected item

Using DropdownButtonFormField element to show a lists of cities. It works fine on change event and show the selected item on change event. Issue is that once I try to show selected item from set state its not working. Although state is successfully set and working.
Here is my DropdownButtonFormField code:-
DropdownButtonFormField(
value: _city,
onChanged: (String newValue) {
setState(() {
_city = newValue;
});
},
// initialValue: 'Male',
items: ['Ajman','Al Ain','Dubai','Fujairah','Ras Al Khaimah', 'Sharjah', 'Ajman','Umm Al Quwain']
.map((cityTitle) => DropdownMenuItem(
value: cityTitle, child: Text("$cityTitle")))
.toList(),
)
I set _city in InitState
#override
void initState() {
setFilters();
super.initState();
}
setFilters(){
setState(() {
_city = "Dubai";
});
}
But it's not showing selected value. How can I fix that ?
Meanz when I want to set value of _city from initState its not working