showPicker function

void showPicker(
  1. BuildContext context,
  2. List<String> items, {
  3. PickerItemSelected? itemSelected,
  4. PickerItemSelected? onDone,
  5. bool useLight = true,
  6. Color textColor = Colors.black,
  7. int? startIndex,
  8. String? title,
})

show picker dialog with List of string items

Implementation

void showPicker(
  BuildContext context,
  List<String> items, {
  PickerItemSelected? itemSelected,
  PickerItemSelected? onDone,
  bool useLight = true,
  Color textColor = Colors.black,
  int? startIndex,
  String? title,
}) {
  var selected = 0;
  var controller = FixedExtentScrollController(initialItem: startIndex ?? 0);
  var views = items
      .map((item) => Center(
              child: Text(
            item,
            style: TextStyle(color: textColor),
          )))
      .toList();

  var picker = () => CupertinoPicker(
        scrollController: controller,
        onSelectedItemChanged: (index) {
          selected = index;
          if (itemSelected != null) itemSelected(selected);
        },
        magnification: 1.2,
        looping: false,
        itemExtent: 40.0,
        children: views,
      );

  showModalBottomSheet(
    context: context,
    backgroundColor: CupertinoColors.systemGrey6,
    clipBehavior: Clip.hardEdge,
    builder: (BuildContext ctx) {
      return Container(
        height: (MediaQuery.of(context).size.height / 3) + 50,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisSize: MainAxisSize.max,
          children: [
            Container(
              color: Colors.white,
              width: double.infinity,
              child: Row(
                mainAxisSize: MainAxisSize.max,
                children: [
                  Visibility(
                    visible: title != null,
                    child: Text(
                      title ?? "",
                      style: TextStyle(
                        color: Colors.black,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                  Expanded(child: Container()),
                  Align(
                    alignment: Alignment.centerRight,
                    child: SizedBox(
                      height: 35,
                      width: 60,
                      child: CupertinoButton(
                        onPressed: () {
                          Navigator.pop(ctx);
                          if (onDone != null) onDone(selected);
                        },
                        padding: EdgeInsets.all(0),
                        child: Text(
                          "Done",
                          style: TextStyle(
                            color: CupertinoColors.activeBlue,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Expanded(
              child: useLight
                  ? Theme(data: ThemeData.light(), child: picker())
                  : picker(),
            ),
          ],
        ),
      );
    },
  );
}