how to update selected radio button in flutter using getx - flutter

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)

Related

flutter ListView.builder make onPressed works for only the concern item

I have a list of cards inside a ListView.builder and each card has a favorite IconButton which change it's color when clicked, but whenever i click on it all the favorite icon change their color too, i wanted to work on the concern item.
Thanks.
bool isPressed = false;
.
.
.
onPressed: () {
setState(() {
isPressed = true;
});
}
In your item you add a field isFavorite as bool type. You change the value of isFavorite and handle color based on isFavorite.
onPressed: (value) {
setState(() {
productItem[index].isFavorite = value;
});
}
For the color part you will check:
color: productItem[index].isFavorite?Colors.pinkAccent: Colors.grey,

How stop SetState flutter

I work on flutter project . when i click to modify icon to edit name for example ==> the screen is roaleded automatically . How i can stop refresh screen after click on edit button ?
this piece of my Form code :
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('Adresse email :',
style: TextStyle(
color: Color(0xFF4053FCF),
fontSize: 16,
fontWeight: FontWeight.w600
),
),
IconButton(
icon: Icon(CommunityMaterialIcons.pencil,
color: Colors.grey,
),
onPressed: () {
emailNode.requestFocus();
setState(() {
enableemail = true;
});
})
],
),
void editUserProfile() async {
setState(() {});
// if (_formKey.currentState.validate()) {
String name = _nameController.text;
String email = _emailController.text;
String adress = _adressController.text;
userApi.editUserProfile(name, email, adress).then((data) {
print(data);
if (data != null) {
// Navigator.pop(context);
/* Navigator.push(
context, MaterialPageRoute(builder: (context) => Profile()));*/
}
// setState(() {});
/* Navigator.push(
context, MaterialPageRoute(builder: (context) => BoxSettings()));*/
setState(() {
enableup = false;
enableadress = false;
enableemail = false;
});
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(data)));
// ScaffoldMessenger.of(context).showSnackBar(snackBar3);
}).catchError((error) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text(error.toString())));
});
setState(() {});
}
and this my screen for more information :
How i can press on edit button without reload screen ?
There some workarounds to achieve this (i.e. update the state of one widget after tapping a completely different widget) like passing the callback function as a parameter etc.
But The best and neat solution here which will solve the above problem and keep your code neat is using Provider pattern.
If you are not aware of how a Provider pattern works, you can easily google search for articles regarding it. Here is one of them :
https://www.raywenderlich.com/6373413-state-management-with-provider
Read the above article before moving below.
Basically what we do is :
Create a ChangeNotifier class.
Wrap the parent of both widgets by a ChangeNotifierProvider widget.
Wrap the widget you want to update with Consumer widget.
Then in your onTap/onPressed function of Edit button you can call a function which will call the notifyListener() function. What this will do is it will notify the above ChangeNotifierProvider widget that some change has neen occured in it's widget tree. Then it will traverse the child whole widget tree below and will update the widget wrapped with Consumer widget.
So this way, you wont need to refresh your whole screen and you can easily update one widget by doing some action on a competely different widget.
Wrap the widgets you want to refresh inside stateful builder and make the whole screen a stateless widget and then call stateful builder

Flutter : Update all items in a ListView

I'm trying to create a simpler contextual action bar(CAB) https://pub.dev/packages/contextualactionbar
What I want to do is to update all the items in a ListView in Flutter.
For example, I want to display a trailing checkbox for each ListTile when I long press on an item. The item can be a stateful widget.
Here is a gif of what I want to do:
I tried to use a GlobalKey example from https://stackoverflow.com/a/57310380/5712419
Using a GlobalKey, only the visible items would update.
Update 28/04/2021: I answered this question with something that worked for me: the Provider package. I think it is cleaner than using a GlobalKey.
It's possible to select all items in a ListView by using Provider. https://pub.dev/packages/provider
These are the steps I took.
Step 1: Create a class that extends ChangeNotifier for a single item
class SelectableItemState extends ChangeNotifier {
bool _selectable = false;
bool get selectable => _selectable;
set selectable(bool value) {
_selectable = value;
notifyListeners();
}
}
Step 2: Register the Change Notifier
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => SelectableItemState()),
], //...
Step 3: Create the Consumer, and update the state from the gesture detector
return Consumer<SelectableItemState>(builder: (context, state, child) {
return GestureDetector(){
onLongPress: () {
state.selectable = true;
},
child: Material( // use state.selectable in the item widget...
}
}
Step 4: Update the trailing of your item (if selectable, show checkbox, else, show an empty container)
trailing: (state.selectable)
? Checkbox(
value: // state.selectedItem,
onChanged: (value) {
setState(() {
// Here we can use something like
// state.setSelectedItem(item, value);
});
},
)
: Container(),

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

How to update Inner Widget in 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);
}
}