Skip to content

Commit f56fb55

Browse files
committed
fix: classtable toprow initalize
1 parent ee1eef9 commit f56fb55

File tree

6 files changed

+278
-319
lines changed

6 files changed

+278
-319
lines changed

lib/page/classtable/classtable_constant.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,5 @@ List<String> weekList = [
5151
'周六',
5252
'周日',
5353
];
54+
55+
String pageTitle = "我的课表";

lib/page/classtable/classtable_page.dart

Lines changed: 127 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,36 @@ See the License for the specific language governing permissions and
2020
limitations under the License.
2121
*/
2222

23+
import 'dart:developer';
2324
import 'dart:io';
2425

2526
import 'package:flutter/material.dart';
27+
import 'package:watermeter/page/classtable/class_table_view/class_table_view.dart';
2628
import 'package:watermeter/page/classtable/classtable_constant.dart';
2729
import 'package:watermeter/page/classtable/classtable_state.dart';
2830
import 'package:watermeter/page/classtable/not_arranged_class_list.dart';
29-
import 'package:watermeter/page/classtable/classtable_page_view.dart';
30-
import 'package:watermeter/page/classtable/week_choice_row.dart';
31+
import 'package:watermeter/page/classtable/week_choice_button.dart';
3132
import 'package:watermeter/repository/network_session.dart';
3233
import 'package:watermeter/repository/preference.dart' as preference;
3334

34-
/// The [ClassTablePage] contains [WeekChoiceRow] and [ClassTablePageView].
3535
class ClassTablePage extends StatefulWidget {
3636
const ClassTablePage({super.key});
3737

3838
@override
39-
State<ClassTablePage> createState() => _ClassTablePageState();
39+
State<StatefulWidget> createState() => _ClassTablePageState();
4040
}
4141

4242
class _ClassTablePageState extends State<ClassTablePage> {
43+
/// A lock of the week choice row.
44+
/// When locked, choiceWeek cannot be changed.
45+
bool isTopRowLocked = false;
46+
47+
/// Classtable pageView controller.
48+
late PageController pageControl;
49+
50+
/// Week choice row controller.
51+
late PageController rowControl;
52+
4353
late BoxDecoration decoration;
4454
late ClassTableState classTableState;
4555

@@ -56,13 +66,87 @@ class _ClassTablePageState extends State<ClassTablePage> {
5666
)
5767
: null,
5868
);
69+
5970
super.initState();
6071
}
6172

6273
@override
6374
void didChangeDependencies() {
64-
super.didChangeDependencies();
6575
classTableState = ClassTableState.of(context)!;
76+
log("Classtable state inited, ${classTableState.currentWeek}");
77+
pageControl = PageController(
78+
initialPage: classTableState.controllers.chosenWeek,
79+
keepPage: true,
80+
);
81+
82+
/// (weekButtonWidth + 2 * weekButtonHorizontalPadding)
83+
/// is the width of the week choose button.
84+
rowControl = PageController(
85+
initialPage: classTableState.controllers.chosenWeek,
86+
viewportFraction: (weekButtonWidth + 2 * weekButtonHorizontalPadding) /
87+
MediaQuery.sizeOf(context).width,
88+
keepPage: true,
89+
);
90+
91+
super.didChangeDependencies();
92+
}
93+
94+
/// Change the position in the topRow
95+
void changeTopRow(int index) => rowControl.animateTo(
96+
(weekButtonWidth + 2 * weekButtonHorizontalPadding) * index,
97+
curve: Curves.easeInOut,
98+
duration: const Duration(milliseconds: changePageTime),
99+
);
100+
101+
/// A row shows a series of buttons about the classtable's index.
102+
///
103+
/// This is at the top of the classtable. It contains a series of
104+
/// buttons which shows the week index, as well as an overview in a 5x5 dot gridview.
105+
///
106+
/// When user click on the button, the pageview will show the class table of the
107+
/// week the button suggested.
108+
Widget _topView() {
109+
return SizedBox(
110+
/// Related to the overview of the week.
111+
height: MediaQuery.sizeOf(context).height >= 500
112+
? topRowHeightBig
113+
: topRowHeightSmall,
114+
child: Container(
115+
padding: const EdgeInsets.only(
116+
top: 2,
117+
bottom: 4,
118+
),
119+
child: PageView.builder(
120+
controller: rowControl,
121+
physics: const ClampingScrollPhysics(),
122+
scrollDirection: Axis.horizontal,
123+
itemCount: classTableState.semesterLength,
124+
itemBuilder: (BuildContext context, int index) {
125+
return WeekChoiceButton(
126+
onTap: () {
127+
isTopRowLocked = true;
128+
129+
/// The following sequence is used when triggering changing page.
130+
/// * topRowLocked
131+
/// * change the chosen week
132+
/// * trigger pageview controller [pageControl] change, as well as
133+
/// * change the [WeekChoiceRow]
134+
classTableState.controllers.chosenWeek = index;
135+
pageControl.animateToPage(
136+
index,
137+
curve: Curves.easeInOutCubic,
138+
duration: const Duration(milliseconds: changePageTime),
139+
);
140+
changeTopRow(index);
141+
142+
setState(() {});
143+
},
144+
index: index,
145+
);
146+
},
147+
),
148+
),
149+
);
66150
}
67151

