Skip to content

Instantly share code, notes, and snippets.

@Emex4gman
Created May 5, 2021 19:04
Show Gist options
  • Save Emex4gman/ffe30c85fffe84c9098d530c3fc40b19 to your computer and use it in GitHub Desktop.
Save Emex4gman/ffe30c85fffe84c9098d530c3fc40b19 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
class DatePicker extends StatefulWidget {
final TextEditingController dateCtr;
DatePicker({required this.dateCtr});
@override
_DatePickerState createState() => _DatePickerState();
}
class _DatePickerState extends State<DatePicker> {
FocusNode _node = FocusNode();
Color _iocnColor = Colors.purple;
@override
void initState() {
super.initState();
_node.addListener(() {
if (_node.hasFocus) {
_iocnColor = Colors.purple;
} else {
_iocnColor = Colors.grey;
}
setState(() {});
});
}
@override
Widget build(BuildContext context) {
return Container(
height: 50,
decoration: BoxDecoration(border: Border.all(color: Colors.grey), borderRadius: BorderRadius.circular(5)),
child: GestureDetector(
onTap: _datePicker,
child: Row(
children: [
Expanded(
child: TextFormField(
enabled: false,
focusNode: _node,
controller: widget.dateCtr,
decoration: InputDecoration(
hintText: "dd/mm/yyyy",
border: InputBorder.none,
),
)),
Container(
padding: const EdgeInsets.all(10),
height: double.infinity,
color: Colors.grey[300],
child: Center(
child: Icon(
Icons.calendar_today_outlined,
color: _iocnColor,
),
),
),
],
),
),
);
}
void _datePicker() async {
var date = await showDatePicker(context: context, initialDate: DateTime.now(), firstDate: DateTime.parse("1900-01-10"), lastDate: DateTime.parse("2030-01-10"));
if (date != null) {
widget.dateCtr.text = "${date.day}/${date.month}/${date.year}";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment