[Flutter]: Searching not working in SelectFormField package - flutter

Using this flutter package: https://pub.dev/packages/select_form_field
Hello I am having an issue regarding searching in "SelectFormFieldType.dialog", I don't know if I have missed something but it seems like the search doesn't seem to work.
Here is my code:
List of items:
List<Map<String, dynamic>> _items = [
{
'value': '',
'label': 'Select Account',
'icon': Icon(Icons.account_balance)
},
{'value': '1', 'label': 'Account 1', 'icon': Icon(Icons.account_balance)},
{'value': '2', 'label': 'Account 2', 'icon': Icon(Icons.account_balance)},
{'value': '3', 'label': 'Account 3', 'icon': Icon(Icons.account_balance)}
];
Here is selectformfield:
SelectFormField(
dialogCancelBtn: 'Cancel',
enableSearch: true,
dialogSearchHint: 'Search account',
type: SelectFormFieldType.dialog,
icon: Icon(Icons.account_balance),
labelText: 'Select Account',
hintText: 'Select Account',
items: _items,
onChanged: (val) {
print(val);
},
)
Here is the screenshot of the list
list of items
Here is the screenshot of output when searching item
output

Related

Add Action to AndroidNotificationDetails

I would like to add a button to my notification, I was able to get it displayed but I can't find the documentation on how to add an action to the button press
const androidNotification = AndroidNotificationDetails(
'Bla', 'Bla',
icon: 'ic_bg_service_small',
ongoing: true,
actions: [AndroidNotificationAction("stop", "Stop")]);
flutterLocalNotificationsPlugin.show(
888,
'MyService',
'bla',
const NotificationDetails(android: androidNotification),
);
You need to use this plugin to make action in notifications https://pub.dev/packages/awesome_notifications
void _showNotificationWithButton() {
AwesomeNotifications().createNotification(
content: NotificationContent(
id: 10,
channelKey: 'basic_channel',
title: 'Simple Notification',
body: 'Simple body'),
actionButtons: <NotificationActionButton>[
NotificationActionButton(key: 'yes', label: 'Yes'),
NotificationActionButton(key: 'no', label: 'No'),
],
);
}

How can we get the current selected item from the Dropdown Search Package in Flutter

I am using this package
https://pub.dev/packages/dropdown_search
I have a dropdown inside a form I made, when a user selects an item, I would like to get the value of the selection and assign it to a variable. I've read some docs but they are quite hard to understand in regards to getting the value out. As far as I understood, the SelectedItem property is only to set a starting selection. How can I get the selected item from the dropdown?
DropdownSearch<String>(
mode: Mode.MENU,
showSearchBox: false,
showSelectedItems: true,
items: const [
'Item 1',
'Item 2',
'Item 3',
],
dropdownSearchDecoration: InputDecoration(
labelText: 'Items',
),
),
In my above code, I'm not understanding how I should get the selected item, I am uploading the selected item to Firestore.
I added an example for you, please check it
DropdownSearch<String>(
mode: Mode.MENU,
showSearchBox: false,
showSelectedItems: true,
items: const [
'Item 1',
'Item 2',
'Item 3',
],
dropdownSearchDecoration: InputDecoration(
labelText: 'Items',
),
onChanged: (String selectedValue) {
yourVariable = selectedValue; // you should change this line according to your own
},
),
There is an onChanged property for DropDownSearch. You can use it to get the selected item.
Eg:
DropdownSearch<UserModel>(
label: "Name",
onFind: (String filter) => getData(filter),
itemAsString: (UserModel u) => u.userAsStringById(),
onChanged: (UserModel data) { <----- THIS ONE
//Do something with this data
},
)

How to add json list items to dropdown search items in Flutter | Dart

