Repository navigation
fix(genui): keep AudioPlayer controls inside narrow and large-text layouts #1069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
c6e2ac5
95a5641
e1169dc
ff95b07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -74,6 +74,24 @@ final audioPlayer = CatalogItem( | |||||||||||||||||
| ], | ||||||||||||||||||
| ); | ||||||||||||||||||
|
|
||||||||||||||||||
| /// The padding inside the player's card. | ||||||||||||||||||
| const _contentPadding = EdgeInsets.all(12); | ||||||||||||||||||
|
|
||||||||||||||||||
| /// The space between the time labels and the volume controls. | ||||||||||||||||||
| const double _volumeGap = 12; | ||||||||||||||||||
|
|
||||||||||||||||||
| /// The size of the volume icon. | ||||||||||||||||||
| const double _volumeIconSize = 20; | ||||||||||||||||||
|
|
||||||||||||||||||
| /// The width of the inline volume slider. | ||||||||||||||||||
| const double _volumeSliderWidth = 100; | ||||||||||||||||||
|
|
||||||||||||||||||
| /// The narrowest the seek bar gets before the controls wrap. | ||||||||||||||||||
| const double _minSeekBarWidth = 96; | ||||||||||||||||||
|
|
||||||||||||||||||
| /// Matches one digit of a time label. | ||||||||||||||||||
| final _digit = RegExp(r'\d'); | ||||||||||||||||||
|
|
||||||||||||||||||
| class _AudioPlayerWidget extends StatefulWidget { | ||||||||||||||||||
| const _AudioPlayerWidget({required this.url, this.description}); | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -154,14 +172,121 @@ class _AudioPlayerWidgetState extends State<_AudioPlayerWidget> { | |||||||||||||||||
| super.dispose(); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /// 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; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| Widget _buildPlayButton() { | ||||||||||||||||||
| return IconButton( | ||||||||||||||||||
| tooltip: _isPlaying ? 'Pause' : 'Play', | ||||||||||||||||||
| icon: Icon(_isPlaying ? Icons.pause : Icons.play_arrow), | ||||||||||||||||||
| onPressed: widget.url != null && widget.url!.isNotEmpty | ||||||||||||||||||
| ? () { | ||||||||||||||||||
| if (_isPlaying) { | ||||||||||||||||||
| _player.pause(); | ||||||||||||||||||
| } else { | ||||||||||||||||||
| _player.resume(); | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| : null, | ||||||||||||||||||
| ); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| Widget _buildSeekBar() { | ||||||||||||||||||
| return MergeSemantics( | ||||||||||||||||||
| child: Semantics( | ||||||||||||||||||
| label: 'Playback position', | ||||||||||||||||||
| child: Slider( | ||||||||||||||||||
| value: _duration.inMilliseconds > 0 | ||||||||||||||||||
| ? _position.inMilliseconds | ||||||||||||||||||
| .clamp(0, _duration.inMilliseconds) | ||||||||||||||||||
| .toDouble() | ||||||||||||||||||
| : 0, | ||||||||||||||||||
| max: _duration.inMilliseconds > 0 | ||||||||||||||||||
| ? _duration.inMilliseconds.toDouble() | ||||||||||||||||||
| : 1, | ||||||||||||||||||
| onChanged: (value) { | ||||||||||||||||||
| _player.seek(Duration(milliseconds: value.toInt())); | ||||||||||||||||||
| }, | ||||||||||||||||||
| ), | ||||||||||||||||||
| ), | ||||||||||||||||||
| ); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| List<Widget> _buildVolumeControls(BuildContext context) { | ||||||||||||||||||
| return [ | ||||||||||||||||||
| const SizedBox(width: _volumeGap), | ||||||||||||||||||
| Icon( | ||||||||||||||||||
| _volume == 0 | ||||||||||||||||||
| ? Icons.volume_off | ||||||||||||||||||
| : _volume < 0.5 | ||||||||||||||||||
| ? Icons.volume_down | ||||||||||||||||||
| : Icons.volume_up, | ||||||||||||||||||
| size: _volumeIconSize, | ||||||||||||||||||
| ), | ||||||||||||||||||
| SizedBox( | ||||||||||||||||||
| width: _volumeSliderWidth, | ||||||||||||||||||
| child: SliderTheme( | ||||||||||||||||||
| data: SliderTheme.of(context).copyWith( | ||||||||||||||||||
| overlayShape: SliderComponentShape.noOverlay, | ||||||||||||||||||
| padding: EdgeInsets.zero, | ||||||||||||||||||
| ), | ||||||||||||||||||
| child: MergeSemantics( | ||||||||||||||||||
| child: Semantics( | ||||||||||||||||||
| label: 'Volume', | ||||||||||||||||||
| child: Slider( | ||||||||||||||||||
| value: _volume, | ||||||||||||||||||
| onChanged: (value) { | ||||||||||||||||||
| setState(() => _volume = value); | ||||||||||||||||||
| _player.setVolume(value); | ||||||||||||||||||
| }, | ||||||||||||||||||
| ), | ||||||||||||||||||
| ), | ||||||||||||||||||
| ), | ||||||||||||||||||
| ), | ||||||||||||||||||
| ), | ||||||||||||||||||
| SizedBox(width: _contentPadding.right), | ||||||||||||||||||
| ]; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| @override | ||||||||||||||||||
| Widget build(BuildContext context) { | ||||||||||||||||||
| final ThemeData theme = Theme.of(context); | ||||||||||||||||||
| const contentPadding = EdgeInsets.all(12); | ||||||||||||||||||
| final TextStyle timeStyle = DefaultTextStyle.of( | ||||||||||||||||||
| context, | ||||||||||||||||||
| ).style.merge(theme.textTheme.bodySmall); | ||||||||||||||||||
|
|
||||||||||||||||||
| return Card( | ||||||||||||||||||
| child: Padding( | ||||||||||||||||||
| padding: contentPadding, | ||||||||||||||||||
| padding: _contentPadding, | ||||||||||||||||||
| child: Column( | ||||||||||||||||||
| mainAxisSize: MainAxisSize.min, | ||||||||||||||||||
| crossAxisAlignment: CrossAxisAlignment.start, | ||||||||||||||||||
|
|
@@ -176,81 +301,46 @@ class _AudioPlayerWidgetState extends State<_AudioPlayerWidget> { | |||||||||||||||||
| overflow: TextOverflow.ellipsis, | ||||||||||||||||||
| ), | ||||||||||||||||||
| ), | ||||||||||||||||||
| Row( | ||||||||||||||||||
| children: [ | ||||||||||||||||||
| IconButton( | ||||||||||||||||||
| tooltip: _isPlaying ? 'Pause' : 'Play', | ||||||||||||||||||
| icon: Icon(_isPlaying ? Icons.pause : Icons.play_arrow), | ||||||||||||||||||
| onPressed: widget.url != null && widget.url!.isNotEmpty | ||||||||||||||||||
| ? () { | ||||||||||||||||||
| if (_isPlaying) { | ||||||||||||||||||
| _player.pause(); | ||||||||||||||||||
| } else { | ||||||||||||||||||
| _player.resume(); | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| : null, | ||||||||||||||||||
| ), | ||||||||||||||||||
| Text( | ||||||||||||||||||
| LayoutBuilder( | ||||||||||||||||||
| builder: (context, constraints) { | ||||||||||||||||||
| final position = Text( | ||||||||||||||||||
| formatDuration(_position), | ||||||||||||||||||
| style: theme.textTheme.bodySmall, | ||||||||||||||||||
| ), | ||||||||||||||||||
| Expanded( | ||||||||||||||||||
| child: MergeSemantics( | ||||||||||||||||||
| child: Semantics( | ||||||||||||||||||
| label: 'Playback position', | ||||||||||||||||||
| child: Slider( | ||||||||||||||||||
| value: _duration.inMilliseconds > 0 | ||||||||||||||||||
| ? _position.inMilliseconds | ||||||||||||||||||
| .clamp(0, _duration.inMilliseconds) | ||||||||||||||||||
| .toDouble() | ||||||||||||||||||
| : 0, | ||||||||||||||||||
| max: _duration.inMilliseconds > 0 | ||||||||||||||||||
| ? _duration.inMilliseconds.toDouble() | ||||||||||||||||||
| : 1, | ||||||||||||||||||
| onChanged: (value) { | ||||||||||||||||||
| _player.seek(Duration(milliseconds: value.toInt())); | ||||||||||||||||||
| }, | ||||||||||||||||||
| ), | ||||||||||||||||||
| ), | ||||||||||||||||||
| ), | ||||||||||||||||||
| ), | ||||||||||||||||||
| Text( | ||||||||||||||||||
| style: timeStyle, | ||||||||||||||||||
| ); | ||||||||||||||||||
| final duration = Text( | ||||||||||||||||||
| formatDuration(_duration), | ||||||||||||||||||
| style: theme.textTheme.bodySmall, | ||||||||||||||||||
| ), | ||||||||||||||||||
| const SizedBox(width: 12), | ||||||||||||||||||
| Icon( | ||||||||||||||||||
| _volume == 0 | ||||||||||||||||||
| ? Icons.volume_off | ||||||||||||||||||
| : _volume < 0.5 | ||||||||||||||||||
| ? Icons.volume_down | ||||||||||||||||||
| : Icons.volume_up, | ||||||||||||||||||
| size: 20, | ||||||||||||||||||
| ), | ||||||||||||||||||
| SizedBox( | ||||||||||||||||||
| width: 100, | ||||||||||||||||||
| child: SliderTheme( | ||||||||||||||||||
| data: SliderTheme.of(context).copyWith( | ||||||||||||||||||
| overlayShape: SliderComponentShape.noOverlay, | ||||||||||||||||||
| padding: EdgeInsets.zero, | ||||||||||||||||||
| style: timeStyle, | ||||||||||||||||||
| ); | ||||||||||||||||||
| if (_fitsOnOneRow(context, constraints.maxWidth, timeStyle)) { | ||||||||||||||||||
| return Row( | ||||||||||||||||||
| children: [ | ||||||||||||||||||
| _buildPlayButton(), | ||||||||||||||||||
| position, | ||||||||||||||||||
| Expanded(child: _buildSeekBar()), | ||||||||||||||||||
| duration, | ||||||||||||||||||
| ..._buildVolumeControls(context), | ||||||||||||||||||
| ], | ||||||||||||||||||
| ); | ||||||||||||||||||
| } | ||||||||||||||||||
| // Too narrow for one row: the times go under the seek bar, | ||||||||||||||||||
| // and the volume is left to the device's own controls. | ||||||||||||||||||
| return Column( | ||||||||||||||||||
| mainAxisSize: MainAxisSize.min, | ||||||||||||||||||
| crossAxisAlignment: CrossAxisAlignment.stretch, | ||||||||||||||||||
| children: [ | ||||||||||||||||||
| Row( | ||||||||||||||||||
| children: [ | ||||||||||||||||||
| _buildPlayButton(), | ||||||||||||||||||
| Expanded(child: _buildSeekBar()), | ||||||||||||||||||
| ], | ||||||||||||||||||
| ), | ||||||||||||||||||
| child: MergeSemantics( | ||||||||||||||||||
| child: Semantics( | ||||||||||||||||||
| label: 'Volume', | ||||||||||||||||||
| child: Slider( | ||||||||||||||||||
| value: _volume, | ||||||||||||||||||
| onChanged: (value) { | ||||||||||||||||||
| setState(() => _volume = value); | ||||||||||||||||||
| _player.setVolume(value); | ||||||||||||||||||
| }, | ||||||||||||||||||
| ), | ||||||||||||||||||
| ), | ||||||||||||||||||
| Wrap( | ||||||||||||||||||
| alignment: WrapAlignment.spaceBetween, | ||||||||||||||||||
| children: [position, duration], | ||||||||||||||||||
| ), | ||||||||||||||||||
|
Comment on lines
+359
to
362
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout OptimizationUsing
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keeping |
||||||||||||||||||
| ), | ||||||||||||||||||
| ), | ||||||||||||||||||
| SizedBox(width: contentPadding.right), | ||||||||||||||||||
| ], | ||||||||||||||||||
| ], | ||||||||||||||||||
| ); | ||||||||||||||||||
| }, | ||||||||||||||||||
| ), | ||||||||||||||||||
| ], | ||||||||||||||||||
| ), | ||||||||||||||||||
|
|
||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Performance & Robustness Issue
_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.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
maybeOfandmaybeTextScalerOfwith safe fallbacks to prevent potential crashes.There was a problem hiding this comment.
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
TextPainteris 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 indispose().Directionality.of/MediaQuery.textScalerOfstay as they are: the widget already rendersText,SliderandIconButton, which require both ancestors, so amaybeOffallback here would not make it work anywhere it fails today.