Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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