Welcome to this exciting journey of discovering the power and flexibility of Flutter. In this blog, we aim to discuss the practical implementation of the Flutter Time Picker, Flutter Date Time Picker, and the robust Flutter Date range picker.
Pickers are common UI components we see in many mobile applications, allowing the user to select a single value from a range of values. In the Flutter ecosystem, we often encounter situations where we need to pick a date, time or both, or even a range of dates.
In this article, we are going to walk through the creation process of these key components. We believe that this post will serve as a handy guide for both beginners and intermediate developers looking to integrate a Flutter Time Picker or a date picker in their Flutter applications. We aim to keep this discussion enjoyable and reader-friendly, focusing on the practical use cases so that you can directly utilize the knowledge in your Flutter applications.
In the world of Flutter, picker controls are critical elements that might seem trivial but play a substantial role in enhancing the user experience. They're employed when we want the user to select a single value or multiple dates from a range of options. Time Picker, Date Picker and Date Range Picker are all key examples of this wide-ranging toolset.
Imagine having a booking app or an event app where the user needs to specify dates or time slots. It's almost impossible to build a user-friendly app of this kind without the use of a date, time, or date and time picker. These components not only allow the user to input their desired date and time data quickly and intuitively but also help to ensure the data is inputted in a standardized format, leading to fewer discrepancies and errors.
Flutter offers a mighty, yet flexible widget for picking time – the TimePicker. It establishes an interactive dialog where users can view and modify a time in a 24-hour format. An important aspect that we, as developers, love about the Flutter TimePicker is its ease of implementation and customization.
Using the TimePicker widget is relatively straightforward. Here's an example of how you can create a basic TimePicker in your Flutter widget:
1 Future<void> _selectTime(BuildContext context) async { 2 final TimeOfDay picked = await showTimePicker( 3 context: context, 4 initialTime: TimeOfDay.now(), 5 ); 6 if (picked != null) 7 print({picked.hour.toString() + ':' + picked.minute.toString()}); 8 } 9
You can call the _selectTime function when the user interacts with a button, or in any event that you think is appropriate, to show a time picker dialog. The showTimePicker function displays the TimePicker dialog, where the user can select the hour and minute.
Flutter provides several ways to customize the look and feel of your TimePicker. For instance, you can use the builder parameter to specify a custom layout:
1 Future<void> _selectTime(BuildContext context) async { 2 final ThemeData theme = Theme.of(context); 3 final TimeOfDay picked = await showTimePicker( 4 context: context, 5 initialTime: TimeOfDay.now(), 6 builder: (BuildContext? context, Widget? child) { 7 return theme.isDark 8 ? MediaQuery( 9 data: MediaQuery.of(context!).copyWith(alwaysUse24HourFormat: false), 10 child: child!, 11 ) 12 : child!; 13 }, 14 ); 15 if (picked != null) 16 print({picked.hour.toString() + ':' + picked.minute.toString()}); 17 } 18
In this example, we used the builder attribute to create a TimePicker that changes its format based on the app's current theme.
Date Picker is another interactive dialog for picking dates within the Flutter widget system. It's easy to use and gives us the ability to ensure that users input date information in the correct format. Like the TimePicker, the DatePicker Widget in Flutter offers high customization options, and its implementation is equally uncomplicated.
Using a DatePicker in Flutter is relatively simple. Below is a basic usage code where we provide an initial date, and constraints on the start and end dates:
1 Future<void> _selectDate(BuildContext context) async { 2 final DateTime? picked = await showDatePicker( 3 context: context, 4 initialDate: DateTime.now(), 5 firstDate: DateTime(2015, 8), 6 lastDate: DateTime(2101), 7 ); 8 if (picked != null) 9 print({picked.toString()}); 10 } 11
The _selectDate function can be called upon a user action to present the DatePicker dialog. In this example, the showDatePicker function opens the DatePicker dialog, allowing users to pick a date within a specific range.
Flutter DatePicker is highly customizable, just like TimePicker. Here's an example of how to use the builder parameter to modify the DatePicker dialog:
1 Future<void> _selectDate(BuildContext context) async { 2 final ThemeData theme = Theme.of(context); 3 final DateTime? picked = await showDatePicker( 4 context: context, 5 initialDate: DateTime.now(), 6 firstDate: DateTime(2015, 8), 7 lastDate: DateTime(2101), 8 builder: (BuildContext? context, Widget? child) { 9 return theme.isDark 10 ? MediaQuery( 11 data: MediaQuery.of(context!).copyWith(alwaysUse24HourFormat: false), 12 child: child!, 13 ) 14 : child!; 15 }, 16 ); 17 if (picked != null) 18 print({picked.toString()}); 19 } 20
This builder attribute lets us design a DatePicker that is contextually aware and adjusts its presentation according to the app's current theme.
Until now, we discussed the ins and outs of TimePicker and DatePicker widgets in Flutter. But, what if our users need to select multiple dates or a range of dates at once?
This scenario introduces us to another compelling Flutter widget – the Date Range Picker. This widget allows the user to pick a range of dates, thus effectively packing the power of multiple instances of Date Pickers into one. It's an excellent tool when we want to restrict the user to select a date within a specific range in our Flutter app.
Creating a Date Range Picker in your Flutter app can be done with the following code:
1 Future<void> _selectDateRange(BuildContext context) async { 2 final DateTimeRange? picked = await showDateRangePicker( 3 context: context, 4 initialDateRange: DateTimeRange( 5 start: DateTime.now(), 6 end: DateTime.now().add(Duration(days: 7)), 7 ), 8 firstDate: DateTime(DateTime.now().year - 5), 9 lastDate: DateTime(DateTime.now().year + 5), 10 ); 11 if (picked != null) 12 print({picked.start.toString() + ' - ' + picked.end.toString()}); 13 } 14
Here, the showDateRangePicker function displays a Date Range Picker dialog, permitting users to select a range of dates within an allowable range.
Customizing the Date Range Picker provides even more capabilities. Let's modify the builder parameter to mould the Date Range Picker:
1 Future<void> _selectDateRange(BuildContext context) async { 2 final ThemeData theme = Theme.of(context); 3 final DateTimeRange? picked = await showDateRangePicker( 4 context: context, 5 initialDateRange: DateTimeRange( 6 start: DateTime.now(), 7 end: DateTime.now().add(Duration(days: 7)), 8 ), 9 firstDate: DateTime(DateTime.now().year - 5), 10 lastDate: DateTime(DateTime.now().year + 5), 11 builder: (BuildContext? context, Widget? child) { 12 return theme.isDark 13 ? MediaQuery( 14 data: MediaQuery.of(context!).copyWith(alwaysUse24HourFormat: false), 15 child: child!, 16 ) 17 : child!; 18 }, 19 ); 20 if (picked != null) 21 print({picked.start.toString() + ' - ' + picked.end.toString()}); 22 } 23
This builder attribute will assist us in maintaining a consistent interface designed according to the app's ongoing theme.
By now, we've seen how to create a Flutter Time Picker, Flutter Date Time Picker and a Flutter Date range picker. But, how do we employ these pieces in a real-world Flutter application? To answer this, let's consider a simple case study.
Imagine we're creating an appointment scheduling app wherein users need to book an appointment date and time.
Firstly, we'll implement date selection using a Flutter DatePicker:
1 Future<void> _selectAppointmentDate(BuildContext context) async { 2 final DateTime? picked = await showDatePicker( 3 context: context, 4 initialDate: DateTime.now(), 5 firstDate: DateTime.now(), 6 lastDate: DateTime.now().add(Duration(days: 365)), 7 ); 8 9 if (picked != null) 10 setState(() { 11 _appointmentDate = picked; 12 }); 13 } 14
Now, once the date is selected, the user should be able to select a time slot. Let's do that using the Flutter Time Picker:
1 Future<void> _selectAppointmentTime(BuildContext context) async { 2 final TimeOfDay? picked = await showTimePicker( 3 context: context, 4 initialTime: TimeOfDay.now(), 5 ); 6 7 if (picked != null) 8 setState(() { 9 _appointmentTime = picked; 10 }); 11 } 12
With the above simple implementations, our users can easily select a suitable date and time for their appointments.
As we wrap up, it's impossible to ignore the modern tools developers are equipped with, which allow us to focus more on logic and less on repetitive coding tasks. An incredible assistant to note is WiseGPT; it is a powerful tool designed to accelerate the coding process by auto-generating widget code, even managing states and aiding in crafting complex widgets & animations.
WiseGPT shines with frameworks like Flutter where widgets are the backbone. Need code for TimePicker, DatePicker, or perhaps an intricate animation? No worries, WiseGPT is on it!
Writing API endpoint functions, managing states, or even creating models can be done promptlessly with WiseGPT. It commits to not just writing code, but emulating your style making the generated code feel like it was handcrafted by you only.
In essence, with the Flutter TimePicker, DatePicker, Date Range Picker, and WiseGPT at your tooling disposal, you are ready to create more engaging and interactive applications. Flutter makes developing beautiful apps incredibly accessible, but WiseGPT's assistance makes it even more delightful.
So, your journey to conquering date and time pickers in Flutter has now reached a new milestone. Along the way, we deciphered not only how to skillfully craft these wonderful widgets but also how to customize them seamlessly to align with your creative vision.
You must remember – while development might sometimes feel complicated, tools at our disposal like WiseGPT continue to simplify things and increase our productivity. With every widget you successfully integrate, you are adding to that unique user experience.
Keep experimenting, keep Fluttering those developer wings, and continue creating astounding applications that make life simpler and enjoyable. 💙✨
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.