codegrue / flutter_material_pickers

A flutter package for displaying common picker dialogs.
https://pub.dev/packages/flutter_material_pickers
MIT License
98 stars 61 forks source link

Feature request: Ability to split label and value on scrollpicker #15

Closed julientechdev closed 3 years ago

julientechdev commented 3 years ago

The scrollpicker is currently taking a list of Strings as items parameter.

I have a use case where I'd like to pass a list of objects as items and specify which object property to use as label (showing in the list), but when selected the returning value would be another object property.

Here's what the show function call might look like:

showSplitScrollPicker(
context: context,
title: 'my split scrollpicker',
items: [{
 id: 1,
 label: 'first item',
 whatever: 'bla'
},{
 id: 2,
 label: 'second',
 whatever: 'bla 2'
},{
 id: 3,
 label: 'third',
 whatever: 'bla 3'
}],
itemLabel: 'label',
itemValue: 'id'
)
codegrue commented 3 years ago

I agree this is a needed change for all the list type pickers. It's a breaking change so will need to go into the 2.0 version.

gbaccetta commented 3 years ago

Hello there, thanks for the hard work! Do you plan to add this feature in a future release ? If I want to show localized String, conversion back and forth can become tricky without a fixed value being exchanged.

codegrue commented 3 years ago

What if I add an optional "values" collection? That way it won't be a breaking change.

gbaccetta commented 3 years ago

Yes that could be a good solution.

Although to do not break it I think it should still be a String list, and if a value List is provided, the variable returned as picked value, is the String from the value array instead of the String from the items array.

For example using enum, this could be straightforward. You would pass the localized version of the enum to the item list, and the enum label to the value list.

A breaking change could have two values returned, both the value and item and hence having the value be any type (i.e the enum value itself).

Another possibility could be a new type of picker altogether because you do not need this when using a numeric, date or color picker. You only need this when using String or Icon + String in my opinion... Although that may not be true for everyone.

codegrue commented 3 years ago

Feature added to 2.1.0

tehsphinx commented 3 years ago

@codegrue: Isn't the whole point of the values/items separation that the user never sees the values, but only the items (labels)?

Unfortunately now it shows the values instead of the items if provided. It should instead show the items and use the values as selectedValue.

codegrue commented 3 years ago

No it shows the items in the pickers. The sample app is displaying the returned values, but that's outside the library.

To show the item outside the picker, you would have to find the index of the value in the values list then get the item from the items list at the same index.

elliotrtd commented 3 years ago

But it doesn't show the items in the picker?

In your example MaterialScrollPicker shows the State Abbreviations in the Picker as well which is the list of values, not items, and it's the same in my application.

The Text widget for each item uses the value variable, which is called from values. The items are not shown anywhere, unless we're both totally missing something?

tehsphinx commented 3 years ago

No it shows the items in the pickers.

It doesn't.

tehsphinx commented 3 years ago

To elaborate: Completely agree with your statement @codegrue. Only problem is that it does not behave as you described. (The PR linked above fixes the issue).

codegrue commented 3 years ago

Can you post some screen shots? For me the example app behaves as I describe.

Hold on, checking...

codegrue commented 3 years ago

Fixed in 2.1.1. Sorry this was a lack of regression testing bug. Thanks for raising it and for the PR.

tehsphinx commented 3 years ago

Thx @codegrue

elliotrtd commented 3 years ago

Thanks for the super quick fix!

gbaccetta commented 3 years ago

Yes thank you,

That works great.

On Tue, Apr 13, 2021, 11:15 elliotrtd @.***> wrote:

Thanks for the super quick fix!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/codegrue/flutter_material_pickers/issues/15#issuecomment-818816884, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADK6WKTLMCWBF6O7265KPJDTIRNYVANCNFSM4SH2NV2Q .