Dart 从当前时间开始到24小时后结束,我将如何抵消24小时的时间范围

Dart 从当前时间开始到24小时后结束,我将如何抵消24小时的时间范围,dart,flutter,Dart,Flutter,我目前有一个滑块,范围从0到24,有24个分区(即每个分区代表一小时) 我在下面写了一个方法,将滑块中的值转换为12小时时间格式,如下所示 String startingTimeDisplay() { if (_lowerValue > 12) { return (_lowerValue - 12.0).toStringAsFixed(0) + ':00 PM'; } else { return (_lowerValue).toStringAsFixed(0) + ':00

我目前有一个滑块,范围从0到24,有24个分区(即每个分区代表一小时)

我在下面写了一个方法,将滑块中的值转换为12小时时间格式,如下所示

String startingTimeDisplay() {
 if (_lowerValue > 12) {
  return (_lowerValue - 12.0).toStringAsFixed(0) + ':00 PM';
} else {
  return (_lowerValue).toStringAsFixed(0) + ':00 AM';
}}

String endingTimeDisplay() {
if (_upperValue > 12) {
  return (_upperValue - 12.0).toStringAsFixed(0) + ':00 PM';
} else {
  return (_upperValue).toStringAsFixed(0) + ':00 AM';
}}
上述结果产生了这样的结果

我可以使用
DateTime.now()
访问当前日期和时间


我试图将滑块的时间帧从当前时间开始偏移到24小时之后,例如,如果当前时间是下午3点,滑块应该从下午3点开始,到明天下午3点结束。有什么想法吗?

而不是硬编码可以使用
intl
DateFormat
格式。此代码:

import 'package:intl/intl.dart';

void main() {
  DateTime now = DateTime.now();
  print(now);
  DateTime nowRounded = DateTime(now.year, now.month, now.day, now.hour);
  DateTime tomorrowRounded =
      DateTime(now.year, now.month, now.day + 1, now.hour);
  print(nowRounded);
  print(tomorrowRounded);

  DateFormat usHour = DateFormat.jm();
  print(usHour.format(nowRounded));
  print(usHour.format(tomorrowRounded));
}
产生:

2018-12-31 21:13:54.434448
2018-12-31 21:00:00.000
2019-01-01 21:00:00.000
9:00 PM
9:00 PM

类主页扩展StatefulWidget{
@凌驾
HomePageState createState(){
返回新的HomePageState();
}
}
类HomePageState扩展了状态{
日期时间(星期四);
日期时间(hourOnly);;
var _低值;
var_值;
@凌驾
void initState(){
var_time=DateTime.now();
_hourOnly=日期时间(_time.year、_time.month、_time.day、_time.hour);
_plusDay=_hourOnly.add(持续时间(天:1));
_lowerValue=_hourOnly.millissecondssinceepoch.toDouble();
_upperValue=_plusDay.millissecondssinceepoch.toDouble();
super.initState();
}
@凌驾
小部件构建(构建上下文){
DateFormat usHour=DateFormat.jm();
返回脚手架(
正文:中(
子:容器(
孩子:填充(
填充:常数边集全部(8.0),
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
划船(
儿童:[
正文(
“${usHour.format(DateTime.fromMicrosecondsSincepoch(_lowerValue.toInt()*1000))”,
扩大(
孩子:新的范围滑块(
最小值:_hourOnly.millissecondssinceepoch.toDouble(),
max:_plusDay.millissecondssinceepoch.toDouble(),
低值:_低值,
upperValue:_upperValue,
分部:24,
showValueIndicator:false,
数值指标规格:1,
一旦更改:
(双newLowerValue,双NewUpper Value){
设置状态(){
_lowerValue=新的lowerValue;
_upperValue=新upperValue;
});
},
onChangeStart:
(双阈值,双阈值){
印刷品(
“以值开始:$startowervalue和$startUpperValue”);
},
onChangeEnd:
(双newLowerValue,双NewUpper Value){
印刷品(
'以值结尾:$newLowerValue和$newUpperValue');
},
),
),
正文(
“${usHour.format(DateTime.fromMicrosecondsSincepoch(_upperValue.toInt()*1000))},”,
],
),
//
],
),
),
),
),
);
}
}
用于将24小时转换为12小时

    class HomePage extends StatefulWidget {
  @override
  HomePageState createState() {
    return new HomePageState();
  }
}

class HomePageState extends State<HomePage> {
  DateTime _plusDay;
  DateTime _hourOnly;

  var _lowerValue;

  var _upperValue;

  @override
  void initState() {
    var _time = DateTime.now();
    _hourOnly = DateTime(_time.year, _time.month, _time.day, _time.hour);
    _plusDay = _hourOnly.add(Duration(days: 1));
    _lowerValue = _hourOnly.millisecondsSinceEpoch.toDouble();
    _upperValue = _plusDay.millisecondsSinceEpoch.toDouble();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    DateFormat usHour = DateFormat.jm();
    return Scaffold(
      body: Center(
        child: Container(
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Row(
                  children: <Widget>[
                    Text(
                        "${usHour.format(DateTime.fromMicrosecondsSinceEpoch(_lowerValue.toInt() * 1000))}"),
                    Expanded(
                      child: new RangeSlider(
                        min: _hourOnly.millisecondsSinceEpoch.toDouble(),
                        max: _plusDay.millisecondsSinceEpoch.toDouble(),
                        lowerValue: _lowerValue,
                        upperValue: _upperValue,
                        divisions: 24,
                        showValueIndicator: false,
                        valueIndicatorMaxDecimals: 1,
                        onChanged:
                            (double newLowerValue, double newUpperValue) {
                          setState(() {
                            _lowerValue = newLowerValue;
                            _upperValue = newUpperValue;
                          });
                        },
                        onChangeStart:
                            (double startLowerValue, double startUpperValue) {
                          print(
                              'Started with values: $startLowerValue and $startUpperValue');
                        },
                        onChangeEnd:
                            (double newLowerValue, double newUpperValue) {
                          print(
                              'Ended with values: $newLowerValue and $newUpperValue');
                        },
                      ),
                    ),
                    Text(
                        "${usHour.format(DateTime.fromMicrosecondsSinceEpoch(_upperValue.toInt() * 1000))}"),
                  ],
                ),
                //
              ],
            ),
          ),
        ),
      ),
    );
  }
}