我在网上搜索类似的问题,如下面的链接
在这里,这个人关闭了这个问题,我在其他任何地方都找不到类似的问题。
这是当前的一周,但在 1 月 6 日这里显示的是星期日,实际上是星期三,我也在使用 DateFormat('EEEE').format(DateTime(................ .))
所以我认为解决上述问题也将解决我的问题。
我的代码:
"${DateFormat('EEEE').format(DateTime(appState.date.year, appState.date.month,currentWeek[index].weekday)).substring(0, 1)}",
本周在哪里
[2020-12-28 13:55:50.903787, 2020-12-29 13:55:50.903787, 2020-12-30 13:55:50.903787, 2020-12-31 13:55:50.903787, 2021-01-01 13:55:50.903787, 2021-01-02 13:55:50.903787, 2021-01-03 13:55:50.903787, 2021-01-04 13:55:50.903787, 2021-01-05 13:55:50.90378 2021-01-06 13:55:50.903787, 2021-01-07 13:55:50.903787, 2021-01-08 13:55:50.903787, 2021-01-09 13:55:50.903787, 2021-01-10 13 :55:50.903787]
这对我来说似乎是正确的。此列表包含当前和前一周的数据
编辑:
class MyInheritedWidget extends InheritedWidget {
final DateTime date;
final List<DateTime> bothWeek;
final DateTime currentdate;
final DateTime currentWeekFirstDay;
final DateTime currentWeekLastDay;
final DateTime previousWeekFirstDay;
final DateTime previousWeekLastDay;
final int selectedDay;
final int monthDateCount;
final bool isDateHolderActive;
final Map<int, bool> dayAvailabilityMap;
final ValueChanged<bool> toggleDateHolderActive;
final ValueChanged<int> setSelectedDay;
MyInheritedWidget({
Key key,
this.date,
this.currentdate,
this.bothWeek,
this.currentWeekLastDay,
this.currentWeekFirstDay,
this.previousWeekFirstDay,
this.previousWeekLastDay,
this.selectedDay,
this.monthDateCount,
this.isDateHolderActive,
this.dayAvailabilityMap,
this.toggleDateHolderActive,
this.setSelectedDay,
Widget child,
}) : super(key: key, child: child);
@override
bool updateShouldNotify(MyInheritedWidget oldWidget) {
return oldWidget.selectedDay != selectedDay ||
oldWidget.toggleDateHolderActive != toggleDateHolderActive;
}
}
class DateIndicatorPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: <Widget>[
DateIndicator(),
Expanded(
child: Container(),
),
],
),
),
);
}
}
class DateIndicator extends StatefulWidget {
static MyInheritedWidget of(BuildContext context) => context.dependOnInheritedWidgetOfExactType();
@override
_DateIndicatorState createState() => _DateIndicatorState();
}
class _DateIndicatorState extends State<DateIndicator> {
DateTime date = DateTime.now();
DateTime currentdate=DateTime.now();
List<DateTime> bothWeek=[];
DateTime currentWeekFirstDay;
DateTime currentWeekLastDay;
DateTime previousWeekFirstDay;
DateTime previousWeekLastDay;
int selectedDay = 1;
int monthDateCount = 1;
bool isDateHolderActive = false;
bool _isButtonDisabled;
void toggleDateHolderActive(bool flag) {
setState(() {
isDateHolderActive = flag;
});
}
void setSelectedDay(int index) {
setState(() {
selectedDay = index;
});
}
DateTime findFirstDateOfTheWeek(DateTime dateTime) {
return dateTime.subtract(Duration(days: dateTime.weekday - 1));
}
DateTime findLastDateOfTheWeek(DateTime dateTime) {
return dateTime.add(Duration(days: DateTime.daysPerWeek - dateTime.weekday));
}
DateTime findFirstDateOfPreviousWeek(DateTime dateTime) {
final DateTime sameWeekDayOfLastWeek =
dateTime.subtract(const Duration(days: 7));
return findFirstDateOfTheWeek(sameWeekDayOfLastWeek);
}
DateTime findLastDateOfPreviousWeek(DateTime dateTime) {
final DateTime sameWeekDayOfLastWeek =
dateTime.subtract(const Duration(days: 7));
return findLastDateOfTheWeek(sameWeekDayOfLastWeek);
}
void getDaysInBetweenBothWeek(DateTime startDate, DateTime endDate) {
for (int i = 0; i <= endDate.difference(startDate).inDays; i++) {
bothWeek.add(startDate.add(Duration(days: i)));
}
}
@override
void initState() {
_isButtonDisabled=false;
final DateTime dateForValues =DateTime(date.year, date.month+1, 0);
monthDateCount = dateForValues.day;
print("month $monthDateCount");
super.initState();
}
@override
Widget build(BuildContext context) {
currentWeekFirstDay=findFirstDateOfTheWeek(currentdate);
currentWeekLastDay=findLastDateOfTheWeek(currentdate);
previousWeekFirstDay=findFirstDateOfPreviousWeek(currentdate);
previousWeekLastDay=findLastDateOfPreviousWeek(currentdate);
getDaysInBetweenBothWeek(previousWeekFirstDay, currentWeekLastDay);
print("dates $bothWeek");
return Container(
margin: EdgeInsets.only(top: 150),
child: Column(
children: [
IconButton(
icon:Icon(Icons.chevron_right),
color: Colors.black,
),
Container(
width: MediaQuery.of(context).size.width,
height: 68.0,
padding:
const EdgeInsets.only(left: 7.0, right: 3.0, top: 2.0, bottom: 2.0),
decoration: BoxDecoration(
color: Theme.of(context).secondaryHeaderColor,
boxShadow: [
BoxShadow(
color: Colors.blueAccent.withOpacity(.7),
offset: Offset(0.0, .5),
blurRadius: 3.0,
spreadRadius: 0.3),
],
),
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 14,
itemBuilder: (BuildContext context, int index) {
return MyInheritedWidget(
date: date,
currentWeekFirstDay: currentWeekFirstDay,
currentWeekLastDay: currentWeekLastDay,
previousWeekFirstDay: previousWeekFirstDay,
previousWeekLastDay: previousWeekLastDay,
currentdate: currentdate,
selectedDay: selectedDay,
bothWeek: bothWeek,
monthDateCount: monthDateCount,
isDateHolderActive: isDateHolderActive,
toggleDateHolderActive: toggleDateHolderActive,
setSelectedDay: setSelectedDay,
child: DateHolder(index,currentdate,date,currentWeekLastDay,currentWeekFirstDay,bothWeek,previousWeekFirstDay,previousWeekLastDay));
}),
),
IconButton(
icon:Icon(Icons.chevron_left),
color: Colors.black,
)
],
),
);
}
}
class DateHolder extends StatelessWidget {
static MyInheritedWidget of(BuildContext context) => context.dependOnInheritedWidgetOfExactType();
DateHolder(this.index,this.currentdate,this.date,this.currentWeekLastDay,this.currentWeekFirstDay,this.bothWeek,this.previousWeekFirstDay,this.previousWeekLastDay);
final int index;
final DateTime currentWeekFirstDay;
final List<DateTime> bothWeek;
final DateTime currentWeekLastDay;
final DateTime previousWeekFirstDay;
final DateTime previousWeekLastDay;
final DateTime date;
final DateTime currentdate;
final Widget activeBubble = Container(
width: 15.0,
height: 15.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.deepOrangeAccent,
),
);
@override
Widget build(BuildContext context) {
bool isSelectedDate = date.compareTo(currentdate) == 0;
final appState = DateIndicator.of(context);
print("current week days:${bothWeek[index].weekday}");
return InkWell(
onTap: () {
appState.toggleDateHolderActive(true);
appState.setSelectedDay(index);
print("Date $index selected!");
},
child: Stack(
children: <Widget>[
Column(
children: <Widget>[
Container(
margin: const EdgeInsets.only(right: 5.0),
child: Text(
///? TO CHECK
"${DateFormat('EEEE').format(DateTime(appState.date.year, appState.date.month,bothWeek[index].weekday)).substring(0, 1)}",
style: TextStyle(color: Theme.of(context).primaryColor, fontSize: 12.0),
)),
Container(
width: 45.0,
height: 45.0,
margin: const EdgeInsets.only(right: 5.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: !isSelectedDate? Colors.orange:Colors.white,
border: (index == (appState.selectedDay) &&
appState.isDateHolderActive == true)
? Border.all(width: 2.0, color: Theme.of(context).primaryColor)
: Border.all(color: Colors.transparent),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: Text(
"${bothWeek[index].day}", // to avoid showing zero
style: TextStyle(color: Theme.of(context).primaryColor, fontSize: 16.0),
),
),
),
),
],
),
(appState.dayAvailabilityMap[index] ?? false)
? Positioned(right: 8.0, bottom: 5.0, child: activeBubble)
: Container(),
],
),
);
}
}