How to Set Default Value by Condition in DropdownButtonFormField in Flutter? - flutter

I Have DropdownButtonFormField as Below.
CaseReasonModel? dropdownValueCaseReason;
List<DropdownMenuItem<CaseReasonModel>> items = [];
dropDownModels.forEach((element) {
items.add(DropdownMenuItem(
child: Text(
element.reasonName!,
style: Themes.getTextStyle(context),
),
value: element,
));
});
DropdownButtonFormField<CaseReasonModel>(
style: Themes.getTextFieldTextStyle(context),
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText:
AppLocalizations.of(context).hintSelectCaseReason,
),
value: dropdownValueCaseReason,
isExpanded: true,
items: items,
onChanged: (value) {
setState(() {
dropdownValueCaseReason = value;
});
},
),
And CaseReasionModel Class as below.
class CaseReasonModel {
String? reasonName;
String? reasonId;
String? reasonStatus;
static CaseReasonModel? fromHashMap(dynamic map) {
if (map == null) return null;
CaseReasonModel result = new CaseReasonModel();
result.reasonName = map["name"];
result.reasonId = map["reasonId"].toString();
result.reasonStatus = map["status"];
return result;
}
static List<CaseReasonModel> fromArrayOfHashMap(dynamic jsonArray) {
List<CaseReasonModel> list = [];
if (jsonArray != null) {
for (var jsonObject in jsonArray) {
list.add(fromHashMap(jsonObject)!);
}
}
return list;
}
}
I got all Reason in Drop-down, So here i have to set default selected value based on reason id which i specified. Suppose i have to set value which id = 2,
so how can I do this ?.

Related

How can I get the id of a selected item in a list using the the dropdownSearch widget in flutter?

