Skip to content

Instantly share code, notes, and snippets.

@unlimitedprograming
Created August 12, 2021 17:45
Show Gist options
  • Save unlimitedprograming/109c0855d7467a508961a24b6f9271fa to your computer and use it in GitHub Desktop.
Save unlimitedprograming/109c0855d7467a508961a24b6f9271fa to your computer and use it in GitHub Desktop.
import 'dart:math';
import 'package:flutter/material.dart';
class SeekBar extends StatefulWidget {
final Duration duration;
final Duration position;
final ValueChanged<Duration> onChanged;
final ValueChanged<Duration> onChangeEnd;
SeekBar({
@required this.duration,
@required this.position,
this.onChanged,
this.onChangeEnd,
});
@override
_SeekBarState createState() => _SeekBarState();
}
class _SeekBarState extends State<SeekBar> {
double _dragValue;
bool _dragging = false;
@override
Widget build(BuildContext context) {
final value = min(_dragValue ?? widget.position.inMilliseconds.toDouble(),
widget.duration.inMilliseconds.toDouble());
if (_dragValue != null && !_dragging) {
_dragValue = null;
}
return Stack(
children: [
Slider(
min: 0.0,
max: widget.duration.inMilliseconds.toDouble(),
value: value,
onChanged: (value) {
if (!_dragging) {
_dragging = true;
}
setState(() {
_dragValue = value;
});
if (widget.onChanged != null) {
widget.onChanged(Duration(milliseconds: value.round()));
}
},
onChangeEnd: (value) {
if (widget.onChangeEnd != null) {
widget.onChangeEnd(Duration(milliseconds: value.round()));
}
_dragging = false;
},
),
Positioned(
right: 16.0,
bottom: 0.0,
child: Text(
RegExp(r'((^0*[1-9]\d*:)?\d{2}:\d{2})\.\d+$')
.firstMatch("$_remaining")
?.group(1) ??
'$_remaining',
style: TextStyle(
color: Color.fromARGB(255, 175, 175, 175)
),
)
),
],
);
}
Duration get _remaining => widget.duration - widget.position;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment