'How can i show previous 30 days in my list calendar?
I have a calendar, that works correct but shows only current day and future. How can i set previous days? I'd like to highliht a current day but also give an opportunity to scroll it to right and left. Maybe have your already faced to this problem. Help me please.
This is a whole class, you can try it
class _MyHomePageState extends State<MyHomePage> {
int currentDateSelectedIndex = 0; //For Horizontal Date
DateTime selectedDate = DateTime.now(); // TO tracking date
late var selectedDateNew;
ScrollController scrollController = ScrollController(); //Scroll Controller for ListView
List<String> listOfMonths = [
"January","February","March","April","May","June","July","August","September","October","November","December"
];
@override
void initState() {
selectedDate = DateTime.now().subtract(Duration(days: 30));
selectedDateNew = formatDate(selectedDate, [yyyy, '-', mm, '-', dd]);
super.initState();
}
@override
Widget build(BuildContext context) {
final FixedExtentScrollController itemController = FixedExtentScrollController();
return Scaffold(
body: Center(
child: calendar_carousel(),
)
);
}
Widget calendar_carousel() {
return Container(
height: 100,
child: ScrollSnapList(
padding: EdgeInsets.zero,
itemBuilder: _buildDaysList,
allowAnotherDirection: true,
itemSize: 196,
initialIndex: 0, //i tryed to put here another number, i didn't work
dynamicItemSize: true,
selectedItemAnchor: SelectedItemAnchor.MIDDLE,
onReachEnd: (){
print('Done!');
},
itemCount: 365,
onItemFocus: (int) {
setState(() {
scrollController.animateTo(0, duration: Duration(days: int), curve: Curves.bounceIn);
currentDateSelectedIndex = int;
selectedDate = DateTime.now().add(Duration(days: int));
selectedDateNew = formatDate(selectedDate, [yyyy, '-', mm, '-', dd]);
//fetch(selectedDateNew);
});
},
)
);
}
Widget _buildDaysList(BuildContext context, int index){
return Container(
child: Container(
color: Colors.blue,
width: 196,
height: 80,
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(DateTime.now().add(Duration(days: index)).day.toString()),
SizedBox(
width: 10,
),
Text(listOfMonths[DateTime.now().add(Duration(days: index)).month - 1].toString(),
),
],
),
),
),
);
}
}
i used dependensies
- scroll_snap_list: ^0.8.6
- date_format: ^2.0.6
Solution 1:[1]
Have you tried using.
var lastMonth = new DateTime(date.year, date.month - 1, date.day);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | rootShiv |