Skip to content

Instantly share code, notes, and snippets.

@sooorajjj
Created July 31, 2020 08:29
Show Gist options
  • Save sooorajjj/e4197bf885f9f0cdf3109bb9dc2337e6 to your computer and use it in GitHub Desktop.
Save sooorajjj/e4197bf885f9f0cdf3109bb9dc2337e6 to your computer and use it in GitHub Desktop.
reverse datepicker in Flutter
import 'package:flutter/material.dart';
import 'package:date_picker_timeline/date_picker_timeline.dart';
import 'package:route_map/constants.dart';
class HistoryPage extends StatefulWidget {
@override
_HistoryPageState createState() => _HistoryPageState();
}
class _HistoryPageState extends State<HistoryPage> {
final startDate = DateTime(2020, 07, 14);
DateTime _selectedValue = DateTime.now();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("History"),
),
body: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
child: DatePicker(
DateTime.now()
.subtract(Duration(days: _calculateDays(startDate))),
daysCount: _calculateDays(startDate) + 1,
width: 60,
height: 80,
initialSelectedDate: DateTime.now(),
selectionColor: kPrimaryColor,
selectedTextColor: Colors.white,
onDateChange: (date) {
// New date selected
setState(() {
_selectedValue = date;
});
},
),
),
],
),
));
}
int _calculateDays(var startDate) {
final today = DateTime.now();
final difference = today.difference(startDate).inDays;
print(difference);
return difference;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment