Skip to content

Commit

Permalink
添加课表页面显示星期几对应的日期
Browse files Browse the repository at this point in the history
  • Loading branch information
nano71 committed Dec 11, 2024
1 parent b1c3abe commit 35b83d8
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 28 deletions.
6 changes: 3 additions & 3 deletions android/local.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
sdk.dir=C:\\sdk\\android-sdk
flutter.sdk=C:\\sdk\\flutter
flutter.buildMode=debug
flutter.versionName=1.6.241125
flutter.versionCode=74
flutter.buildMode=release
flutter.versionName=1.6.241211
flutter.versionCode=75
1 change: 1 addition & 0 deletions lib/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class AppData {
"queryYear": "",
"threshold": "5",
"showLessonTimeInList": "0",
"showDayByWeekDay": "0",
"newVersion": "",
"newBody": "",
"newTime": "",
Expand Down
91 changes: 73 additions & 18 deletions lib/pages/schedule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import 'dart:core';

import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';

import '/common/io.dart';
import '/data.dart';
import '/widget/bars.dart';
import '/widget/dialog.dart';

import '../config.dart';

class RowHeader extends StatefulWidget {
Expand All @@ -18,14 +18,32 @@ class RowHeader extends StatefulWidget {
}

class RowHeaderState extends State<RowHeader> {
String _week = AppData.persistentData["week"] ?? "";
String _week = AppData.persistentData["week"]!;

@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
// decoration: ,
children: _loopRowHeader(AppData.persistentData["week"] == _week),
children: [
SizedBox(width: 20),
Expanded(
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: _loopRowHeader(AppData.persistentData["week"] == _week),
),
(AppData.persistentData["showDayByWeekDay"] ?? "0") == "1"
? Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: _loopRowHeader2(DateTime.now(), int.parse(_week)),
)
: Container(),
],
),
)
],
);
}

