DateFormat 在颤动中显示工作日的错误输出

问题描述

我在网上搜索类似下面链接的类似问题

github issue link

此人关闭了问题,我在其他任何地方都找不到类似的问题。

就我而言,我正在制作如下所示的水平日历

enter image description here

这是当前周,但在 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-137:10-35 01-01 13:55:50.903787,2021-01-02 13:55:50.903787,2021-01-03 13:55:50.903787,2021-01-04 13,903-55:15:15:05:20 :50.903787,2021-01-06 13:55:50.903787,2021-01-07 13:55:50.903787,2021-01-08 13:55:50.903787-05-3787-05-05-09-07-05 -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),)
        ],);
  }
}

class DateHolder extends StatelessWidget {
  static MyInheritedWidget of(BuildContext context) => context.dependOnInheritedWidgetofExactType();

  DateHolder(this.index,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,bothWeek[index].weekday)).substring(0,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(),);
  }
}

解决方法

2021-12-28 是星期一。我认为您的问题与您在工作日首字母下方显示的数字有关,因为根据您描述的日期,这些数字是正确的

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...