-
Notifications
You must be signed in to change notification settings - Fork 37
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
propably fix #53 #56
base: main
Are you sure you want to change the base?
propably fix #53 #56
Conversation
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.
Boils down to this:
I'd rather use max(0, _sizes.length - 1)
instead of adding manual logic.
Haven't tested this code, but it should work out of the box.
@@ -207,7 +207,9 @@ class _ExpandablePageViewState extends State<ExpandablePageView> { | |||
_sizes = _prepareSizes(); | |||
_pageController = widget.controller ?? PageController(); | |||
_pageController.addListener(_updatePage); | |||
_currentPage = _pageController.initialPage.clamp(0, _sizes.length - 1); | |||
final sizesLength = _sizes.length; | |||
final sizesUpperBounds = sizesLength == 0 ? 0 : sizesLength - 1; |
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.
use _sizes.isEmpty
instead of checking if length is 0. Irrelevant though, see below
_currentPage = _pageController.initialPage.clamp(0, _sizes.length - 1); | ||
final sizesLength = _sizes.length; | ||
final sizesUpperBounds = sizesLength == 0 ? 0 : sizesLength - 1; | ||
_currentPage = _pageController.initialPage.clamp(0, sizesUpperBounds); |
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.
Suggestion:
_currentPage = _pageController.initialPage.clamp(0, max(0, _sizes.length - 1));
@@ -207,7 +207,9 @@ class _ExpandablePageViewState extends State<ExpandablePageView> { | |||
_sizes = _prepareSizes(); | |||
_pageController = widget.controller ?? PageController(); | |||
_pageController.addListener(_updatePage); | |||
_currentPage = _pageController.initialPage.clamp(0, _sizes.length - 1); | |||
final sizesLength = _sizes.length; | |||
final sizesUpperBounds = sizesLength == 0 ? 0 : sizesLength - 1; |
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.
I'd generally advise against creating pointless variables for this. It doesn't save any space and it prevents the linter from telling you to use isEmpty
instead of .length == 0
widget.onPageChanged?.call(_currentPage); | ||
|
||
_previousPage = (_currentPage + differenceFromPreviousToCurrent) | ||
.clamp(0, _sizes.length - 1); | ||
.clamp(0, sizesLength - 1); |
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.
Same solution as above:
max(0, _sizes.length - 1)
} | ||
|
||
_previousPage = _previousPage.clamp(0, _sizes.length - 1); | ||
final sizesUpperBounds = sizesLength == 0 ? 0 : sizesLength - 1; | ||
_previousPage = _previousPage.clamp(0, sizesUpperBounds); |
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.
Same solution as above:
max(0, _sizes.length - 1)
@ciriousjoker I think you're completely right. I'll check your suggestion and make adjustments. |
@jayjah any updates? |
Should be a fix to Issue#53
It just makes sure, that
clamp
get's called on a valid range otherwise it might just throw an error like the following: