-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add datetime pickers separately. #1
- Loading branch information
1 parent
fd80e37
commit 10e8659
Showing
5 changed files
with
90 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -206,3 +206,5 @@ Now we're ready to rock 🎸! | |
|
||
## 2. `Material` Date/Time Pickers | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:intl/intl.dart'; | ||
|
||
class MaterialExamplePage extends StatefulWidget { | ||
const MaterialExamplePage({super.key}); | ||
|
||
@override | ||
State<MaterialExamplePage> createState() => _MaterialExamplePageState(); | ||
} | ||
|
||
class _MaterialExamplePageState extends State<MaterialExamplePage> { | ||
DateTime dateTime = DateTime.now(); | ||
|
||
Future<DateTime?> pickDate() => showDatePicker(context: context, initialDate: dateTime, firstDate: DateTime(1900), lastDate: DateTime(2100)); | ||
|
||
Future<TimeOfDay?> pickTime() => showTimePicker(context: context, initialTime: TimeOfDay(hour: dateTime.hour, minute: dateTime.minute)); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Container( | ||
child: Padding( | ||
padding: const EdgeInsets.only(right: 8.0, left: 8.0), | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
Row( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
Text( | ||
DateFormat('yyyy-MM-dd').format(dateTime), | ||
style: const TextStyle(fontSize: 30, fontWeight: FontWeight.bold), | ||
), | ||
Text( | ||
DateFormat(' kk:mm').format(dateTime), | ||
style: const TextStyle(fontSize: 30), | ||
), | ||
], | ||
), | ||
Padding( | ||
padding: const EdgeInsets.only(top: 16.0), | ||
child: Row( | ||
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||
children: [ | ||
ElevatedButton( | ||
child: const Text("Date", style: TextStyle(fontSize: 20),), | ||
onPressed: () async { | ||
final newDate = await pickDate(); | ||
if (newDate == null) return; // person pressed 'CANCEL' | ||
|
||
// Update datetime object that's shown with new date | ||
final newDateTime = DateTime(newDate.year, newDate.month, newDate.day, dateTime.hour, dateTime.minute); | ||
setState( | ||
() => dateTime = newDateTime, | ||
); | ||
}), | ||
ElevatedButton( | ||
child: const Text("Time", style: TextStyle(fontSize: 20),), | ||
onPressed: () async { | ||
final newTime = await pickTime(); | ||
if (newTime == null) return; // person pressed 'CANCEL' | ||
|
||
// Update datetime object that's shown with new time | ||
final newDateTime = DateTime(dateTime.year, dateTime.month, dateTime.day, newTime.hour, newTime.minute); | ||
setState( | ||
() => dateTime = newDateTime, | ||
); | ||
}) | ||
], | ||
), | ||
) | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters