createOneCupertinoPicker<T> method

Widget createOneCupertinoPicker<T>({
  1. required List<T> values,
  2. required ValueChanged<int> onSelectedChanged,
  3. FixedExtentScrollController? scrollController,
  4. String displayOfValue(
    1. T e,
    2. int index
    )?,
  5. bool isSelectedValue(
    1. T e,
    2. int index
    )?,
  6. T? selectedValue,
})

Implementation

Widget createOneCupertinoPicker<T>({
  required List<T> values,
  required ValueChanged<int> onSelectedChanged,
  FixedExtentScrollController? scrollController,
  // get display text
  String Function(T e, int index)? displayOfValue,
  // check if selected
  bool Function(T e, int index)? isSelectedValue,
  T? selectedValue,
}) {
  return Expanded(
    flex: 1,
    child: Container(
      height: themeData.pickerHeight,
      decoration: const BoxDecoration(color: Colors.white),
      child: CupertinoPicker.builder(
        itemExtent: themeData.pickerItemHeight,
        backgroundColor: Colors.white,
        scrollController: scrollController,
        onSelectedItemChanged: onSelectedChanged,
        childCount: values.length,
        itemBuilder: (context, index) {
          dynamic value = values[index];
          String text = displayOfValue?.call(value, index) ?? (value != null ? '$value' : '');
          bool? isSelected = isSelectedValue?.call(value, index) ?? value == selectedValue;
          return Container(
            alignment: Alignment.center,
            height: themeData.pickerItemHeight,
            child: Text(
              text,
              style: isSelected
                  ? const TextStyle(color: Color(0xFF006BE1), fontSize: 18)
                  : TextStyle(color: const Color(0xFF31456A).withOpacity(0.36), fontSize: 16),
            ),
          );
        },
        selectionOverlay: Column(
          children: [
            Container(
              height: 0.5,
              color: const Color(0xFF31456A).withOpacity(0.16),
            ),
            const Spacer(),
            Container(
              height: 0.5,
              color: const Color(0xFF31456A).withOpacity(0.16),
            ),
          ],
        ),
      ),
    ),
  );
}