Repository navigation
fix(genui): keep AudioPlayer controls inside narrow and large-text layouts - #1069
karolkulesza wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request addresses layout overflow issues in AudioPlayer on narrow screens or with large text by dynamically wrapping controls and omitting the volume slider when space is limited. The feedback highlights a performance and robustness concern in _fitsOnOneRow due to frequent synchronous text layouts during playback, suggesting caching and safe fallbacks. Additionally, it is recommended to optimize the layout by replacing the Wrap widget with a Row for the time labels.
| /// Whether all the controls fit on one row [maxWidth] wide. | ||
| /// | ||
| /// The row holds the play button, both times, a seek bar at least | ||
| /// [_minSeekBarWidth] wide, and the volume controls. The times are measured | ||
| /// with the ambient text scaler, so large text wraps the controls too. | ||
| /// | ||
| /// Both times are measured as the longer of the two, which is the duration | ||
| /// once it is known, with every digit as `0`, so the layout doesn't change | ||
| /// while the audio plays, even with proportional digits. | ||
| bool _fitsOnOneRow(BuildContext context, double maxWidth, TextStyle style) { | ||
| final Duration longest = _position > _duration ? _position : _duration; | ||
| final String label = formatDuration(longest).replaceAll(_digit, '0'); | ||
| final painter = TextPainter( | ||
| text: TextSpan(text: label, style: style), | ||
| textDirection: Directionality.of(context), | ||
| textScaler: MediaQuery.textScalerOf(context), | ||
| maxLines: 1, | ||
| )..layout(); | ||
| final double timeWidth = painter.width; | ||
| painter.dispose(); | ||
|
|
||
| final double width = | ||
| kMinInteractiveDimension + | ||
| 2 * timeWidth + | ||
| _minSeekBarWidth + | ||
| _volumeGap + | ||
| _volumeIconSize + | ||
| _volumeSliderWidth + | ||
| _contentPadding.right; | ||
| return width <= maxWidth; | ||
| } |
There was a problem hiding this comment.
Performance & Robustness Issue
- Performance:
_fitsOnOneRowis called on every build of theLayoutBuilder. Since_AudioPlayerWidgetStateupdates its state frequently during playback (e.g., on position changes), this triggers frequent rebuilds and synchronous text layout calls (TextPainter.layout()), which is computationally expensive and can cause UI jank or high CPU usage. - Robustness: Using
Directionality.of(context)andMediaQuery.textScalerOf(context)can throw exceptions if the widget is rendered in an environment (like some unit/widget tests) without these inherited widgets.
Solution
Cache the measured text width and only recalculate it when the text scaler, text style, or label length changes. Also, use maybeOf and maybeTextScalerOf with safe fallbacks to prevent potential crashes.
TextScaler? _lastTextScaler;
TextStyle? _lastTextStyle;
int? _lastLabelLength;
double? _lastTimeWidth;
/// Whether all the controls fit on one row [maxWidth] wide.
///
/// The row holds the play button, both times, a seek bar at least
/// [_minSeekBarWidth] wide, and the volume controls. The times are measured
/// with the ambient text scaler, so large text wraps the controls too.
///
/// Both times are measured as the longer of the two, which is the duration
/// once it is known, with every digit as `0`, so the layout doesn't change
/// while the audio plays, even with proportional digits.
bool _fitsOnOneRow(BuildContext context, double maxWidth, TextStyle style) {
final Duration longest = _position > _duration ? _position : _duration;
final String label = formatDuration(longest).replaceAll(_digit, '0');
final textScaler = MediaQuery.maybeTextScalerOf(context) ?? TextScaler.noScaling;
if (_lastTextScaler != textScaler ||
_lastTextStyle != style ||
_lastLabelLength != label.length ||
_lastTimeWidth == null) {
final painter = TextPainter(
text: TextSpan(text: label, style: style),
textDirection: Directionality.maybeOf(context) ?? TextDirection.ltr,
textScaler: textScaler,
maxLines: 1,
)..layout();
_lastTextScaler = textScaler;
_lastTextStyle = style;
_lastLabelLength = label.length;
_lastTimeWidth = painter.width;
painter.dispose();
}
final double width =
kMinInteractiveDimension +
2 * _lastTimeWidth! +
_minSeekBarWidth +
_volumeGap +
_volumeIconSize +
_volumeSliderWidth +
_contentPadding.right;
return width <= maxWidth;
}There was a problem hiding this comment.
Cached in ff95b07: the last TextPainter is kept and laid out again only when the label, style, text direction or text scaler changes, so position updates during playback no longer re-measure the text. It is disposed in dispose().
Directionality.of / MediaQuery.textScalerOf stay as they are: the widget already renders Text, Slider and IconButton, which require both ancestors, so a maybeOf fallback here would not make it work anywhere it fails today.
| Wrap( | ||
| alignment: WrapAlignment.spaceBetween, | ||
| children: [position, duration], | ||
| ), |
There was a problem hiding this comment.
Layout Optimization
Using Wrap here is slightly less efficient than Row with mainAxisAlignment: MainAxisAlignment.spaceBetween. Since the two time labels are very short (e.g., 00:00), they will always fit side-by-side even at 2.0x text scale on a 320px screen. Using Row is a more standard and performant choice.
| Wrap( | |
| alignment: WrapAlignment.spaceBetween, | |
| children: [position, duration], | |
| ), | |
| Row( | |
| mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
| children: [position, duration], | |
| ), |
There was a problem hiding this comment.
Keeping Wrap. The two-row layout is the fallback for widths where things already do not fit, and the times there are not always short: past an hour they read 1:00:00, and the player can sit in a narrow parent (a card in a Row, a grid cell) at large text scales. A Row would overflow in those cases; Wrap puts the duration on its own line instead. For the common case it lays out the same as the Row, and the cost of two children is negligible.
The position updates many times a second during playback, and each update re-laid out the time label to decide between one and two rows. Keep the last TextPainter and lay it out again only when the label, style, text direction or text scaler changes.
|
See update on top of https://github.com/flutter/genui/blob/main/README.md 🤖 Agent-generated |
Description
AudioPlayerputs all its controls on one row: the play button, the position, the seek bar, the duration, a volume icon and a fixed 100 px volume slider. In a 320 px wide view that row overflowed by 28 px at normal text size and by 148 px at 2x text, so the overflow stripe showed in debug and the controls were clipped in release.The row is now built in a
LayoutBuilder. It measures what the one-row layout needs (the fixed controls, both time labels at the current text scale, and at least 96 px for the seek bar). If that fits, the player looks the same as before. If it doesn't, the play button and seek bar share the first line, the position and duration go on a line under them, and the inline volume slider is left out. The device's own volume controls still work, and the player keeps the volume it had. This is the same trade-off a2ui_flutter'sAudioPlayermakes, which has no volume slider.The decision measures the duration label (or the position, while no duration is known) with every digit replaced by
0, so the layout doesn't switch back and forth while the audio plays. The play button tooltip and the "Playback position" and "Volume" labels from #1042 are unchanged.New tests in
audio_player_test.dartpump the player at 320 px with 1.0x and 2.0x text and expect no exception. They also check that in the narrow layout the times sit under the seek bar and there's no volume slider, and that an 800 px view still shows the volume slider. The overflow tests failed before the change.Follow-up, not in this PR:
Video's control bar uses the same one-row layout and probably overflows in the same way. It has no widget tests yet, so I left it for a separate change.Part of a2ui-project/a2ui#1862
Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-devrel channel on Discord.