Searchable List of icons - list

I would like to create a dropdown or a list of all free FontAwesomeIcons to allow the user choose the one he/she like more. Also if You write something, the list have to filter the icons(that´s optional).
List<IconData> fontAwesomeIcons = [FontAwesomeIcons.accessibleIcon,FontAwesomeIcons.americanSignLanguageInterpreting,FontAwesomeIcons.assistiveListeningSystems,FontAwesomeIcons.audioDescription,];

You can do the following:
DropdownButton<IconData>(
value: dropdownValue,
onChanged: (IconDatanewValue) {
setState(() {
dropdownValue = newValue;
});
},
items: <IconData>[FontAwesomeIcons.accessibleIcon,FontAwesomeIcons.americanSignLanguageInterpreting,FontAwesomeIcons.assistiveListeningSystems,FontAwesomeIcons.audioDescription]
.map<DropdownMenuItem<IconData>>((IconData value) {
return DropdownMenuItem<IconData>(
value: value,
child: Text(value),
);
})
.toList(),
),
The DropdownMenuItem is a class used to represent the items.
onChanged is called when the user selects an item.
Check the docs for more information:
https://api.flutter.dev/flutter/material/DropdownButton-class.html

Related

How do I create a list of map values on a button pressed in flutter

I'm trying to create a List of values like
{
"name1" : "one"
},
where I get the "one" mentioned above from a textfield. Suppose I input three of such values in three separate textFields.
Now I have to create a List called "names" as follows when I press a submit button
names: [
{
"name1" : "one"
},
{
"name2" : "two"
},
{
"name3" : "three"
},
]
here "one", "two", "three" are the textField values and I need to generate the rest when there is a button click event
(I have to create many of these for a http post I'll have to do later, So please feel free to let me know a better method if exists or please correct me if the procedure I'm heading with is wrong)
You need to add all TextField value in list look like below on button tap event
var nameOne = TextEditingController();
var nameTwo = TextEditingController();
var nameThree = TextEditingController();
List<Map<String,String>> names = [];
void _submite() {
if (nameOne.text.isEmpty) {
print('Enter nameOne name');
} else if (nameTwo.text.isEmpty) {
print('Enter nameTwo name');
} else if (nameThree.text.isEmpty) {
print('Enter nameThree name');
} else {
names.add({"name1": nameOne.text});
names.add({"name2": nameTwo.text});
names.add({"name3": nameThree.text});
}
}
After call _submite() method
//OUTPUT : [{"name1" : "one"},{"name2" : "two"},{"name3" : "three"},]
you can do like this:
List names = [
{"name1": "one"},
{"name2": "two"},
{"name3": "three"},
];
#override
Widget build(BuildContext context) {
return Container(
height: 300,
width: 300,
child: ListView.builder(
itemCount: names.length,
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
return GestureDetector(
onTap: () {
//do what you want here for each item
print(names[index].keys.first.toString());
},
child: Text(names[index].keys.first.toString()));
},
),
);
}
here you generate a ListView of Texts that are wrapped with GestureDetector,
so you generate a list of Texts according to your list, and when you click on one of the texts you can do what ever you want according to the item you clicked on.
so for example, the output for the previous code would look like this:
and when you click on one of the texts, you can do what you whatever you want according to the clicked item (here I just print the item to the consol).

make a List from class elements

I have a simple question, I have dummy data about some products and I need to take all categories into the new list. How can I do it?
class Products with ChangeNotifier {
List<Product> _productItems = [
Product(
id: 'p1',
title: 'Red Shirt',
price: 29.99,
category: 'shirts',
),
Product(
id: 'p2',
title: 'Trousers',
description: 'A nice pair of trousers.',
price: 59.99,
category: 'Trousers',
),
Product(
id: 'p3',
title: 'Yellow Scarf',
price: 19.99,
category: 'Scarfs',
),
Product(
id: 'p4',
title: 'A Pan',
price: 49.99,
category: 'Pans',),];
List<Product> get items {
return [...items];
}
List<Product> get fovoriteItems {
return _productItems.where((prodItem) => prodItem.isFavorite!).toList();
}
List<Product> get stockItems {
return _productItems.where((prodItems) => prodItems.isStock!).toList();
}
List<Product> get categoriesList {}
}
I need to take a List like categories = [Shirts,Trousers,Scarfs,Pans];
Your problem isn't clear but there are notable issues in your code. You firstly need to update your items method to return all the products. So update it to something like this:
List<Product> get items {
return _productItems;
}
Then in the get favoriteItems method, you have not defined the isFavorite property in any of the dummy classes. So update them to include it. This also goes for your get stockItems method. Here's an example:
Product(
id: 'p9',
title: 'Apples',
description: 'A delicious red treat',
price: 1.99,
category: 'Food',
isFavorite: false,
isStock: true,
),
Also make sure to remove the ! from prodItem.isFavorite! and prodItems.isStock! because this will give the opposite result.
The categoriesList method should be of type String because a category isn't necessarily product. Here's the implementation:
List<String> get categories {
List<String> ctgrs = [];
for (item in _productItems) {
ctgrs.add(item.category);
}
return ctgrs;
}
I would also highly recommend using UniqueKey() or using the UUID package for every product's id so you don't have to make a custom one for every product. Using UUID is very secure as well.
List<String> categories = [];
_productItems.forEach((element) {
if (categories.indexOf(element.category) < 0) {//avoid duplicates
categories.add(element.category);
}
});

