gpt4 book ai didi

Flutter Flutter Datetime Picker 包生成 AM/PM?

转载 作者:行者123 更新时间:2023-12-03 22:06:28 29 4
gpt4 key购买 nike

有没有人使用 Flutter Datetime Picker ( enter link description here ) 以 12 小时格式成功制作了一个 TimePicker,其小时数:上午/下午的分钟数?对于我的生活,我无法使用 CustomPickerModel 让它工作。我只是在秒 View 中看到一堆 PM,当你移动它们时,打印的结果是几秒钟。

dependencies: flutter_datetime_picker: ^1.2.5


class CustomPicker extends CommonPickerModel {
String digits(int value, int length) {
return '$value'.padLeft(length, "0");
}

CustomPicker({DateTime currentTime, LocaleType locale})
: super(locale: locale) {
_fillRightLists();
this.currentTime = currentTime ?? DateTime.now();
this.setLeftIndex(this.currentTime.hour);
this.setMiddleIndex(this.currentTime.minute);
this.setRightIndex(0);
}

void _fillRightLists() {
this.rightList = List.generate(2, (int index) {
if (index == 0) {
return 'AM';
} else {
return 'PM';
}
});
}

@override
String leftStringAtIndex(int index) {
if (index >= 0 && index < 24) {
return this.digits(index, 2);
} else {
return null;
}
}

@override
String middleStringAtIndex(int index) {
if (index >= 0 && index < 60) {
return this.digits(index, 2);
} else {
return null;
}
}

@override
String rightStringAtIndex(int index) {
if (index == 0) {
return "AM";
} else {
return "PM";
}

// Orignal code
// if (index >= 0 && index < 60) {
// return this.digits(index, 2);
// } else {
// return null;
// }
}

@override
String leftDivider() {
return ":";
}

@override
String rightDivider() {
return " ";
}

@override
List<int> layoutProportions() {
return [1, 1, 1];
}

@override
DateTime finalTime() {
return currentTime.isUtc
? DateTime.utc(
currentTime.year,
currentTime.month,
currentTime.day,
this.currentLeftIndex(),
this.currentMiddleIndex(),
this.currentRightIndex())
: DateTime(
currentTime.year,
currentTime.month,
currentTime.day,
this.currentLeftIndex(),
this.currentMiddleIndex(),
this.currentRightIndex());
}
}

要显示它,请调用:
FlatButton(
onPressed: () {
DatePicker.showPicker(context,
showTitleActions: true,
onChanged: (date) {
print('change $date');
}, onConfirm: (date) {
print('confirm $date');
},
pickerModel: CustomPicker(
currentTime: DateTime.now(),
locale: LocaleType.en));
},
child: Text(
'show date time picker',
style: TextStyle(color: Colors.blue),
))

最佳答案

只需对代码进行一些更改,您就需要覆盖“setRightIndex”:

@override
void setRightIndex(int index) {
super.setRightIndex(index);
_fillRightList();
}

并更新'rightStringAtIndex',添加返回空值
@override
String rightStringAtIndex(int index) {
if (index == 0) {
return 'AM';
} else if (index == 1) {
return 'PM';
}
return null;
}

以及完整的代码
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';

class CustomPicker extends CommonPickerModel {
String digits(int value, int length) {
return '$value'.padLeft(length, "0");
}

CustomPicker({DateTime currentTime, LocaleType locale}) : super(locale: locale) {
this.currentTime = currentTime ?? DateTime.now();
var _dayPeriod = 0;
this.setLeftIndex(this.currentTime.hour);
this.setMiddleIndex(this.currentTime.minute);
this.setRightIndex(_dayPeriod);
_fillRightList();
}

@override
String leftStringAtIndex(int index) {
if (index >= 1 && index < 13) {
return this.digits(index, 2);
} else {
return null;
}
}

@override
String middleStringAtIndex(int index) {
if (index >= 0 && index < 60) {
return this.digits(index, 2);
} else {
return null;
}
}

@override
String rightStringAtIndex(int index) {
if (index == 0) {
return 'AM';
} else if (index == 1) {
return 'PM';
}
return null;
}

void _fillRightList() {
this.rightList = List.generate(2, (int index) {
return '$index';
});
}

@override
void setRightIndex(int index) {
super.setRightIndex(index);
_fillRightList();
}

@override
String leftDivider() {
return ":";
}

@override
String rightDivider() {
return " ";
}

@override
List<int> layoutProportions() {
return [1, 1, 1];
}

@override
DateTime finalTime() {
return currentTime.isUtc
? DateTime.utc(currentTime.year, currentTime.month, currentTime.day,
this.currentLeftIndex(), this.currentMiddleIndex(), this.currentRightIndex())
: DateTime(currentTime.year, currentTime.month, currentTime.day, this.currentLeftIndex(),
this.currentMiddleIndex(), this.currentRightIndex());
}
}

关于Flutter Flutter Datetime Picker 包生成 AM/PM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57016056/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com