I am using the DropDownSearch widget in flutter to display a list of items. I am displaying the value "acYear" from the list but I want to be able to pass the value "id" of the selected item to the controller i.e academicYearController.text .How can I achieve this please? This is what I have tried:
child: DropdownSearch < dynamic > (
autoValidateMode: AutovalidateMode.onUserInteraction,
validator: (value) =>
value == null ? "Academic Year is required " : null,
dropdownDecoratorProps: const DropDownDecoratorProps(
dropdownSearchDecoration: InputDecoration(
hintText: "Select academic year",
helperText: '',
labelText: "Academic Year *",
contentPadding: EdgeInsets.fromLTRB(12, 12, 0, 0),
border: OutlineInputBorder(),
),
),
items: dataService.academicYearList.map((AcademicYear yr) {
return yr.accYear.toString();
}).toList(),
// items: dataService.academicYearList.map((AcademicYear yr) {
// return DropdownMenuItem(
// child: Text(yr.id.toString()),
// value: yr.id,
// );
// }).toList(),
onChanged: (value) {
academicYearController.text = value;
setState(() {
dataService.selectedAcademicYear = dataService.selected;
});
debugPrint('selectedId: ${academicYearController.text}');
},
),
And the AcademicYear Object and academicYear List look like this:
class AcademicYear {
int id;
String accYearId;
String accYear;
int status;
AcademicYear({
required this.id,
required this.accYearId,
required this.accYear,
required this.status,
});
[{
"id": 1,
"accYearId": "20212022",
"accYear": "20212022",
"status": 1
}]
I have Added the API call to get the academicYear data just incase am doing something wrong as shown:
Future < List < AcademicYear >> getYears() async {
academicUrl = "${dataService.baseUrl}/academicyear";
// debugPrint('url: $academicUrl');
http.Response response = await http.get(
Uri.parse(academicUrl),
headers: {
"Content-Type": "application/json",
},
);
List < AcademicYear > result(dynamic str) => List < AcademicYear > .from(
json.decode(str).map((x) => AcademicYear.fromJson(x)));
// debugPrint('result: ${response.body}');
if (response.statusCode == 200) {
return result(response.body);
} else {
throw Exception('error loading object');
}
}
In your DropdownSearch's onChanged , do this:
onChanged: (value) {
var xList = dataService.academicYearList.where((element) => element.accYear == value);
if (xList.isNotEmpty) {
AcademicYear x = xList.first;
print("x = ${x.id}");
academicYearController.text = value;
}
setState(() {
dataService.selectedAcademicYear = dataService.selected;
});
debugPrint('selectedId: ${academicYearController.text}');
},

how can get radio value in Flutter?

how can get radio value in Flutter?
I writing this code and tring to get value, but has a little problems.
Code
enum SingingCharacter { Australia, USA }
SingingCharacter? _character = SingingCharacter.Australia;
String res = await AuthMethods().signUpUser(
email: _emailController.text,
password: _passwordController.text,
username: _usernameController.text,
bio: _bioController.text,
file: _image!,
country: _character.toString(),
);
Column(
children: <Widget>[
RadioListTile<SingingCharacter>(
title: const Text('Australia'),
value: SingingCharacter.Australia,
groupValue: _character,
onChanged: (SingingCharacter? value) {
setState(() {
_character = value;
});
},
),
RadioListTile<SingingCharacter>(
title: const Text('USA'),
value: SingingCharacter.USA,
groupValue: _character,
onChanged: (SingingCharacter? value) {
setState(() {
_character = value;
});
},
),
],
),
When the use register and choose the country, I will got this value SingingCharacter.Australia and writing firebase, but I hope I could got like this value Australia or USA, not got SingingCharacter.Australia or SingingCharacter.USA
Where is my falut code?
You have given the RadioListTile<T> where T as SingingCharacter. So you will get the value as SingingCharacter.USA or SingingCharacter.Australia. If you want only the name, try like below.
onChanged: (SingingCharacter? value) {
setState(() {
if (value != null) {
_character = value;
String enumValue = value.name; // USA
// or
enumValue = describeEnum(value); // USA
}
});
}
https://api.flutter.dev/flutter/foundation/describeEnum.html
For a flexible solution, perhaps you can leverage extension, something like this:
enum SingingCharacter { Australia, USA }
extension SingingCharacterExt on SingingCharacter {
String get code {
switch (this) {
case SingingCharacter.Australia:
return "AU";
case SingingCharacter.USA:
return "US";
}
}
}
Usage:
debugPrint(SingingCharacter.Australia.code);

How to use DropDownButton for dynamic list in flutter?

I am trying to implement dynamic dropdownButton in my app where the items of dropdown is going to come from the names of columns in my excel sheet. I am able to show all the columns of excel but I couldn't able to trace the index of column which the user is selecting from the dropdown.
I tried to make a map of dropdownitems like this in which the key is the index and value is the DropdownMenuItem like this :
late int selectedIndex; //where I want to store the selected index
late String initialDropDownVal;
List<Map<int,DropdownMenuItem<String>>> dropdownItems = [];
Then I added some values by iterating the columns of excel using a for loop :
excel = Excel.decodeBytes(bytes);
sheet = excel['Sheet1'];
for(int i = 1; i< sheet.maxCols; ++i){
var cell = sheet.cell(CellIndex.indexByColumnRow(rowIndex: 0, columnIndex: i));
String val = cell.value.toString();
if(val=="null"){
break;
}else{
if(i==1){
initialDropDownVal = val;
}
var newItem = DropdownMenuItem(
child: Text(val),
value: val,
);
dropdownItems.add({i:newItem});
}
}
But I could not able to map the values in items attribute of DropdownButton, I tried to implement like this but this is throwing error
DropdownButton(
value: selectedVal,
icon: const Icon(Icons.keyboard_arrow_down),
items: dropdownItems.map((int i,DropdownMenuItem<String> p) => p).toList(),
onChanged: (String? value){
setState(() {
initialDropDownVal = value!;
});
})
And I am not sure how to change set the selectedIndex in onChanged function. Please help me in this. Thanks
late int selectedIndex; //where I want to store the selected index
late String initialDropDownVal;
List<DropdownMenuItem<String>> dropdownItems = [];
excel = Excel.decodeBytes(bytes);
sheet = excel['Sheet1'];
for(int i = 1; i< sheet.maxCols; ++i){
var cell = sheet.cell(CellIndex.indexByColumnRow(rowIndex: 0, columnIndex: i));
String val = cell.value.toString();
if(val=="null"){
break;
}else{
if(i==1){
initialDropDownVal = val;
}
var newItem = DropdownMenuItem(
child: Text(val),
value: val,
);
dropdownItems.add(newItem);
}
}
DropdownButton(
value: selectedVal,
icon: const Icon(Icons.keyboard_arrow_down),
items: dropdownItems,
onChanged: (String? value){
setState(() {
initialDropDownVal = value!;
selectedIndex=dropdownItems.indexWhere((i)=>i.value==value);
});
})
late int selectedIndex; //where I want to store the selected index
late String initialDropDownVal;
List<Map<String,int>> dropdownItems = [];
excel = Excel.decodeBytes(bytes);
sheet = excel['Sheet1'];
for(int i = 1; i< sheet.maxCols; ++i){
var cell = sheet.cell(CellIndex.indexByColumnRow(rowIndex: 0, columnIndex: i));
String val = cell.value.toString();
if(val=="null"){
break;
}else{
if(i==1){
initialDropDownVal = val;
}
dropdownItems.add({val:i});
}
}
DropdownButton(
value: selectedVal,
icon: const Icon(Icons.keyboard_arrow_down),
items: dropdownItems.map((e) {
return DropdownMenuItem(
child: Text(e.keys.first),
value: e.keys.first,
);
}).toList(),
onChanged: (String? value){
setState(() {
initialDropDownVal = value!;
for(int i = 0; i< dropdownItems.length; ++i){
if(dropdownItems[i].keys.first==value){
setState(() {
selectedIndex = dropdownItems[i].values.first;
});
break;
}
}
});
}),
)