I'm creating a dropdown button with search. So I using this package . Here with this package I have problem. The response from the API is like this.
List countries = [
{
'id': '1',
'name': 'Brazil',
},
{
'id': '2',
'name': 'India',
},
{
'id': '3',
'name': 'Japan',
},
{
'id': '4',
'name': 'Tokyo',
},
{
'id': '5',
'name': 'Australia',
},
{
'id': '6',
'name': 'Srilanka',
},
{
'id': '7',
'name': 'Canada',
},
];
My Dropdown code is this,
body: Column(
children: [
DropdownSearch<String>(
mode: Mode.MENU,
items: countries['name'],
showSearchBox: true,
label: "Menu mode",
hint: "country in menu mode",
onChanged: (value){
print(countries['name']);
},
selectedItem: "Canada",
),
],
),
Here items: countries['name'] line i getting error as The argument type 'String' can't be assigned to the parameter type 'int'. Also when i select a country i want to print the country id. eg: if i select country as japan then it should print 4 in console. My Code is not working.
First of all, a List of Map doesn't work like this -> (countries['name]), try replace your items to:
DropdownSearch<String>(
mode: Mode.MENU,
items: countries.map((e)=>e['name']).toList(),
showSearchBox: true,
label: "Menu mode",
hint: "country in menu mode",
onChanged: (value){
print(value);
},
selectedItem: "Canada",
),
Try below code hope its help to you.
Refer my answer here for put json data to dropdown
Padding(
padding: const EdgeInsets.all(8.0),
child: DropdownSearch<dynamic>(
mode: Mode.MENU,
items: countries.map((e) => e['name']).toList(),
showSearchBox: true,
label: "Menu mode",
hint: "country in menu mode",
onChanged: (value) {},
selectedItem: "Canada",
),
),
Your result screen->
you have to create a data class Country and override the toString function to return the country name.
and update your dropdownSearch like this:
//don't forget to generate the list of countries frop the map
DropdownSearch<Country>(
mode: Mode.MENU,
items: yourCountries,
showSearchBox: true,
label: "Menu mode",
hint: "country in menu mode",
onChanged: (Country value){
print(value.id);
},
selectedItem: Country("Canada",7),
),
use your own country Model like this
DropdownSearch<Country>(
mode: Mode.MENU,
items: yourCountries,
popupProps: PopupProps.dialog(
showSelectedItems: false,
showSearchBox: true,
),
showSearchBox: true,
label: "Menu mode",
hint: "country in menu mode",
itemAsString: (Country u) => u.name,
onChanged: (Country country){
print(country.name);
print(country.id);
},
selectedItem: Country("Canada",7),
),
don't forget to use -> itemAsString field
//OR
you can Visit the - https://pub.dev/packages/dropdown_search#customize-showed-field-itemasstring. section from the package it self

How can I insert new food data to specific category id

final List <Category> _categories = [
Category(
id: '1',
title: 'Poultry',
food: [
Food(
id: '1',
title: 'Chicken 1',
description: "qdwwqd",
price: 500.00,
image: "https://jb-ph-cdn.tillster.com/menu-images/prod/45df1872-c7f7-4b3d-baa9-1b0c4f56a5cc.png",
choice: [],
),
],
),
Category(
id: '2',
title: 'Vegetables',
food: [
Food(
id: '1',
title: 'Cabbage',
description: "qdwwqd",
price: 500.00,
image: "https://jb-ph-cdn.tillster.com/menu-images/prod/45df1872-c7f7-4b3d-baa9-1b0c4f56a5cc.png",
choice: [],
),
],
),
]
'''
In order to insert new data into the food array, first you need to find the correct category:
String myID = '1';
int index = _categories.indexWhere((cat) => cat.id == myID);
Once you did that, it is quite simple:
_categories[index].food.add(Food());

How to get List of dates when user selects days in Flutter

I want to show a list of dates when a user selects days.
Container(
padding: EdgeInsets.symmetric(horizontal: 10, vertical: 0),
decoration: BoxDecoration(
border: Border.all(
width: 1.5,
color: Color(0xFF5c77e0),
style: BorderStyle.solid),
borderRadius: BorderRadius.circular(3)),
// dropdown below..
child: DropdownButton<String>(
isExpanded: true,
value: dropdownValue,
icon: Icon(Icons.keyboard_arrow_down),
iconSize: 30,
underline: SizedBox(),
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: <String>[
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10',
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList()),
),
Suppose the user selected 5 days. I want to display dates of the previous 5 days from the current date, e.g today's date is "21-03-2020". If the user selected 5 days the list should be like:
17-03-2020
18-03-2020
19-03-2020
20-03-2020
21-03-2020
How can I do this?
Assuming that your input is a numeric string value.
List<String> getDates(String dateCount){
List<String> dates=[];
var yourDateCountValue=int.parse(dateCount);
var dateToday=DateTime.now();
while(yourDateCountValue!=0){
//Print Full date
print(dateToday.subtract(Duration(days: yourDateCountValue-1)));
//Print Date Exactly of your format
print(dateToday.subtract(Duration(days: yourDateCountValue-1)).toIso8601String().substring(0,10));
//Add dates to the list being started from 5 days back.
dates.add(dateToday.subtract(Duration(days: yourDateCountValue-1)).toIso8601String().substring(0,10));
yourDateCountValue--;
}
return dates;
}
call your method as
getDates("5");