Skip to content

fix(genui): keep AudioPlayer controls inside narrow and large-text layouts - #1069

Closed
karolkulesza wants to merge 4 commits into
flutter:mainfrom
karolkulesza:fix/audio-player-narrow-layout
Closed

karolkulesza wants to merge 4 commits into
flutter:mainfrom
karolkulesza:fix/audio-player-narrow-layout

Conversation

@karolkulesza

Copy link
Copy Markdown

Description

AudioPlayer puts 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's AudioPlayer makes, 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.dart pump 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

  • I read the Flutter Style Guide recently, and have followed its advice.
  • I signed the CLA.
  • I read the Contributors Guide.
  • I have added sample code updates to the changelog.
  • I updated/added relevant documentation (doc comments with ///).
  • If my PR is a fork PR, I've checked that [e2e tests] passed.

If you need help, consider asking for advice on the #hackers-devrel channel on Discord.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +175 to +205
/// 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;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Performance & Robustness Issue

  1. Performance: _fitsOnOneRow is called on every build of the LayoutBuilder. Since _AudioPlayerWidgetState updates 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.
  2. Robustness: Using Directionality.of(context) and MediaQuery.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;
  }

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +337 to 340
Wrap(
alignment: WrapAlignment.spaceBetween,
children: [position, duration],
),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
Wrap(
alignment: WrapAlignment.spaceBetween,
children: [position, duration],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [position, duration],
),

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@polina-c

Copy link
Copy Markdown
Collaborator

genui package is deprecated in favor of these modular packages:
a2ui_core,
a2ui_agent, and
a2ui_flutter.
The new packages have a different API. To migrate your code, use the
A2UI migration skill.

See update on top of https://github.com/flutter/genui/blob/main/README.md

🤖 Agent-generated

@polina-c polina-c closed this Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants