Home > Enterprise >  how to disable past time in showTimePicker()
how to disable past time in showTimePicker()

Time:02-18

I am trying to let user select time but I don't want him to select past time.

final TimeOfDay picked = await showTimePicker(
       context: context,
       initialTime: selectedTime,
);

is there a way without using any package?

CodePudding user response:

This question is similar to this. I am repeating the same answer.

This CustomTimerPicker provide hour and minute on callback, and we can provide initial TimeOfDay else it will use TimeOfDay.now().

Run on dartPad.

class CustomTimerPicker extends StatefulWidget {
  late final TimeOfDay intiTime;

  final Function(int selectedHour, int selectedMinute) onChanged;
  CustomTimerPicker({
    Key? key,
    required this.onChanged,
    TimeOfDay? intiTimeOfDay,
  }) : super(key: key) {
    intiTime = intiTimeOfDay ?? TimeOfDay.now();
  }

  @override
  State<CustomTimerPicker> createState() => _CustomTimerPickerState();
}

class _CustomTimerPickerState extends State<CustomTimerPicker> {
  late int selectedHour;
  late int selectedMinutes;

  @override
  void initState() {
    super.initState();
    selectedHour = widget.intiTime.hour;
    selectedMinutes = widget.intiTime.minute;
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        // hour picker
        SizedBox(
          width: kToolbarHeight,
          child: CupertinoPicker(
            // backgroundColor: Colors.cyanAccent, //bg Color
            itemExtent: 50,
            onSelectedItemChanged: (v) {
              setState(() {
                selectedHour = v   widget.intiTime.hour;
              });
              widget.onChanged(
                selectedHour,
                selectedMinutes,
              );
            },
            children: List.generate(
              24 - widget.intiTime.hour,
              (index) => Center(
                child: Text(
                  "${index   widget.intiTime.hour}",
                  textAlign: TextAlign.center,
                ),
              ),
            ),
          ),
        ),

        //minute Picker
        SizedBox(
          width: kToolbarHeight,
          child: CupertinoPicker(
            itemExtent: 50,
            onSelectedItemChanged: (v) {
              setState(() {
                selectedMinutes = v   widget.intiTime.minute;
              });
              widget.onChanged(
                selectedHour,
                selectedMinutes,
              );
            },
            children: List.generate(
              60 - widget.intiTime.minute,
              (index) => Center(
                child: Text(
                  "${index   widget.intiTime.minute}",
                  textAlign: TextAlign.center,
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }
}

And use this widget providing height

SizedBox(
  height: 200,
  child: CustomTimerPicker(
    // intiTimeOfDay: TimeOfDay(hour: 3, minute: 4),
    onChanged: (selectedHour, selectedMinute) {
      setState(() {
        h = selectedHour;
        m = selectedMinute;
      });
      debugPrint("H: $selectedHour minute: $selectedMinute");
    },
  ),
),

CodePudding user response:

use 'initialTime: dateTime.now()' instead selectedTime

  • Related