【问题标题】:Convert Milliseconds to Duration to String?将毫秒转换为持续时间到字符串?
【发布时间】:2021-08-07 01:15:59
【问题描述】:

我有一个 min 和 max 参数,它们都需要一个双精度参数。

  min: 0, 
  max: _duration!.inMilliseconds

然后将这些参数转换为字符串,用作滑块拇指球顶部的动态文本。

但是对于max 值,我想使用实际的持续时间;

IE:1:34:30 而不是 94,500。

所需的最终产品是一个字符串。

那么如何在保留 Duration 格式的同时将 Duration 转换为字符串?

请记住,它最初是作为双精度传递的。

这是拇指球的完整课程;

class CustomSliderThumbCircle extends SliderComponentShape {
  final double thumbRadius;
  final int min;
  final int max;

  const CustomSliderThumbCircle({
    required this.thumbRadius,
    this.min = 0,
    this.max = 10,
  });

  @override
  Size getPreferredSize(bool isEnabled, bool isDiscrete) {
    return Size.fromRadius(thumbRadius);
  }

  @override
  void paint(
    PaintingContext context,
    Offset center, {
    Animation<double>? activationAnimation,
    Animation<double>? enableAnimation,
    bool? isDiscrete,
    TextPainter? labelPainter,
    RenderBox? parentBox,
    SliderThemeData? sliderTheme,
    TextDirection? textDirection,
    double? value,
    double? textScaleFactor,
    Size? sizeWithOverflow,
  }) {
    final Canvas canvas = context.canvas;

    final paint = Paint()
      ..color = Colors.white //Thumb Background Color
      ..style = PaintingStyle.fill;

    TextSpan span = new TextSpan(
      style: new TextStyle(
        fontSize: thumbRadius,
        //fontWeight: FontWeight.w700,
        color: Colors.white, //Text Color of Value on Thumb
      ),
      text: getValue(value!),
    );

    TextPainter tp = new TextPainter(
        text: span,
        textAlign: TextAlign.center,
        textDirection: TextDirection.ltr);
    tp.layout();
    Offset textCenter =
        Offset(center.dx - (tp.width / 2), center.dy - (tp.height / 2));

    final rect = Rect.fromCircle(center: center, radius: thumbRadius);

    final rrect = RRect.fromRectAndRadius(
      Rect.fromPoints(
        Offset(rect.left - 10, rect.top),
        Offset(rect.right + 10, rect.bottom),
      ),
      Radius.elliptical(20, 100),
    );

    final fillPaint = Paint()
      ..color = sliderTheme!.activeTrackColor!
      ..style = PaintingStyle.fill;

    final borderPaint = Paint()
      ..color = Colors.black
      ..strokeWidth = 2.8
      ..style = PaintingStyle.stroke;

    canvas.drawRRect(rrect, fillPaint);
    canvas.drawRRect(rrect, borderPaint);

    tp.paint(canvas, textCenter);
  }

  String getValue(double value) {
    return (min + (max - min) * value).round().toString();
  }
}

【问题讨论】:

    标签: string flutter dart double duration


    【解决方案1】:

    如果CustomSliderThumbCircle 是您自己的类,我会将其更改为采用回调函数将值格式化为String(如果未指定回调,则回退到.toString()),类似于:

    class CustomSliderThumbCircle extends SliderComponentShape {
      final String Function(int value)? formatValue;
      ...
    
      const CustomSliderThumbCircle({
        required this.thumbRadius,
        this.min = 0,
        this.max = 10,
        this.formatValue,
      });
    
    
      String getValue(double value) {
        var clamped = (min + (max - min) * value).round();
        return (formatValue == null) ? clamped.toString() : formatValue!(clamped);
      }
    }
    

    然后你的调用者可以传递一个回调:

    CustomSliderThumbCircle(
      thumbRadius: ...,
      ...,
      formatValue: (value) => Duration(milliseconds: value).toString(),
    )
    

    这还允许您使用将Durations 格式化为 1m34.5s 而不是 0:01:34.500000 的函数。我通常不喜欢用冒号表示持续时间,因为如果省略小数秒,它可能会模棱两可。例如,使用a different function to format Durations:

    CustomSliderThumbCircle(
      thumbRadius: ...,
      ...,
      formatValue: (value) => prettyDuration(Duration(milliseconds: value)),
    )
    

    如果CustomSliderThumbCircle 不在您的控制之下,那么您可以使用您自己的覆盖getValue 的类来扩展它。

    【讨论】:

    • 我不确定应该如何将该 formatValue papameter 传递给 CustomSliderThumbCircle。那行“formatValue = (value) => Duration(milliseconds: value).toString()”是否应该是 CustomSliderThumbCircle 的参数?
    • 对不起,我的意思是formatValue: (value) =&gt; ...。已更正。
    • 虽然格式很长,但几乎可以使用 0:00:00:8650。
    • @Meggy 正如我所说,我不喜欢Duration.toString 使用的格式。您可以轻松地将回调函数格式化为您想要的格式。
    • 有人在我打开的这个新问题中向我展示了如何正确格式化。如果您想将其添加到您的答案中,我会选择它。 stackoverflow.com/questions/68682573/…
    【解决方案2】:

    我怀疑这比 jamesdlin 的解决方案更好,但我不知道如何按照我希望使用回调的方式格式化它。另外,我意识到我只需要几秒钟和几毫秒,因为无论如何音频都限制在 60 秒。所以这就是我所做的;

    String getValue(double value) {
    double seconds = value * (max/1000);
    String secstring = secs.truncate().toString();
    var milliseconds = value * (max.remainder(1000));
    String millistring = milliseconds.truncate().toString();
    String stringDuration = secstring + ':' + millistring;
    return stringDuration ;
    

    }

    【讨论】:

    • 1.使用我的回答中的回调,您只需创建一个接受毫秒数的函数(它已经被限制为min 和max;您的答案在这里忽略min)并返回任何@987654325 @你想要的表示。 2. 用: 分隔秒和毫秒会很混乱。 3. 这似乎没有用0s 填充毫秒,所以我认为,比如说,3 毫秒会被报告为'0:3',这又会非常令人困惑。
    猜你喜欢
    • 2013-03-02
    • 2012-06-16
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    相关资源
    最近更新 更多