How can I save radiobutton group data in list? - flutter

I am making one quiz app. I have 10 questions list and each have four option. User select answer one by one. Then all answers save and give result in last screen.
//This is a code of radiobuttongroup
child: RadioButtonGroup(
padding: EdgeInsets.all(20.0),
labels: <String>[
que_list[index]['option_list'][0],
que_list[index]['option_list'][1],
que_list[index]['option_list'][2],
que_list[index]['option_list'][3]
],
labelStyle: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Colors.white),
onChange: (String label, int index) {},
onSelected: (String selected) async {
setState(() {
_picked = selected;
});
//print(selected);
print(">>>>>>>>>" + _picked);
},
picked: _picked,
activeColor: Colors.white,
},
),
I am getting data from API.

Make another list of answers entered like this -
List<String> selectedAns = [];
After selecting, keep adding the answers to this list-
selectedAns.add(selected);

Related

Obscure text bool value is not updating in flutter

I am using pin_code_text_field package to create pin code textfields. But when I was updating the bool value used to obscure text. Bool value is not updating in pin code fields until I click on textfields.
Code was added below:
bool pinWasObscured = true;
Row(
children: [
PinCodeTextField(
maxLength: 4,
hideCharacter: pinWasObscured,
highlight: true,
highlightAnimation: true,
highlightAnimationBeginColor: Colors.black,
highlightAnimationEndColor: Colors.white,
highlightAnimationDuration: Duration(seconds: 5),
highlightColor: Color(0xFFF37021),
pinBoxDecoration: ProvidedPinBoxDecoration.underlinedPinBoxDecoration,
maskCharacter: "*",
pinTextStyle: TextStyle(
fontSize: 15.sp,
fontWeight: FontWeight.bold,
),
pinBoxWidth: SizeConfig.blockSizeHorizontal! * 12,
pinBoxHeight: SizeConfig.blockSizeHorizontal! * 10,
autofocus: false,
controller: pinController,
defaultBorderColor: Colors.black26,
),
SizedBox(width: 10),
IconButton(
icon: pinWasObscured
? Icon(Icons.visibility_off_outlined)
: Icon(Icons.visibility_outlined),
onPressed: () {
setState(() {
pinWasObscured = !pinWasObscured;
});
},
),
],
),
the issue is that you are also updating the value of the "hideCharacter" on the click of item. you dont have to update the hideCharacter value everytime.
instead you have to set the value to true if you want to hide the character and false to show.

how to get selections from multi_select_flutter?

has to be something simple I'm not getting but have been struggling on this for way too long. If anybody can help me out I would be appreciative. I believe it has to do with some kind of type issues as when I make selections I have a print statement that then prints out what I'm getting back:
[Instance of 'MultiSelectItem', Instance of 'Analysis', Instance of 'Analysis']
Not sure how to convert the above to a List of Analysis and not able to carry out per the example for the package:
MultiSelectDialogField(
items: _animals.map((e) => MultiSelectItem(e, e.name)).toList(),
listType: MultiSelectListType.CHIP,
onConfirm: (values) {
_selectedAnimals = values;
},
),
where I also have to use a 'cast' in my code. My attempt:
MultiSelectDialogField(
items: _analyses.map((analysis) => MultiSelectItem<Analysis>(analysis, analysis.name)).toList(),
initialValue: outwardsData.outwards[widget.index].lossFeed
.map((analysis) => MultiSelectItem<Analysis>(analysis, analysis.name))
.toList(),
title: Text(
"Loss Feeds:",
style: TextStyle(color: Colors.black),
),
selectedColor: kConvexGreen,
unselectedColor: kConvexLightGreen,
backgroundColor: kConvexGreen,
decoration: BoxDecoration(
color: kConvexGreen.withOpacity(0.1),
borderRadius: BorderRadius.all(Radius.circular(5)),
border: Border.all(
color: kConvexGreen,
width: 2,
),
),
confirmText: Text(
'Submit',
style: TextStyle(color: Colors.white),
),
cancelText: Text(
'Cancel',
style: TextStyle(color: Colors.white),
),
searchable: true,
buttonText: Text("Loss Feeds"),
onSelectionChanged: (results) {
**print(results)**;
},
onConfirm: (results) {
// this is different than what example shows; without cast it errors
// "A value of type 'List<Object?>' can't be assigned to a variable of type 'List<Analysis>'". (Documentation)
_selectedAnalyses = results.cast();
},
chipDisplay: MultiSelectChipDisplay(
chipColor: kConvexGreen,
textStyle: TextStyle(color: Colors.white),
onTap: (value) {
setState(() {
_selectedAnalyses.remove(value);
// crashes here even though both are of same type List<Analyses> (theoretically)
// this is just here for testing purposes as am through many iterations trying to get
// data into lossFeed.
outwardsData.outwards[widget.index].lossFeed =
_selectedAnalyses;
});
},
),
),
I'm sure a more experienced flutter person will understand this (maybe) but I found that I couldn't just have the initial values come from the same list type used for the item (List< Analysis> _analyses) but I actually had to map it to use the same items data (_analyses).
initialValue: outwardsData.outwards[widget.index].lossFeed.map((analysis) => _analyses[analysis.id - 1]).toList()
Had other problems with this widget such as I found I couldn't map the results from onConfirm directly to List but instead had to do the following:
_rData.lossFeed = results.toList().cast<Analysis>()
as the data came back as List< dynamic>.
Couldn't find great examples online and trust experience will be the cure-all at some point.

