Skip to content

Instantly share code, notes, and snippets.

@jamesmenera
Last active January 7, 2020 02:01
Show Gist options
  • Save jamesmenera/83ae2b9b5ebcf680383caf98e7af2513 to your computer and use it in GitHub Desktop.
Save jamesmenera/83ae2b9b5ebcf680383caf98e7af2513 to your computer and use it in GitHub Desktop.
Hacky way to add a placeholder/ empty value on flutter direct-select package
// Create a few methods to build the direct select that take additional options to add the placeholder
// and account for the changed index of doing this
// These are the following 3 methods
DirectSelectItem<String> _getDropDownMenuItem(value) {
return DirectSelectItem<String>(
key: ValueKey(new DateTime.now()),
itemHeight: 60,
value: value,
itemBuilder: (context, value) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Text(
value,
style: TextStyle(
color: brightBlue
)
),
);
});
}
_getDslDecoration() {
return BoxDecoration(
// color: headerBg,
border: BorderDirectional(
bottom: BorderSide(width: 1, color: brightBlue),
top: BorderSide(width: 1, color: brightBlue),
),
);
}
_makeDirectSelectList(List<String> items, int selectedIndex, Function onSelect, [Map<String,dynamic> options]) {
if (items[0] != options['placeholder']) {
items.insert(0, options['placeholder']);
}
final dsl = new DirectSelectList<String>(
values: items,
defaultItemIndex: selectedIndex,
itemBuilder: (String value) => _getDropDownMenuItem(value),
focusedItemDecoration: _getDslDecoration(),
onItemSelectedListener: (item, index, context) {
final List<Map> originalList = new List<Map>.from(options['originalList']);
if (options['placeholder'] != null && index != 0) {
onSelect(item, index, context, originalList, index - 1);
} else if (options['placeholder'] != null && index == 0) {
return;
} else {
onSelect(item, index, context, originalList);
}
}
);
// Then I can use it like this in my template
_makeDirectSelectList(_selectableZoneNames, _selectedZoneIndex, (zone, index, context, originalList, correctedIndex) {
setState(() {
_selectedZoneId = originalList[correctedIndex]['id'];
_selectedZoneIndex = index;
});
}, {
'originalList': _selectableZones,
'placeholder': 'Select Zone'.toUpperCase()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment