Skip to content

Instantly share code, notes, and snippets.

@cankush625
Created March 13, 2021 16:57
Show Gist options
  • Save cankush625/14ac1fc0c4b660450366741641285aff to your computer and use it in GitHub Desktop.
Save cankush625/14ac1fc0c4b660450366741641285aff to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/rendering.dart';
import 'package:intl/intl.dart';
class DatePicker extends StatefulWidget {
@override
_DatePickerState createState() => _DatePickerState();
}
class _DatePickerState extends State<DatePicker> {
var fsconnect = FirebaseFirestore.instance;
DateTime selectedDate = DateTime.now();
var formatter = new DateFormat('dd-MM-yyyy');
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
backgroundColor: Colors.black26,
title: Text(
'Date Picker',
),
),
body: SafeArea(
child: GestureDetector(
onTap: () async {
DateTime date = await _selectDate(context);
fsconnect.collection("WorkoutDateWise").add({
"date": formatter
.format(date)
.toString(),
}).then((value) => print(value.id));
},
child: Card(
child: Text("hsdb sjfjdskjbf sb hfbjh bdfjbj"),
),
),
),
);
}
bool _decideWhichDayToEnable(DateTime day) {
if ((day.isAfter(DateTime.now().subtract(Duration(days: 1))) &&
day.isBefore(DateTime.now().add(Duration(days: 10))))) {
return true;
}
return false;
}
Future<DateTime> _selectDate(BuildContext context) async {
final DateTime picked = await showDatePicker(
context: context,
initialDate: selectedDate, // Refer step 1
firstDate: DateTime(2000),
lastDate: DateTime(2025),
selectableDayPredicate: _decideWhichDayToEnable,
);
if (picked != null && picked != selectedDate) {
selectedDate = picked;
print(formatter.format(selectedDate));
}
return selectedDate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment