Flutter - I have two issue with RadioListTile - flutter

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.

Related

Flutter firebase drowpdown items cannot display

I try to make a dropdown taking data from firestore, after selecting the items from DromdownMenuItem the selected new value cannot display please help..?
DropdownButtonHideUnderline(
child: DropdownButton<String>(
isDense: true,
//isExpanded: false,
value: projectName,
onChanged: (String? newValue) {
debugPrint('selected onchange: $newValue');
setState(
() {
debugPrint('make selected: $newValue');
projectName = newValue.toString();
state.didChange(newValue);
setDefaultMake = false;
///var setDefaultMakeModel = true;
controllerProjectName.text = newValue.toString();
},
);
},
items: snapshot.data!.docs.map((value) {
return DropdownMenuItem<String>(
value: value.get('project_name'),
child: Text('${value.get('project_name')}'),
);
}).toList(),
),
),

how to pass index inside Radio listTile in Flutter?

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

how to make today's date default value as dropdown value in flutter?

My dropDown working fine, instead of Today as a string in the dropDown initial value I want Today's date-time using the property DateTime.now()
String dropdownvalue = 'Today';
var items = [
'Today',
'Choose from calendar',
];
My dropdown:
DropdownButton(
underline: Container(),
isExpanded: true,
value: dropdownvalue,
icon: const Icon(
Icons.keyboard_arrow_down,
color: Color(0xffB50000),
),
items: items.map((String items) {
return DropdownMenuItem(
value: items,
child: Text(
items,
style: TextStyle(
color: Color(0xffB50000),
fontWeight: FontWeight.w400),
),
);
}).toList(),
onChanged: (String? newValue) {
setState(() {
dropdownvalue = newValue!;
});
if(dropdownvalue =='Choose from calendar'){
setState(() {
_selectedDate(context);
});
}
if(dropdownvalue =='Today'){
setState(() {
String today = DateFormat('dd-MM-yyyy').format(currentDateTime);
print("today");
print(today);
final storeProvider = Provider.of<StorageProvider>(context, listen: false);
storeProvider.updateAppointmentDate(today);
final slotBookingProvider = Provider.of<SlotBookingProvider>(context, listen: false);
slotBookingProvider.checkAvailableSlot(date: today, context: context);
});
}
},
),
You can
You can replace replacing Today with DateTime.now().toString()
String? dropdownvalue;
var items = [
DateTime.now().toString(),
'Choose from calendar',
];

Cannot Select Checkbox List Tile Flutter

I've created a map of values to make checkboxes. The map consists of String and bools and when the value of the bool is changed, the check box value should change.
EDIT 1:
My ListView Checkbox
CheckboxListTile(
title: new Text(
key,
style: textHeaderStyle,
),
value: _selectedOrders.contains(undeliveryOrders[key]),
activeColor: Colors.pink,
checkColor: Colors.white,
onChanged: (bool value) {
setState(() {
if(value){
_selectedOrders.add(undeliveryOrders[key]);
undeliveryOrders[key] = value;
}else{
setState(() {
_selectedOrders.remove(undeliveryOrders[key]);
});
}
});
},
)
Creating the Map:
void _formatOrders(availableOrders) {
for (int i = 0; i < availableOrders.length; i++) {
var tempOrder = '${availableOrders[i].customer.uniqueInfo.name} , ${availableOrders[i].address}';
undeliveryOrders['$tempOrder'] = false;
}
print('$undeliveryOrders');
print('$numbers');
}
Selected Order Method
var _selectedOrders = [];
getItems() {
undeliveryOrders.forEach((key, value) {
if (value == false) {
_selectedOrders.add(key);
}
});
print(_selectedOrders);
_selectedOrders.clear();
}
I think you might be over complicating things every value does not have to be mapped to a boolean the way I do it is I add the value that gets checked to an array then check if that item is in that array if it is its true if not its false. You just have to remember to remove the item if the checkbox is unchecked here is some sample code for you.
List<String> items = ['Item 1', 'Item 2', 'Item 3'];
List<String> isChecked = [];
//Initialized outside build
ListView(
children: <Widget>[
...items
.map(
(item) => CheckboxListTile(
subtitle: Text('This is a subtitle'),
secondary: Text('This is Secondary text'),
title: Text(item),
value: isChecked.contains(item),
onChanged: (bool value) {
if (value) {
setState(() {
isChecked.add(item);
});
} else {
setState(() {
isChecked.remove(item);
});
}
},
),
)
.toList()
],
),
In my case, the setState() is not responding, try to use StatefulBuilder().
For Example:
...
bool isSelected = false;
StatefulBuilder(
builder: (context, _setState) {
return ListView(
children: [
CheckboxListTile(
value: isSelected,
onChanged: (bool? value) {
_setState(() => isSelected = value);
},
),
],
);
},
);
...
You can try with this code bellow ?
onChanged: (bool value) {
setState(() {
undeliveryOrders[key] = value ?? false;
});
},

Radio buttons isn't changed

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;
});
}