Using Time Picker and dealing with time objects
In Nowa, you can use the Time Picker to allow users to select a specific time. This is useful in many real-life scenarios, like:
- Letting users set reminders or alarms.
- Choosing a time for booking appointments.
- Selecting delivery or pickup time.
- Logging activity times (e.g., workout or medication).
π How to Add a Time Pickerβ
To use the time picker in your app:
- Open a function that belongs to a screen or component. This could be an event like
On Pressed
or a function you created. - Click on the small circle to add a node.
- From the node menu, open the category
Nowa
β chooseshowTimePicker
.
Once added, you can customize the time picker popup from the Details Panel on the right.
β±οΈ Receiving the Picked Timeβ
The showTimePicker
node returns a Future<TimeOfDay>
. Here's what that means:
TimeOfDay
is a special object that holds a time (e.g., 8:30 PM).Future<TimeOfDay>
means it returns the result later when the user finishes selecting the time.
You can handle this in two ways:
Option 1: Using await
β
- Turn on the
await
toggle in the Future Options section. - This makes the function wait until the user selects a time.
- Then scroll down to
Store Result
:- Choose new variable β creates a temporary variable of type
TimeOfDay
. - Or choose an existing variable (must also be
TimeOfDay
).
- Choose new variable β creates a temporary variable of type
Once stored, you now have access to the selected time to use however you want.
βοΈ Time Picker Node Optionsβ
Here are the configurable options available in the showTimePicker
node:
- Initial Time (Hour, Minute): Sets the default time when the picker opens.
- Builder: Leave it as
null
unless you want to override the design. - Barrier Dismissible: If off, the picker canβt be closed by tapping outside.
- Barrier Color: Color of the background behind the popup.
- Barrier Label: Used for accessibility tools.
- Use Root Navigator: Should be on if you're using nested navigators.
- Cancel Text / Confirm Text: Change the text for the Cancel/Confirm buttons.
- Help Text: Add a label at the top of the picker.
- Hour Label / Minute Label: Customize labels inside the picker.
- Error Invalid Text: Message if user enters an invalid time (for manual input).
- Route Settings / Entry Mode / Anchor Point: Advanced configuration for dialog display.
- Switch To Input Mode / Dial Mode: Choose how users select time.
π§ Understanding TimeOfDay
β
When the user selects a time, it returns a TimeOfDay
object.
You can call the following methods on it:
hour
: Returns the hour (0β23).minute
: Returns the minute (0β59).period
: AM or PM.hourOfPeriod
: Hour in 12-hour format.format(...)
: Returns the time as a formattedString
(e.g., "8:45 PM").isBefore(...)
,isAfter(...)
,isAtSameTimeAs(...)
: Compare times.toString()
: Returns the raw string representation.
π‘ Real Use Case Exampleβ
We created a screen with:
- A Button to pick a time.
- A Text widget that shows the selected time.
- A variable called
time
(String) connected to the Text widget.
Steps:
- Open the
On Pressed
event for the button. - Add
showTimePicker
and turn onawait
. - Store the result in a new variable called
selectedTime
. - Add a new node using
selectedTime
, and press + next to it in the details panel. - Choose the
format()
method to convert the time into a readable string. - Choose to store the result into the existing
time
variable. - Add a
refresh
node to update the UI with the new time.
Now when the user picks a time, it will appear in the Text widget!
π Using On Value
Instead of await
β
You can also handle the result using a callback by:
- Opening the same
On Pressed
event. - Add
showTimePicker
but donβt turn onawait
. - Click
On Value
β attach a function (callback). - Inside the callback, use the
value
parameter (which is of typeTimeOfDay
). - Call
format()
onvalue
, and store the result in thetime
variable. - Add a
refresh
node.
On Value
?Using On Value
doesnβt pause the rest of the functionβit waits in the background and reacts once the user selects a time.
Both approaches (await
and On Value
) achieve the same result. Choose the one that best fits your flow!