Expand All @@ -37,24 +55,61 @@ class RowHeaderState extends State<RowHeader> {
List<Widget> _loopRowHeader(bool currentWeek) {
List _weekDayList = ["一", "二", "三", "四", "五", "六", "日"];
List<Widget> list = [];
for (int i = 0; i < 8; i++) {
if (i == 0) {
list.add(SizedBox(width: 20));
} else {
list.add(
Expanded(
child: Container(
height: 30,
child: Center(
child: Text(
"周${_weekDayList[i - 1]}",
style: TextStyle(color: currentWeek ? (i == DateTime.now().weekday ? readColor() : Colors.grey) : Colors.grey),
),
for (int i = 0; i < 7; i++) {
list.add(
Expanded(
child: Container(
height: 30,
child: Center(
child: Text(
"周${_weekDayList[i]}",
style: TextStyle(color: currentWeek ? (i == DateTime.now().weekday ? readColor() : Colors.grey) : Colors.grey),
),
),
),
);
}
),
);
}
return list;
}

List<int> fillWeekWithWeekOffset(DateTime today, int difference) {
// 获取今天是星期几,1是周一,7是周日
int currentDayOfWeek = today.weekday;

// 计算今天所在周的周一日期
DateTime startOfWeek = today.subtract(Duration(days: currentDayOfWeek - 1));

// 通过偏移量计算目标周的起始日期
DateTime targetWeekStart = startOfWeek.add(Duration(days: difference * 7));

// 生成目标周的日期天数,0:周一, 1:周二, ..., 6:周日
List<int> weekDays = List.generate(7, (index) {
return targetWeekStart.add(Duration(days: index)).day;
});

return weekDays;
}

List<Widget> _loopRowHeader2(DateTime current, int currentWeek) {
List<Widget> list = [];
int difference = currentWeek - int.parse(AppData.persistentData["week"]!);
List<int> weekDays = fillWeekWithWeekOffset(current, difference);
for (var value in weekDays) {
list.add(
Expanded(
child: Container(
// height: 30,
padding: EdgeInsets.fromLTRB(0, 0, 0, 5),
child: Center(
child: Text(
"$value",
style: TextStyle(color: Colors.grey),
),
),
),
),
);
}
return list;
}
Expand Down
57 changes: 57 additions & 0 deletions lib/pages/setting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,63 @@ class _SettingPageState extends State<SettingPage> with WidgetsBindingObserver {
})
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Icon(
FlutterRemix.calendar_2_line,
color: readColor(),
),
Container(
padding: EdgeInsets.fromLTRB(16, 14, 0, 14),
child: Text(
"课表日期",
style: TextStyle(fontSize: 16, color: Colors.black),
),
)
],
),
Builder(builder: (BuildContext context) {
return DropdownButton(
icon: Icon(
FlutterRemix.arrow_down_s_line,
size: 18,
),
enableFeedback: true,
// style: TextStyle(color: readColor()),
iconEnabledColor: readColor(),
elevation: 0,
hint: Text(
(AppData.persistentData["showDayByWeekDay"] ?? "0") == "1" ? "显示" : "隐藏",
style: TextStyle(color: readColor(), fontSize: 14),
),
items: [
DropdownMenuItem(
child: Text(
"显示",
style: TextStyle(fontSize: 14),
),
value: "1"),
DropdownMenuItem(
child: Text(
"隐藏",
style: TextStyle(fontSize: 14),
),
value: "0")
],
underline: Container(height: 0),
onChanged: (value) {
setState(() {
AppData.persistentData["showDayByWeekDay"] = value.toString();
});
writeConfig();
},
);
})
],
),
InkWell(
onTap: () {
Navigator.of(context).push(MaterialPageRoute(builder: (context) => TimeManagePage()));
Expand Down
35 changes: 29 additions & 6 deletions lib/widget/bars.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ SliverAppBar publicTopBarWithInfoIcon(String title, [dynamic inkWell = const Tex
children: [
Text(
title,
style: TextStyle(
color: color2
),
style: TextStyle(color: color2),
),
SizedBox(
width: 8,
),
SizedBox(width: 8,),
SizedBox(
width: 12,
height: 12,
Expand Down Expand Up @@ -127,7 +127,7 @@ class ScheduleTopBar extends StatefulWidget {
}

class ScheduleTopBarState extends State<ScheduleTopBar> {
String _week = AppData.persistentData["week"] ?? "";
String _week = AppData.persistentData["week"] ?? "1";

void back() {
ScaffoldMessenger.of(context).removeCurrentSnackBar();
Expand All @@ -140,6 +140,29 @@ class ScheduleTopBarState extends State<ScheduleTopBar> {
return "${d.year}-${d.month}-${d.day}";
}

List<String> months = [
'January', // 1月
'February', // 2月
'March', // 3月
'April', // 4月
'May', // 5月
'June', // 6月
'July', // 7月
'August', // 8月
'September', // 9月
'October', // 10月
'November', // 11月
'December' // 12月
];

String month() {
if ((AppData.persistentData["showDayByWeekDay"] ?? "0") == "1") {
int difference = int.parse(_week) - int.parse(AppData.persistentData["week"]!);
return months[DateTime.now().add(Duration(days: difference * 7)).month - 1] + ", ";
}
return "";
}

@override
Widget build(BuildContext context) {
return AppBar(
Expand All @@ -151,7 +174,7 @@ class ScheduleTopBarState extends State<ScheduleTopBar> {
children: [
InkWell(
child: Text(
"Week $_week",
month() + "Week $_week",
style: TextStyle(color: Colors.black),
),
onTap: () {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish_to: "https://pub.flutter-io.cn" # Remove this line if you wish to publis
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.6.241125+74
version: 1.6.241211+75

environment:
sdk: ">=2.14.0 <4.0.0"
Expand Down

0 comments on commit 35b83d8

Please sign in to comment.