有没有办法在飞镖中重构这些类似的功能?
Is there a way I could refactor these similar functions in dart?
我正在尝试使用 Flutter 中的提供者状态管理来设置变量的状态,因此当我在我的构建中调用提供者时,它将更改我的提供者中布尔值的状态 class .
现在的问题是提供程序中的大部分功能都在做基本相同的事情,因为它们只是将布尔值更改为相反的值。
我一直在寻找一种方法来缩短代码,因为这些函数基本上做同样的事情。我一直在思考整个事情,但我什么也想不出来,而且这种意大利面条般的代码让我的眼睛受伤:
class AlarmState extends ChangeNotifier {
var hour = 0;
var min = 0;
bool isWednesday = false;
bool isMonday = false;
bool isTuesday = false;
bool isThursday = false;
bool isFriday = false;
bool isSaturday = false;
bool isSunday = false;
bool alarmRing = false;
bool vibrate = false;
bool snooze = false;
List<AlarmObject> alarmList = [];
bool isAlarmOn = true;
void handleChangeHour(int index) {
hour = index;
notifyListeners();
}
void handleChangeMin(int index) {
min = index;
notifyListeners();
}
void changeMonday() {
isMonday = !isMonday;
notifyListeners();
}
void changeTuesday() {
isTuesday = !isTuesday;
notifyListeners();
}
void changeWednesday() {
isWednesday = !isWednesday;
notifyListeners();
}
void changeThursday() {
isThursday = !isThursday;
notifyListeners();
}
void changeFriday() {
isFriday = !isFriday;
notifyListeners();
}
void changeSaturday() {
isSaturday = !isSaturday;
notifyListeners();
}
void changeSunday() {
isSunday = !isSunday;
notifyListeners();
}
void handleAlarmRing(value) {
alarmRing = value;
notifyListeners();
}
void handleSnooze(value) {
snooze = value;
notifyListeners();
}
void handleVibrate(value) {
vibrate = value;
notifyListeners();
}
void handleIsAlarmOn(bool value) {
isAlarmOn = value;
print(isAlarmOn);
notifyListeners();
}
}
我建议使用枚举 Weekday
来表示日期,并使用 Set<Weekday>
来跟踪哪些日期是活跃的。然后,使用带有可选命名参数的单个函数来更新 AlarmState
.
最终结果可能如下所示:
enum Weekday {
monday,
tuesday,
wednesday,
thursday,
friday,
saturday,
sunday,
}
class AlarmState extends ChangeNotifier {
var hour = 0;
var min = 0;
Set<Weekday> activeDays = Set();
bool alarmRing = false;
bool vibrate = false;
bool snooze = false;
List<AlarmObject> alarmList = [];
bool isAlarmOn = true;
void update({
int? hour,
int? min,
Set<Weekday>? activeDays,
bool? alarmRing,
bool? vibrate,
bool? snooze,
bool? isAlarmOn,
}) {
this.hour = hour ?? this.hour;
this.min = min ?? this.min;
this.activeDays = activeDays ?? this.activeDays;
this.alarmRing = alarmRing ?? this.alarmRing;
this.vibrate = vibrate ?? this.vibrate;
this.snooze = snooze ?? this.snooze;
this.isAlarmOn = isAlarmOn ?? this.isAlarmOn;
notifyListeners();
}
}
那么,对于更新闹钟的几个例子,它应该看起来像这样:
final alarmState = AlarmState();
// Add monday, wednesday and friday
alarmState.update(
activeDays: alarmState.activeDays..addAll([
Weekday.monday,
Weekday.wednesday,
Weekday.friday,
]),
);
// Remove monday
alarmState.update(
activeDays: alarmState.activeDays..remove(Weekday.monday),
);
// Update hour and minute
alarmState.update(
hour: 12,
min: 30,
);
// Turn off the alarm
alarmState.update(
isAlarmOn: false,
);
// Check whether the alarm should go off on monday
final isActiveOnMonday = alarmState.activeDays.contains(Weekday.monday);
我正在尝试使用 Flutter 中的提供者状态管理来设置变量的状态,因此当我在我的构建中调用提供者时,它将更改我的提供者中布尔值的状态 class .
现在的问题是提供程序中的大部分功能都在做基本相同的事情,因为它们只是将布尔值更改为相反的值。
我一直在寻找一种方法来缩短代码,因为这些函数基本上做同样的事情。我一直在思考整个事情,但我什么也想不出来,而且这种意大利面条般的代码让我的眼睛受伤:
class AlarmState extends ChangeNotifier {
var hour = 0;
var min = 0;
bool isWednesday = false;
bool isMonday = false;
bool isTuesday = false;
bool isThursday = false;
bool isFriday = false;
bool isSaturday = false;
bool isSunday = false;
bool alarmRing = false;
bool vibrate = false;
bool snooze = false;
List<AlarmObject> alarmList = [];
bool isAlarmOn = true;
void handleChangeHour(int index) {
hour = index;
notifyListeners();
}
void handleChangeMin(int index) {
min = index;
notifyListeners();
}
void changeMonday() {
isMonday = !isMonday;
notifyListeners();
}
void changeTuesday() {
isTuesday = !isTuesday;
notifyListeners();
}
void changeWednesday() {
isWednesday = !isWednesday;
notifyListeners();
}
void changeThursday() {
isThursday = !isThursday;
notifyListeners();
}
void changeFriday() {
isFriday = !isFriday;
notifyListeners();
}
void changeSaturday() {
isSaturday = !isSaturday;
notifyListeners();
}
void changeSunday() {
isSunday = !isSunday;
notifyListeners();
}
void handleAlarmRing(value) {
alarmRing = value;
notifyListeners();
}
void handleSnooze(value) {
snooze = value;
notifyListeners();
}
void handleVibrate(value) {
vibrate = value;
notifyListeners();
}
void handleIsAlarmOn(bool value) {
isAlarmOn = value;
print(isAlarmOn);
notifyListeners();
}
}
我建议使用枚举 Weekday
来表示日期,并使用 Set<Weekday>
来跟踪哪些日期是活跃的。然后,使用带有可选命名参数的单个函数来更新 AlarmState
.
最终结果可能如下所示:
enum Weekday {
monday,
tuesday,
wednesday,
thursday,
friday,
saturday,
sunday,
}
class AlarmState extends ChangeNotifier {
var hour = 0;
var min = 0;
Set<Weekday> activeDays = Set();
bool alarmRing = false;
bool vibrate = false;
bool snooze = false;
List<AlarmObject> alarmList = [];
bool isAlarmOn = true;
void update({
int? hour,
int? min,
Set<Weekday>? activeDays,
bool? alarmRing,
bool? vibrate,
bool? snooze,
bool? isAlarmOn,
}) {
this.hour = hour ?? this.hour;
this.min = min ?? this.min;
this.activeDays = activeDays ?? this.activeDays;
this.alarmRing = alarmRing ?? this.alarmRing;
this.vibrate = vibrate ?? this.vibrate;
this.snooze = snooze ?? this.snooze;
this.isAlarmOn = isAlarmOn ?? this.isAlarmOn;
notifyListeners();
}
}
那么,对于更新闹钟的几个例子,它应该看起来像这样:
final alarmState = AlarmState();
// Add monday, wednesday and friday
alarmState.update(
activeDays: alarmState.activeDays..addAll([
Weekday.monday,
Weekday.wednesday,
Weekday.friday,
]),
);
// Remove monday
alarmState.update(
activeDays: alarmState.activeDays..remove(Weekday.monday),
);
// Update hour and minute
alarmState.update(
hour: 12,
min: 30,
);
// Turn off the alarm
alarmState.update(
isAlarmOn: false,
);
// Check whether the alarm should go off on monday
final isActiveOnMonday = alarmState.activeDays.contains(Weekday.monday);