-
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.
- Loading branch information
1 parent
3a4c4bf
commit 84429f6
Showing
5 changed files
with
74 additions
and
1 deletion.
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
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,62 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:intl/intl.dart'; | ||
|
||
class TUIDatePicker extends StatefulWidget { | ||
final String placeholder; | ||
final String dateFormat; | ||
final DateTime? minimumDate; | ||
final DateTime? maximumDate; | ||
final Function(DateTime)? dateSelected; | ||
|
||
const TUIDatePicker( | ||
{required this.placeholder, | ||
this.dateFormat = 'yyyy-MM-dd', | ||
this.minimumDate, | ||
this.maximumDate, | ||
this.dateSelected}); | ||
|
||
@override | ||
State<TUIDatePicker> createState() => _TUIDatePickerState(); | ||
} | ||
|
||
class _TUIDatePickerState extends State<TUIDatePicker> { | ||
final TextEditingController _dateController = TextEditingController(); | ||
DateTime? _selectedDate; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return TextField( | ||
controller: _dateController, | ||
decoration: InputDecoration( | ||
labelText: widget.placeholder, | ||
prefixIcon: Icon(Icons.calendar_today), | ||
), | ||
readOnly: true, | ||
onTap: () { | ||
_selectDate(context); | ||
}, | ||
); | ||
} | ||
|
||
Future<void> _selectDate(BuildContext context) async { | ||
final DateTime? pickedDate = await showDatePicker( | ||
context: context, | ||
initialDate: _selectedDate ?? DateTime.now(), | ||
firstDate: widget.minimumDate ?? DateTime(1900), | ||
lastDate: widget.maximumDate ?? DateTime(2101), | ||
); | ||
|
||
if (pickedDate != null && pickedDate != _selectedDate) { | ||
setState(() { | ||
_selectedDate = pickedDate; | ||
_dateController.text = DateFormat(widget.dateFormat).format(pickedDate); | ||
widget.dateSelected?.call(pickedDate); | ||
}); | ||
} | ||
} | ||
} |
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
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