How to pass list to another class?

I have problems wit passing data to another class and trying to solve it.
Basicly I'm getting some values from textfields and storing them in a list.
ElevatedButton(
child: Text("Add Task"),
onPressed: () {
setState(() {
itemList.add(
ListItems(
title: _titleController.text,
text: _textController.text,
selectedDayTime: _selectedDay,
),
);
Navigator.pop(context);
});
},
),
I have also another class to show these items as a listview.Everytime I add new item to the list, I will show this data as listview item. However, I don't know how to pass the itemList from AddTask class to HomePage class.
ListItems class:
class ListItems {
String? title;
String? text;
DateTime? selectedDayTime;
ListItems(
{required this.title, required this.text, required this.selectedDayTime});
}
My question is how can I pass the itemList from AddTask class and use it in my HomePage class.

How to ADD a List<String> to CheckboxListTile values?

On this example im trying to add to the CheckboxListTile the values i have saved on my List.
But for somereason im stuck here and i cant figure it out how to solve this problem.
Global List
List<String> _lstNomeData = new List<String>();
Values Map
Map<String, bool> values = {
'$_lstNomeData[index]': false,
};
Get selected checkbox values
var tmpArray = [];
getCheckboxItems() {
values.forEach((key, value) {
if (value == true) {
tmpArray.add(key);
}
});
print(tmpArray);
tmpArray.clear();
}
Body
body: Column(
children: <Widget>[
Expanded(
child: ListView(
children: values.keys.map((String key) {
return new CheckboxListTile(
title: new Text(key),
value: values[key],
activeColor: Colors.blue,
checkColor: Colors.white,
onChanged: (bool value) {
setState(() {
values[key] = value;
});
},
);
}).toList(),
),
),
],
)
Error
Print of the error displayed
You are on the right track. You can do two things here:
Prefill the map with false for every key (what you are trying to do)
Assume that if the map does not have a key, the answer is false (default)
Second approach is probably even better because by prefilling the map with false you could not distinct between false being actually answered by the user or if it was set by default. If a key is not in the map you know that the user has not answered the question so far. I will go and show how to work with the second approach:
Keep your global list as it is:
List<String> _lstNomeData = [];
Initialise the map (which represents the answers from the user for each question) with an empty Map:
Map<String, bool> answers = {};
Now correctly reference those answers in your Checkbox widget and make use of the onChanged property:
ListView(
children: _lstNomeData.map((key) =>
CheckboxListTile(
title: Text(key),
// Here we check if the answers map has this key and if it does, use the bool value, otherwise set it to false (user has not answered this so far)
value: answers[key] ?? false,
activeColor: Colors.blue,
checkColor: Colors.white,
onChanged: (answer) =>
setState(() => answers[key] = answer)
),
).toList(),
),

Flutter/Dart: Defining a list from a variable in drop down menu

I have a list of profile fields that I would like to create using a single widget, but I'm new to Flutter and can't seem to work out one single thing: passing a list variable through a parameter to a drop down menu. I have been able to create many drop down field widgets that work just fine, using (for example) _countries.map(), but now that I'm trying to convert the drop down field to a single widget as not to repeat myself, it gives me an error Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<DropdownMenuItem<String>>' when I try to pass it through a variable.
I have the following code (of course removing all that I believe to be unnecessary):
class _ProfileDataState extends State<ProfileData> {
final _countries = DropDownLists.countries;
String _country;
var listType;
Widget profileDropDown(var list, var listType) {
return Card(
onTap: () async {
AlertDialog(
content: DropdownButtonFormField<String>(
isExpanded: true,
items: list.map((String value) { // <-- If I test this with _countries.map(), it works
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
isDense: true,
value: listType,
onChanged: (value) {
FocusScope.of(context).requestFocus(FocusNode());
setState(() {
this.listType = value;
});
},
)
)
}
)
}
#override
Widget build(BuildContext context) {
return profileDropDown(_countries, _country),
...