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

transform does not work for showMaterialScrollPicker #33

Open blinddateapp1st opened 3 years ago

blinddateapp1st commented 3 years ago

flutter_material_pickers: ^3.1.0

I'm using a transform parameter in my showMaterialScrollPicker function. I expect to get a debug print message "transform" when building the picker dialog. but it looks like the transform function will not be called at all.

Future<T?> showPickerDialog<T>(
    BuildContext context,
    List<T> items,
    String transformString,
    void Function(T) onChange,
    T selected,
  ) {
    if (transformString.endsWith("."))
      transformString =
          transformString.substring(0, transformString.length - 1);
    return showMaterialScrollPicker(
      transformer: (e) => "$transformString.values.${items.indexOf(e)}".tr(),
      confirmText: LocaleKeys.standard_button_okay.tr(),
      cancelText: LocaleKeys.standard_button_cancel.tr(),
      context: context,
      title: "$transformString.title".tr(),
      items: items,
      onChanged: onChange,
      selectedItem: selected,
    );
  }

As List items i transform an enum to an array by passing "EnumName.values" this will then generate a List.

For my translation i use something like:

en:
"fruites.title": "Fruits",
"fruites.values.1": "Apple",
"fruites.values.2": "Banana",
"fruites.values.3": "Orange"
de:
"fruites.title": "Früchte",
"fruites.values.1": "Apfel",
"fruites.values.2": "Banane",
"fruites.values.3": "Orange"

Than i use an enum Fruites { apple, banana, orange }

And than i try to get the translation according to the index in the enum.

Hint: I'm using easy_localization as translator plugin.

blinddateapp1st commented 3 years ago

For the Checkbock Picker i'm using the same concept and there it works:

  Future<List<T>?> showCheckboxPicker<T>(
    BuildContext context,
    List<T> items,
    List<T> selected,
    String transformString,
    void Function(List<T>) onChange,
  ) {
    return showMaterialCheckboxPicker(
      context: context,
      items: items,
      selectedItems: selected,
      onChanged: onChange,
      transformer: (e) => "$transformString.values.${items.indexOf(e)}".tr(),
      confirmText: LocaleKeys.standard_button_okay.tr(),
      cancelText: LocaleKeys.standard_button_cancel.tr(),
      title: "$transformString.title".tr(),
    );
  }
Eranot commented 3 years ago

Can confirm this isn't working. I opened the pull request https://github.com/codegrue/flutter_material_pickers/pull/34 to fix it.

codegrue commented 2 years ago

Please check if this is working since the PR was merged in.