【发布时间】:2021-01-24 08:50:48
【问题描述】:
大家好,我有一个显示工作日的 Listview.builder,我想让这个 listview.builder 始终从突出显示的当前日期开始。 这是我的代码:
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),
),
),
),
),
如您所见,当前日期由now.weekday == index + 1 && weekIndex == 0控制
我想我需要向 Listviewbuilder 添加一个控制器来实现我的目的,但我不知道该怎么做。 我还需要考虑星期天什么时候在列表中只剩下一天。 任何输入?
【问题讨论】:
标签: flutter