68152
/// If no class, a special page appears.
@@ -109,25 +193,31 @@ class _ClassTablePageState extends State<ClassTablePage> {
109193
? topRowHeightBig
110194
: topRowHeightSmall,
111195
),
112-
child: const WeekChoiceRow(),
196+
child: _topView(),
113197
),
114198
Expanded(
115199
child: DecoratedBox(
116200
decoration: decoration,
117-
child: const ClassTablePageView(),
201+
child: _classTablePage(),
118202
),
119203
),
120204
],
121205
)
122206
: Container(
123207
decoration: decoration,
208+
// color: Colors.grey.shade200.withOpacity(0.75),
124209
child: const Center(
125210
child: Column(
126211
mainAxisAlignment: MainAxisAlignment.center,
127212
children: [
128-
Icon(Icons.error, size: 100),
129-
SizedBox(height: 30),
130-
Text("本学期学期没有课程,不会吧?"),
213+
Icon(
214+
Icons.error,
215+
size: 100,
216+
),
217+
SizedBox(
218+
height: 30,
219+
),
220+
Text("这个学期没有课程,不会吧?"),
131221
Text("如果你没选课,快去 xk.xidian.edu.cn!"),
132222
Text("如果你要毕业了,祝你前程似锦。"),
133223
Text("如果你已经毕业,快去关注 SuperBart 哔哩哔哩帐号!"),
@@ -137,4 +227,31 @@ class _ClassTablePageState extends State<ClassTablePage> {
137227
),
138228
);
139229
}
230+
231+
/// The [_classTablePage] is controlled by [pageControl].
232+
Widget _classTablePage() => PageView.builder(
233+
scrollDirection: Axis.horizontal,
234+
controller: pageControl,
235+
onPageChanged: (value) {
236+
/// When [pageControl.animateTo] triggered,
237+
/// page view will try to refresh the [chosenWeek] everytime the page
238+
/// view changed into a new page. Because animateTo will load every page
239+
/// it passed.
240+
///
241+
/// So that's the [isTopRowLocked] is used for. When week choice row is
242+
/// locked, it will not refresh the [chosenWeek]. And when [chosenWeek]
243+
/// is equal to the current page, unlock the [isTopRowLocked].
244+
if (!isTopRowLocked) {
245+
setState(() {
246+
changeTopRow(value);
247+
classTableState.controllers.chosenWeek = value;
248+
});
249+
}
250+
if (classTableState.controllers.chosenWeek == value) {
251+
isTopRowLocked = false;
252+
}
253+
},
254+
itemCount: classTableState.semesterLength,
255+
itemBuilder: (context, index) => ClassTableView(index: index),
256+
);
140257
}

lib/page/classtable/classtable_page_view.dart

Lines changed: 0 additions & 72 deletions
This file was deleted.

lib/page/classtable/classtable_state.dart

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ See the License for the specific language governing permissions and
2020
limitations under the License.
2121
*/
2222

23+
import 'dart:developer';
24+
2325
import 'package:flutter/material.dart';
2426
import 'package:watermeter/model/xidian_ids/classtable.dart';
25-
import 'package:watermeter/page/classtable/classtable_constant.dart';
2627

2728
/// The controllers and shared datas of the class table.
2829
class ClassTableState extends InheritedWidget {
@@ -54,11 +55,6 @@ class ClassTableState extends InheritedWidget {
5455
/// The changeable data of the state.
5556
late final ClassTableWidgetState controllers;
5657

57-
/// Refrence [ClassTableWidgetState.isTopRowLocked]
58-
bool get isTopRowLocked => controllers.isTopRowLocked;
59-
set isTopRowLocked(bool status) =>
60-
status ? controllers.lockTopRow() : controllers.unlockTopRow();
61-
6258
ClassTableState({
6359
super.key,
6460
required super.child,
@@ -79,7 +75,10 @@ class ClassTableState extends InheritedWidget {
7975
} else {
8076
toShowChoiceWeek = currentWeek;
8177
}
82-
controllers = ClassTableWidgetState(choiceWeek: toShowChoiceWeek, totalWeek: semesterLength,);
78+
controllers = ClassTableWidgetState(
79+
chosenWeek: toShowChoiceWeek,
80+
);
81+
log("Chosen Week is ${controllers.chosenWeek}");
8382
}
8483

8584
static ClassTableState? of(BuildContext context) {
@@ -94,54 +93,10 @@ class ClassTableState extends InheritedWidget {
9493

9594
/// The changeable data of the class table state.
9695
class ClassTableWidgetState extends ChangeNotifier {
97-
/// Week choice row controller.
98-
late ScrollController rowControl;
99-
100-
/// Classtable pageView controller.
101-
late PageController pageControl;
102-
10396
/// Current showing week.
104-
late int chosenWeek;
105-
106-
/// From the length of the semester.
107-
final int totalWeek;
108-
109-
/// A lock of the week choice row.
110-
///
111-
/// When locked, choiceWeek cannot be changed.
112-
bool isTopRowLocked = false;
113-
114-
void changeTopRow(int index) => rowControl.animateTo(
115-
(weekButtonWidth + 2 * weekButtonHorizontalPadding) * index,
116-
curve: Curves.easeInOut,
117-
duration: const Duration(milliseconds: changePageTime ~/ 1.5),
118-
);
97+
int chosenWeek;
11998

12099
ClassTableWidgetState({
121-
required int choiceWeek,
122-
required this.totalWeek,
123-
}) {
124-
chosenWeek = choiceWeek;
125-
pageControl = PageController(
126-
initialPage: chosenWeek,
127-
keepPage: true,
128-
);
129-
130-
/// (weekButtonWidth + 2 * weekButtonHorizontalPadding)
131-
/// is the width of the week choose button.
132-
rowControl = ScrollController(
133-
initialScrollOffset:
134-
totalWeek - chosenWeek < 5 ? (weekButtonWidth + 2 * weekButtonHorizontalPadding) * (totalWeek - 5)
135-
:
136-
(weekButtonWidth + 2 * weekButtonHorizontalPadding) * chosenWeek,
137-
);
138-
}
139-
140-
void lockTopRow() => isTopRowLocked = true;
141-
142-
/// When top row unlocked, notify the week choice row to refresh the state.
143-
void unlockTopRow() {
144-
isTopRowLocked = false;
145-
notifyListeners();
146-
}
100+
required this.chosenWeek,
101+
});
147102
}

0 commit comments

Comments
 (0)