RadioButton is only set to one of them and not changing
Radio<int>(activeColor: Colors.orange,value: 0, groupValue: 1,onChanged:
HandleRadio)
,Radio<int>(activeColor: Colors.amber,value: 1, groupValue: 1, onChanged:
HandleRadio)
,Radio<int>(activeColor: Colors.blue, value: 2, groupValue: 1, onChanged:
HandleRadio)
//function
int RadioValue = 0;
void HandleRadio(int value){
setState(() {
RadioValue = value;
});
}
It should set to radio selected
That's because you were giving hard coded value as groupValue to your Radio
Here is the working code.
Radio<int>(
activeColor: Colors.orange,
value: 0,
groupValue: radioValue, // changed this
onChanged: handleRadio,
),
Radio<int>(
activeColor: Colors.amber,
value: 1,
groupValue: radioValue, // changed this
onChanged: handleRadio,
),
Radio<int>(
activeColor: Colors.blue,
value: 2,
groupValue: radioValue, // changed this
onChanged: handleRadio,
),
//function
int radioValue = 0;
void handleRadio(int value) {
setState(() {
radioValue = value;
});
}
Related
I am trying to pass the index inside the Radio button list tile in order to set Locale language and also save settings into shared preferences.
This is a the part of main code where i want to pass the index:
body: CupertinoPageScaffold(
child: Container(
child: SingleChildScrollView(
child: Container(
child: CupertinoFormSection.insetGrouped(
header: Text('choose_a_language'.tr()),
children: [
RadioListTile( title: Text('English'), value: Language.English, groupValue: _selecLangIndex, subtitle: Text('English'), selected: _selectedIndex == 0 , onChanged: (newValue) =>
setState(() => _selecIndex = newValue)),
RadioListTile(title: Text('French'), value: Language.French, groupValue: _selecLangIndex, subtitle: Text('French'), selected: _selectedIndex == 1, onChanged: (newValue) =>
setState(() => _selecIndex = newValue)),
RadioListTile(title: Text('Italian'), value: Language.Italian, groupValue: _selecLangIndex, subtitle: Text('Italian'), selected: _selectedIndex == 2, onChanged: (newValue) =>
setState(() => _selecIndex = newValue)),
TextButton(onPressed:
_saveSettings,
child: Text('save'.tr().toUpperCase(),
style: TextStyle(fontWeight: FontWeight.bold),),
Right now on Save button, with onPressed method, i am able to save settings, but i cannot set locale language.
I also tried to implement this code, without any result.
void setLocale() {
int index = 0;
setState(() => _selectedIndex == index );
if (index == 0){
context.setLocale(Locale('en','US'));
}
else if (index == 1){
context.setLocale(Locale('fr','FR'));
}
else if (index == 2){
context.setLocale(Locale('it','IT'));
}
print(index);
}
You can do something like this
onChanged: (newValue) {
setLocale(); //
_selecIndex = newValue ;
setState((){});
},
If your setLocale is not using/updating the _selectedIndex. You can pass index on setLocale(0) and it doesnt need to have setState because we already call setState end of onChanged
I want to increment the value of a var for when the slider is incrementing and decrement that value when the slider is decrementing, I did the first part but I'm stuck on the second one can anyone point out what I should do?
My slider code:
child: Slider(
value: sliderValue,
min: min,
max: activityMax,
divisions: 4,
label: '${sliderValue.round().toString()}',
mouseCursor: MouseCursor.uncontrolled,
autofocus: true,
onChanged: (value) {
setState(
() {
sliderValue = value;
_amountController.text =
sliderValue.toInt().toString();
sizeHeight += 250;
},
);
},
),
Try this:
Slider(
value: sliderValue,
min: min,
max: activityMax,
divisions: 4,
label: '${sliderValue.round().toString()}',
mouseCursor: MouseCursor.uncontrolled,
autofocus: true,
onChanged: (value) {
if(value < sliderValue){
executeWhenDecrementing();
}
setState(
() {
sliderValue = value;
_amountController.text = sliderValue.toInt().toString();
});
},
)
void executeWhenDecrementing(){
//Do something
}
I am using CheckboxListTile in for check box. It works good if selected : false. If selected is true then unable to un check it.
final checkBox = CheckboxListTile(
contentPadding: EdgeInsets.all(0),
title: Text("Remember Me", style: TextStyle(fontSize: 15.0)),
selected: checkBoxState,
value: checkBoxState,
checkColor: Colors.white,
activeColor: Colors.grey,
controlAffinity: ListTileControlAffinity.leading,
onChanged: (newValue) {
setState(() {
checkBoxState = newValue;
});
},
);
This is the code what I am using.
Thanks in Advance,
I don't know how you are using checkBox, but this code works perfectly fine.
class _MyWidgetState extends State<MyWidget> {
bool checkBoxState = true;
#override
Widget build(BuildContext context) {
return CheckboxListTile(
contentPadding: EdgeInsets.all(0),
title: Text("Remember Me", style: TextStyle(fontSize: 15.0)),
selected: checkBoxState,
value: checkBoxState,
checkColor: Colors.white,
activeColor: Colors.grey,
controlAffinity: ListTileControlAffinity.leading,
onChanged: (newValue) {
setState(() {
checkBoxState = newValue;
});
},
);
}
}
You should use like below. You need to set bool true false reversing inside setState like checkBoxState = !checkBoxState;. And declare bool globally. bool checkBoxState = false;
bool checkBoxState = false;
final checkBox = CheckboxListTile(
contentPadding: EdgeInsets.all(0),
title: Text("Remember Me", style: TextStyle(fontSize: 15.0)),
selected: checkBoxState,
value: checkBoxState,
checkColor: Colors.white,
activeColor: Colors.grey,
controlAffinity: ListTileControlAffinity.leading,
onChanged: (newValue) {
setState(() {
checkBoxState = !checkBoxState;
});
},
)
I created my dropdown button, but whenever an item is selected does not update value.But whenever I hotreload all my values ββare updated. Can anyone guess the reason? or is there a solution?
DropdownButton<String>(
value: dropdownValue,
icon: Icon(Icons.keyboard_arrow_down),
iconSize: 15,
elevation: 10,
style: TextStyle(color: Colors.grey),
onChanged: (newValue) {
setState(() async {
dropdownValue = newValue;
final selectedLanguage= await SharedPreferences.getInstance();
selectedLanguage.setString('selectedLanguage', dropdownValue);
});
allTranslations.setNewLanguage(
allTranslations.getLocaleKey(
dropdownValue,
),
);
},
items: <String>[
englishText,
chineseText,
russianText,
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
#kimSoo great that it worked!
Solution for everyone is the async closure in setstate wont work. We have to remove it and initialize Shared Preferences earlier.
Initialize final selectedLanguage= await SharedPreferences.getInstance(); earlier
DropdownButton<String>(
value: dropdownValue,
icon: Icon(Icons.keyboard_arrow_down),
iconSize: 15,
elevation: 10,
style: TextStyle(color: Colors.grey),
onChanged: (newValue) {
setState(() { <----- removed async
dropdownValue = newValue;
selectedLanguage.setString('selectedLanguage', dropdownValue);
});
allTranslations.setNewLanguage(
allTranslations.getLocaleKey(
dropdownValue,
),
);
},
items: <String>[
englishText,
chineseText,
russianText,
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
I have a problem with RadioListTile it doesn't work when I make the change that does not change unless I leave it and return to it I find it changed.
The second problem is I want to save the option when the application is run for the first time, it is the first choice.
if someone has answered, please?
this is my code:
int radioValue = 0;
int tafIbnkatheer = 1;
int tafBaghawy = 2;
int tafQurtubi = 3;
int tafSaadi = 4;
int tafTabari = 5;
var showTaf;
void handleRadioValueChanged(int val) {
TranslateRepository2 translateRepository2 = new TranslateRepository2();
TranslateRepository translateRepository = new TranslateRepository();
TranslateRepository3 translateRepository3 = new TranslateRepository3();
TranslateRepository4 translateRepository4 = new TranslateRepository4();
TranslateRepository5 translateRepository5 = new TranslateRepository5();
setState(() {
radioValue = val;
switch (PrefService.getInt('$radioValue')) {
case 1:
setState(() {
showTaf = translateRepository2;
});
break;
case 2:
setState(() {
showTaf = translateRepository;
});
break;
case 3:
setState(() {
showTaf = translateRepository3;
});
break;
case 4:
setState(() {
showTaf = translateRepository4;
});
break;
case 5:
setState(() {
showTaf = translateRepository5;
});
break;
}
print(showTaf);
});
}
#override
void initState() {
setState(() {
radioValue = 1;
});
super.initState();
}
...
RadioListTile(
value: tafIbnkatheer,
groupValue: radioValue,
title: Text("Radio 1"),
subtitle: Text("Radio 1 Subtitle"),
onChanged: (val) {
setState(() {
handleRadioValueChanged(val);
});
},
activeColor: Colors.red,
selected: true,
),
RadioListTile(
value: tafBaghawy,
groupValue: radioValue,
title: Text("Radio 2"),
subtitle: Text("Radio 2 Subtitle"),
onChanged: (val) {
setState(() {
handleRadioValueChanged(val);
});
},
activeColor: Colors.red,
selected: false,
),
RadioListTile(
value: tafQurtubi,
groupValue: radioValue,
title: Text("Radio 3"),
subtitle: Text("Radio 3 Subtitle"),
onChanged: (val) {
setState(() {
handleRadioValueChanged(val);
});
},
activeColor: Colors.red,
selected: false,
),
RadioListTile(
value: tafSaadi,
groupValue: radioValue,
title: Text("Radio 4"),
subtitle: Text("Radio 4 Subtitle"),
onChanged: (val) {
setState(() {
handleRadioValueChanged(val);
});
},
activeColor: Colors.red,
selected: false,
),
RadioListTile(
value: tafTabari,
groupValue: radioValue,
title: Text("Radio 5"),
subtitle: Text("Radio 5 Subtitle"),
onChanged: (val) {
setState(() {
handleRadioValueChanged(val);
});
},
activeColor: Colors.red,
selected: false,
),
...
To make a Radio List Tile selected, as the documentation says, you can use checked. In fact
checked β bool
Whether this radio button is checked
selected β bool
Whether to render icons and text in the activeColor.