Hello guys I have a Listview.builder which display the week days, and I want to make this listview.builder start always from the current days which is highlighted.
This is my Code:
ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 7,
itemBuilder: (BuildContext context, int index) {
DateTime now = DateTime.now();
int milliseconds = now.millisecondsSinceEpoch -
(now.weekday - 1) * 24 * 60 * 60 * 1000 +
weekIndex * 7 * 24 * 60 * 60 * 1000 +
index * 24 * 60 * 60 * 1000;
DateTime dayDateTime =
DateTime.fromMillisecondsSinceEpoch(milliseconds);
int monthIndex = dayDateTime.month - 1;
List months = [
'Gennaio',
'Febbraio',
'Marzo',
'Aprile',
'Maggio',
'Giugno',
'Luglio',
'Agosto',
'Settembre',
'Ottobre',
'Novembre',
'Dicembre'
];
List weekdays = [
'Lunedì',
'Martedì',
'Mercoledì',
'Giovedì',
'Venerdì',
'Sabato',
'Domenica'
];
String month = months[monthIndex];
String day = dayDateTime.day.toString();
String weekday = weekdays[dayDateTime.weekday - 1];
String date = weekday + ' ' + day + ' ' + month;
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Column(
children: [
Container(
height: 50,
width: MediaQuery.of(context).size.width*0.50,
child: Card(
color: now.weekday == index + 1 && weekIndex == 0 ? Colors.orange[900]: Colors.orange,
child: Center(
child: Text(
date,
style: TextStyle(
fontSize: 13, color: Colors.white),
),
),
),
),
as you can see the current days is controlled by now.weekday == index + 1 && weekIndex == 0
I guess I need to add a controller to Listviewbuilder to achieve my purpose but I dont know how to do.
And I need also to consider when is Sunday it remaine just one day in the list.
Any Input?
question from:
https://stackoverflow.com/questions/65868635/how-to-make-listview-builder-start-from-the-current-day-in-flutter 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…