Skip to content

Commit

Permalink
added datepicker
Browse files Browse the repository at this point in the history
  • Loading branch information
Ibrahimhass committed Apr 16, 2024
1 parent 3a4c4bf commit 84429f6
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
9 changes: 9 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,15 @@ class _HomePageState extends State<HomePage> {
},
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),
],
),
Expand Down
62 changes: 62 additions & 0 deletions lib/components/datepicker/date_picker.dart
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);
});
}
}
}
1 change: 1 addition & 0 deletions lib/tarka_ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ packages:
source: hosted
version: "0.15.0"
intl:
dependency: transitive
dependency: "direct main"
description:
name: intl
sha256: "3bc132a9dbce73a7e4a21a17d06e1878839ffbf975568bc875c60537824b0c4d"
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies:
flutter_localizations:
sdk: flutter
fluentui_system_icons: ^1.1.226
intl: ^0.18.1

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 84429f6

Please sign in to comment.