From 84429f6c9de69c86a68b5cd3ff39b11b1092da40 Mon Sep 17 00:00:00 2001 From: Ibrahim Hassan Date: Tue, 16 Apr 2024 18:21:28 +0530 Subject: [PATCH] added datepicker --- example/lib/main.dart | 9 ++++ lib/components/datepicker/date_picker.dart | 62 ++++++++++++++++++++++ lib/tarka_ui.dart | 1 + pubspec.lock | 2 +- pubspec.yaml | 1 + 5 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 lib/components/datepicker/date_picker.dart diff --git a/example/lib/main.dart b/example/lib/main.dart index 18120b7..3443331 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1221,6 +1221,15 @@ class _HomePageState extends State { }, child: Text('Show Modal Bottom Sheet'), ), + const SizedBox(height: 8), + const Text("Date Picker", style: TUITextStyle.heading6), + const SizedBox(height: 8), + TUIDatePicker( + placeholder: "placeHolder", + dateSelected: (date) { + print(date); + }, + ), const SizedBox(height: 100), ], ), diff --git a/lib/components/datepicker/date_picker.dart b/lib/components/datepicker/date_picker.dart new file mode 100644 index 0000000..a8118cd --- /dev/null +++ b/lib/components/datepicker/date_picker.dart @@ -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 createState() => _TUIDatePickerState(); +} + +class _TUIDatePickerState extends State { + 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 _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); + }); + } + } +} diff --git a/lib/tarka_ui.dart b/lib/tarka_ui.dart index b461f30..c8a40e5 100644 --- a/lib/tarka_ui.dart +++ b/lib/tarka_ui.dart @@ -36,6 +36,7 @@ export 'package:tarka_ui/components/breadcrumb/breadcrumb.dart'; export 'package:tarka_ui/components/button/button.dart'; export 'package:tarka_ui/components/button/icon_button.dart'; export 'package:tarka_ui/components/checkbox/checkbox.dart'; +export 'package:tarka_ui/components/datepicker/date_picker.dart'; export 'package:tarka_ui/components/divider/divider.dart'; export 'package:tarka_ui/components/email_subject_field/email_subject_field.dart'; export 'package:tarka_ui/components/floating_action_button/floating_action_button.dart'; diff --git a/pubspec.lock b/pubspec.lock index cb7a03c..3fae536 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -89,7 +89,7 @@ packages: source: hosted version: "0.15.0" intl: - dependency: transitive + dependency: "direct main" description: name: intl sha256: "3bc132a9dbce73a7e4a21a17d06e1878839ffbf975568bc875c60537824b0c4d" diff --git a/pubspec.yaml b/pubspec.yaml index a0603ea..db01f9a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,6 +15,7 @@ dependencies: flutter_localizations: sdk: flutter fluentui_system_icons: ^1.1.226 + intl: ^0.18.1 dev_dependencies: flutter_test: