mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-16 05:13:41 +00:00
fix(ui): smooth task progress bar updates (#1532)
This commit is contained in:
1 file changed
+63
-54
@@ -1,3 +1,4 @@
|
|||||||
|
import 'package:shadcn_flutter/shadcn_flutter.dart' show kDefaultDuration;
|
||||||
import 'package:flutter/widgets.dart';
|
import 'package:flutter/widgets.dart';
|
||||||
|
|
||||||
class TaskProgressBar extends StatefulWidget {
|
class TaskProgressBar extends StatefulWidget {
|
||||||
@@ -74,68 +75,76 @@ class _TaskProgressBarState extends State<TaskProgressBar> with SingleTickerProv
|
|||||||
color: widget.trackColor,
|
color: widget.trackColor,
|
||||||
child: LayoutBuilder(
|
child: LayoutBuilder(
|
||||||
builder: (context, constraints) {
|
builder: (context, constraints) {
|
||||||
return AnimatedBuilder(
|
final progress = (widget.value ?? 0).clamp(0.0, 1.0);
|
||||||
animation: _controller,
|
return TweenAnimationBuilder<double>(
|
||||||
builder: (context, child) {
|
tween: Tween<double>(begin: progress, end: progress),
|
||||||
if (widget.indeterminate) {
|
duration: MediaQuery.disableAnimationsOf(context) || widget.indeterminate
|
||||||
final segmentWidth = constraints.maxWidth * 0.4;
|
? Duration.zero
|
||||||
final left = lerpDouble(-segmentWidth, constraints.maxWidth, _controller.value);
|
: kDefaultDuration,
|
||||||
|
curve: Curves.easeInOut,
|
||||||
|
builder: (context, animatedProgress, child) => AnimatedBuilder(
|
||||||
|
animation: _controller,
|
||||||
|
builder: (context, child) {
|
||||||
|
if (widget.indeterminate) {
|
||||||
|
final segmentWidth = constraints.maxWidth * 0.4;
|
||||||
|
final left = lerpDouble(-segmentWidth, constraints.maxWidth, _controller.value);
|
||||||
|
|
||||||
|
return Stack(
|
||||||
|
fit: StackFit.expand,
|
||||||
|
children: [
|
||||||
|
Positioned(
|
||||||
|
left: left,
|
||||||
|
width: segmentWidth,
|
||||||
|
top: 0,
|
||||||
|
bottom: 0,
|
||||||
|
child: _ProgressFill(
|
||||||
|
color: widget.fillColor,
|
||||||
|
shimmer: widget.shimmer,
|
||||||
|
highlightStartColor: widget.highlightStartColor,
|
||||||
|
highlightEndColor: widget.highlightEndColor,
|
||||||
|
bandLeft: lerpDouble(-segmentWidth, segmentWidth * 2, _controller.value),
|
||||||
|
bandWidth: segmentWidth * 0.5,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final fillWidth = constraints.maxWidth * animatedProgress;
|
||||||
|
final bandWidth = fillWidth == 0 ? 0.0 : (fillWidth * 0.35).clamp(16.0, 40.0);
|
||||||
|
final travelDistance = fillWidth + bandWidth * 2;
|
||||||
|
final durationMs = (travelDistance / _shimmerPixelsPerSecond * 1000)
|
||||||
|
.clamp(_shimmerMinDurationMs, _shimmerMaxDurationMs)
|
||||||
|
.round();
|
||||||
|
|
||||||
|
if (widget.shimmer && widget.value != null && fillWidth > 0) {
|
||||||
|
final duration = Duration(milliseconds: durationMs);
|
||||||
|
if (_controller.duration != duration) {
|
||||||
|
_controller.duration = duration;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return Stack(
|
return Stack(
|
||||||
fit: StackFit.expand,
|
fit: StackFit.expand,
|
||||||
children: [
|
children: [
|
||||||
Positioned(
|
Align(
|
||||||
left: left,
|
alignment: Alignment.centerLeft,
|
||||||
width: segmentWidth,
|
child: SizedBox(
|
||||||
top: 0,
|
width: fillWidth,
|
||||||
bottom: 0,
|
child: _ProgressFill(
|
||||||
child: _ProgressFill(
|
color: widget.fillColor,
|
||||||
color: widget.fillColor,
|
shimmer: widget.shimmer,
|
||||||
shimmer: widget.shimmer,
|
highlightStartColor: widget.highlightStartColor,
|
||||||
highlightStartColor: widget.highlightStartColor,
|
highlightEndColor: widget.highlightEndColor,
|
||||||
highlightEndColor: widget.highlightEndColor,
|
bandLeft: lerpDouble(-bandWidth, fillWidth + bandWidth, _controller.value),
|
||||||
bandLeft: lerpDouble(-segmentWidth, segmentWidth * 2, _controller.value),
|
bandWidth: bandWidth,
|
||||||
bandWidth: segmentWidth * 0.5,
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
},
|
||||||
|
),
|
||||||
final fillWidth = constraints.maxWidth * ((widget.value ?? 0).clamp(0, 1));
|
|
||||||
final bandWidth = fillWidth == 0 ? 0.0 : (fillWidth * 0.35).clamp(16.0, 40.0);
|
|
||||||
final travelDistance = fillWidth + bandWidth * 2;
|
|
||||||
final durationMs = (travelDistance / _shimmerPixelsPerSecond * 1000)
|
|
||||||
.clamp(_shimmerMinDurationMs, _shimmerMaxDurationMs)
|
|
||||||
.round();
|
|
||||||
|
|
||||||
if (widget.shimmer && widget.value != null && fillWidth > 0) {
|
|
||||||
final duration = Duration(milliseconds: durationMs);
|
|
||||||
if (_controller.duration != duration) {
|
|
||||||
_controller.duration = duration;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Stack(
|
|
||||||
fit: StackFit.expand,
|
|
||||||
children: [
|
|
||||||
Align(
|
|
||||||
alignment: Alignment.centerLeft,
|
|
||||||
child: SizedBox(
|
|
||||||
width: fillWidth,
|
|
||||||
child: _ProgressFill(
|
|
||||||
color: widget.fillColor,
|
|
||||||
shimmer: widget.shimmer,
|
|
||||||
highlightStartColor: widget.highlightStartColor,
|
|
||||||
highlightEndColor: widget.highlightEndColor,
|
|
||||||
bandLeft: lerpDouble(-bandWidth, fillWidth + bandWidth, _controller.value),
|
|
||||||
bandWidth: bandWidth,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
|||||||
Reference in new issue
Block a user