How to reset second drop down list if i change first drop down value?

I have two drop down which second is depends on first.
When i changing the first drop down value after selecting second dropdown value it throws an error as below.
So how to reset second drop down list if i changing first drop down value?
Drop down buttons are as below.
DropdownButtonFormField<Standard>(
validator: (value) {
if (value == null) {
return "Select Standard";
}
},
isExpanded: true,
hint: Text('Select Standard'),
value: selectedStandard,
items: _standards.map((Standard standard) {
return DropdownMenuItem<Standard>(
value: standard,
child: Text(standard.standardName),
);
}).toList(),
onChanged: (val) {
setState(() {
selectedStandard = val;
standardId = val?.standardId;
onMediumChange(val);
});
}),
SizedBox(height: 20),
DropdownButtonFormField<Medium>(
validator: (value) {
if (value == null) {
return "Select Medium";
}
},
isExpanded: true,
hint: Text('Select Medium'),
value: selectedMedium,
items: _mediums.map((Medium medium) {
return DropdownMenuItem<Medium>(
value: medium,
child: Text(medium.mediumName),
);
}).toList(),
onChanged: (val) {
setState(() {
selectedMedium = val;
mediumId = val?.mediumId;
});
}),
And get Values code is as below. Varibles which i used.
var _standards = <Standard>[];
var _mediums = <Medium>[];
Standard? selectedStandard;
Medium? selectedMedium;
#override
void initState() {
super.initState();
ApiManager().getStandards().then((standards) {
setState(() {
_standards = standards;
});
});
}
void onMediumChange(standard) {
setState(() {
selectedStandard = standard;
_mediums = [];
});
String mediumUrl =
"$baseUrl/medium/get_by_course_id?standard_id=${selectedStandard?.standardId}";
ApiManager().getMediums(mediumUrl).then((List<Medium> value) => {
setState(() {
_mediums = value;
})
});
}
As I understand there is a problem with selectedMedium
Try change Make it selectedMedium null inside onMediumChange
void onMediumChange(standard) {
setState(() {
selectedMedium = null;
selectedStandard = standard;
_mediums = [];
});
String mediumUrl =
"$baseUrl/medium/get_by_course_id?standard_id=${selectedStandard?.standardId}";
ApiManager().getMediums(mediumUrl).then((List<Medium> value) => {
setState(() {
_mediums = value;
})
});
}
first, check the values in _mediums because you must have a different value of selectedMedium here as according to this error !!!

Other fields gets updated to blank as I call set State, Is there a way by which TextFormFields won't become Blank as i call set state

I know these fields are getting updated to blank because i am calling the set state method, but if i don't call the set state method in my toggle button the values won't change.
So is there a way by which i can use my toggle button and the TextFormFields won't get updated to blank
This is where the values are
class _AddPostState extends State<AddPost> {
List<bool> _typeSelection = [false, false, false];
String title = "";
String type = "";
This is the field which gets updated as i press the Toggle Button
TextFormField(
decoration: InputDecoration(
hintText: "Enter Title",
labelText: "Title",
),
validator: (value) {
if (value.isEmpty) {
return 'Enter an Title';
} else {
return null;
}
},
onChanged: (value) {
title = value;
},
),
This is the button
ToggleButtons(
onPressed: (int newIndex) {
if (newIndex == 0) {
type = "Dog";
print(type);
}
if (newIndex == 1) {
type = "Cat";
print(type);
}
if (newIndex == 2) {
type = "Bird";
print(type);
}
setState(() {
for (int index = 0;
index < _typeSelection.length;
index++) {
if (index == newIndex) {
_typeSelection[index] = true;
} else {
_typeSelection[index] = false;
}
}
});