Flutter: How to retrieve the data from firebase and show in the dropdown field

I am developing a Flutter application where I need to show the category field as a dropdown. I have added the category field as a dropdown in the form. I need to retrieve the data from Firebase for the category field and I have succeeded in getting the data but the problem is, it is not showing the entire data from the collection rather it is showing only one piece of data from the collection in the category dropdown field. How do I show the entire data from the collection? Please help me with your answer. Thanks!!
Here is the code for getting data from Firebase Collection
String _category;
void _onCategory() async {
_firestore.collection("categories").get().then((querySnapshot) {
querySnapshot.docs.forEach((result) {
print(result.data()['category']);
setState(() {
_category = result.data()['category'] ;
});
});
});
}
This is the code for showing the category data in the dropdown
String _dropDownValue;
Widget _categoryInput(){
return Padding(
padding: const EdgeInsets.only(top: 20.0),
child: DropdownButton(
hint: _dropDownValue == null
? Text('Category', style: TextStyle(color: Theme.of(context).primaryColor),)
: Text(
_dropDownValue,
style: TextStyle(color: Colors.white),
),
isExpanded: true,
iconSize: 30.0,
style: TextStyle(color: Colors.black),
items: ['$_category'].map(
(val) {
return DropdownMenuItem<String>(
value: val,
child: Text(val),
);
},
).toList(),
onChanged: (val) {
setState(
() {
_dropDownValue = val;
},
);
},
),
);
}

Variable value gets null outside setState() in Flutter

TextField(
style: TextStyle(
fontFamily: "Averta",
fontWeight: FontWeight.bold,
fontSize: 16,
color: Colors.black),
enableSuggestions: true,
onChanged: (value) {
setState(() {
final title = value;
print(title);
});
},
decoration: InputDecoration(
border: InputBorder.none,
contentPadding:
EdgeInsets.all(18),
hintText: "Enter Task Title",
hintStyle: TextStyle(
fontFamily: "Averta",
fontWeight: FontWeight.normal,
fontSize: 12,
color: Colors.grey.shade300),
),
),
Here,I have a title variable which I update with onChanged() function.
Here the print() function shows exact change in value which I do in TextField.
But I don't want to pass it here, I want to take some more data and then pass it when I click the CREATE button here-
InkWell(
onTap: () {
print(title);
setState(() {
DatabaseHelper _dbhelper =
DatabaseHelper();
Task _newTask = Task(
title: title,
year: year,
month: month,
day: daye,
hour: hour,
minute: minute,
weekday: weekday);
_dbhelper
.insertTask(_newTask);
});
}
Here the print statement shows "null",but I want to use the value which was updated above in TextField.
How can I achieve this in Flutter?
This line creates a new variable called title and sets it:
final title = value;
You want to access the already existing variable:
title = value;
That should work.

How to set a radio button inside a list of expansion tile checked by default in Flutter?

I have a list of expansion tiles and each of these tiles have 3 radio buttons each. I want to set some radio buttons checked by default when the page is loaded. How can I do this? Please help.
RadioButtonGroup(
activeColor: Color(ProjectColors.mainColor),
labelStyle: TextStyle(
fontFamily: "AN",
fontSize: 16,
fontWeight: FontWeight.w400,
color: Color(ProjectColors.secondaryColor),
),
labels: _role.values.map((x) => x.title).toList(),
)
Code is below:
String _picked = "Two"; //a variable _picked declared
RadioButtonGroup(
orientation: GroupedButtonsOrientation.HORIZONTAL,
margin: const EdgeInsets.only(left: 12.0),
onSelected: (String selected) => setState((){
_picked = selected;
}),
labels: <String>[
"One",
"Two",
],
picked: _picked,
itemBuilder: (Radio rb, Text txt, int i){
return Column(
children: <Widget>[
Icon(Icons.public),
rb,
txt,
],
);
},
),
Output: