[material_ui] Fix TabBar assertion to allow indicatorWeight: 0 with a themed indicator - #12696
[material_ui] Fix TabBar assertion to allow indicatorWeight: 0 with a themed indicator#12696Massinissa-Mouhoub wants to merge 1 commit into
TabBar assertion to allow indicatorWeight: 0 with a themed indicator#12696Conversation
There was a problem hiding this comment.
Code Review
This pull request allows TabBar to accept an indicatorWeight of 0.0 when a custom indicator is supplied by the widget or TabBarThemeData. It removes the constructor-level assertions and introduces a runtime assertion with a detailed FlutterError when the default underline indicator is drawn with an invalid weight. Additionally, tests are added to verify these behaviors. The review feedback suggests improving the robustness of the weight check to handle NaN values and correcting a class reference in the error message for consistency.
| return tabBarTheme.indicator!; | ||
| } | ||
| assert(() { | ||
| if (widget.indicatorWeight <= 0.0) { |
There was a problem hiding this comment.
Using !(widget.indicatorWeight > 0.0) instead of widget.indicatorWeight <= 0.0 is more robust because it also correctly handles double.nan values, ensuring that any invalid weight triggers the assertion rather than potentially causing silent rendering issues or secondary errors later.
if (!(widget.indicatorWeight > 0.0)) {| ErrorDescription( | ||
| 'The indicatorWeight must be greater than zero when the TabBar ' | ||
| 'draws its default underline indicator, that is when no indicator ' | ||
| 'is provided by the TabBar or the TabBarTheme.', | ||
| ), |
There was a problem hiding this comment.
For consistency with the rest of the error message (such as the ErrorHint below and the documentation), refer to TabBarThemeData instead of TabBarTheme in the error description.
ErrorDescription(
'The indicatorWeight must be greater than zero when the TabBar '
'draws its default underline indicator, that is when no indicator '
'is provided by the TabBar or the TabBarThemeData.',
),TabBar assertion to allow indicatorWeight: 0 with a themed indicator"
TabBar assertion to allow indicatorWeight: 0 with a themed indicator"TabBar assertion to allow indicatorWeight: 0 with a themed indicator
QuncCccccc
left a comment
There was a problem hiding this comment.
LGTM. Thank you for porting it over!
This is the port of this PR after decoupling.
Fixes this.
Original PR Description
This PR moves the assertion that checks indicator != null || (indicatorWeight > 0.0) from the TabBar constructors into the Decoration _getIndicator(TabBarIndicatorSize indicatorSize) method. During the first phase of creating the TabBar widget, Dart just fills in the fields, there is no BuildContext available yet, so the widget has no way of access the TabBarTheme via context.dependOnInheritedWidgetOfExactType(). As a result, the constructor's assertion can only see the indicator passed directly to the widget, and it fails even when a valid indicator is supplied through the TabBarTheme.
By moving the assertion into the method mentionned above, which runs at build time, the TabBar now has access to the TabBarTheme and can validate the indicator correctly, accepting indicatorWeight: 0 when a themed indicator is present, while still asserting when the default underline indicator is used.
Fixes flutter/flutter#188837
Pre-Review Checklist
[shared_preferences]///).