Flutter Duration User Input

2,590

You can use flutter_picker package for this.

Following snippet shows an example that takes hours and minutes input from the user:

void onTap() {
  Picker(
    adapter: NumberPickerAdapter(data: <NumberPickerColumn>[
      const NumberPickerColumn(begin: 0, end: 999, suffix: Text(' hours')),
      const NumberPickerColumn(begin: 0, end: 60, suffix: Text(' minutes'), jump: 15),
    ]),
    delimiter: <PickerDelimiter>[
      PickerDelimiter(
        child: Container(
          width: 30.0,
          alignment: Alignment.center,
          child: Icon(Icons.more_vert),
        ),
      )
    ],
    hideHeader: true,
    confirmText: 'OK',
    confirmTextStyle: TextStyle(inherit: false, color: Colors.red, fontSize: 22),
    title: const Text('Select duration'),
    selectedTextStyle: TextStyle(color: Colors.blue),
    onConfirm: (Picker picker, List<int> value) {
      // You get your duration here
      Duration _duration = Duration(hours: picker.getSelectedValues()[0], minutes: picker.getSelectedValues()[1]);
    },
  ).showDialog(context);
}
Share:
2,590
Marc B.
Author by

Marc B.

Updated on December 14, 2022

Comments

  • Marc B.
    Marc B. over 1 year

    In my app I need the user to give me a duration, I am thinking about seconds and minutes. So after pressing a button i want something like a Pop-Up Window in which the user then can select a duration by scrolling through a list and picking the minutes and seconds (or something similar to that).

    After a long internet search I can't find any template which does something like that and I really don't want to code everything by hand. I would appreciate a link to a template and if not maybe suggestions on what would be the right way to do something like that.