-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Create CarouselView widget - Part 2
#149775
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
Conversation
c076d7f to
d3efad5
Compare
|
Since Hans came back, I also added him as a reviewer😁 |
This comment was marked as outdated.
This comment was marked as outdated.
ScrollBehavior is already inherited by the internal scrollable widget. You should be able to configure this by adding a ScrollConfiguration above the carousel. We usually only expose a scrollBehavior property if the widget is overriding it internally for a specific feature, making it inaccessible without exposing it as a property. I don't know that we need to do that in this case. |
That makes sense, I will try it like this. Thank you for the reply! |
Piinks
left a comment
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.
This is coming along so nicely! Mostly nits and a couple of questions. Thanks for your patience!
| /// enables dynamic item sizing. Each item is assigned a weight that determines | ||
| /// the portion of the viewport it occupies. This allows you to easily create | ||
| /// layouts like multi-browse, and hero. In order to have a full-screen layout, | ||
| /// if [CarouselView] is used, then set the [itemExtent] to screen size; if |
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.
The viewport won't necessarily be the full screen size, it could be part of a column with other elements, or under an app bar in a Scaffold. How would someone determine the size of the viewport in order to set itemExtent? This might be a good example to include.
| /// As the initially visible items change size during scrolling, item 3 enters | ||
| /// the view to fill the remaining space. Its extent starts at a minimum of | ||
| /// [shrinkExtent] (or 0 if [shrinkExtent] is not provided) and gradually | ||
| /// increases to match the extent of the last visible item (100 in this example). |
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.
These docs are super valuable! But they will not be on https://api.flutter.dev/ since this is a private class. Should we move these docs up to the public class?
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.
Yes, I can move these docs to the public CarouselView class! Is this okay if we include these calculation explanation in public doc?
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.
Yeah, I think it is fine to include! :)
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.
Updated!
| // This method is mostly the same as its parent class [RenderSliverFixedExtentList]. | ||
| // The difference is when we allow some space before the leading items or after | ||
| // the trailing items with smaller weights, we leave extra scroll offset. |
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.
Rather than copying most of this logic, can we add a spacing value/getter/method on the super class? If this is feasible, we could do it in a separate PR.
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.
Sounds good to me! Added a TODO here:)
7a88cee to
866ad31
Compare
| /// Scaffold( | ||
| /// body: CarouselView( | ||
| /// scrollDirection: Axis.vertical, | ||
| /// itemExtent: MediaQuery.sizeOf(context).height, |
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.
This would be the size of the device screen, not necessarily the size of the CarouselView. What if the carousel is smaller than the device screen?
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.
To make the itemExtent match the size of the carousel, we can use double.infinity. Just updated the sample code:)!
| class _CarouselViewState extends State<CarouselView> { | ||
| late double _itemExtent; | ||
| double? _itemExtent; | ||
| List<int>? _weights; |
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.
The whole purpose for this is when the initial item is given some value, we know how the initial view should look like.
Is this only used for initialization? Do we need to tell the controller (i.e. in didUpdateWidget) when these values have changed on the widget so the controller can redo its calculations with the new values?
(Instead copying I would also lean toward getters, the other question as to whether we need to tell the controller when these values change still stands, though)
| _consumeMaxWeight = widget.consumeMaxWeight; | ||
| _itemExtent = widget.itemExtent; |
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.
Why copy these in didChangeDependencies, but _weights is copied in initState?
| final double? itemExtent = _carouselState!._itemExtent; | ||
| int expandedItem = initialItem; | ||
|
|
||
| if (weights != null && !_carouselState!._consumeMaxWeight) { |
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.
Related to the above question: What if the values read from _carouselState change after they have been read? Do we need to inform the controller and redo some of these calculations? Would also be good to add a test for that case.
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.
We've been chatting about this. I think what is needed here is to update the position, not the controller. The values provided to the controller are just passed to (and only used by) the position, which calculates what item is at what pixel, or what pixel is at which item based on those values.
So if they change here, we should update _controller.position.VALUE so it can make the right calculations based on the new info.
Converting the properties in the carousel scroll position to setters/getters will make this possible, with an update to the pixels when the value changes. (See _PagePosition.viewportFraction). And thanks Qun for mentioning PageView, looking at that really helped!
What do you all think?
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.
This makes sense to me:) Trying to add setters and getters for itemExtent and flexWeights in _CarouselPosition and setting they to new values when they are updated. Thanks for the suggestions!
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.
Just updated! _flexWeights and _consumeMaxWeight are changed to getters in _CarouselViewState class. _itemExtent should be constrained to be smaller than the viewport in the implementation, so I keep it as is.
And when we update flexWeights, the layout will change to match the new weight list but the item that occupies the max weight stays the same. When we update itemExtent, the item main-axis extent will change but leading item also stays the same.
Demo 1 (Updating weights respects `initialItem`)
update_weights.mov
Demo 2 (Updating weights respects the item that currently occupied the max weight)
current_max_item.mov
6c3db4c to
dd98db8
Compare
Piinks
left a comment
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.
Roll Flutter from 58068d8 to 7d5c1c0 (104 revisions) flutter/flutter@58068d8...7d5c1c0 2024-07-19 [email protected] Enhances intuitiveness of RawMagnifier's example (flutter/flutter#150308) 2024-07-19 [email protected] Roll pub packages (flutter/flutter#151992) 2024-07-19 [email protected] Add test for scrollbar.1.dart (flutter/flutter#151463) 2024-07-19 [email protected] Roll Flutter Engine from ea1e53a4e810 to 969fb7abc449 (3 revisions) (flutter/flutter#152018) 2024-07-19 [email protected] docimports for rendering library (flutter/flutter#151958) 2024-07-19 [email protected] Roll Flutter Engine from b65c93ea948e to ea1e53a4e810 (2 revisions) (flutter/flutter#152012) 2024-07-19 [email protected] painting: drop deprecated (exported) hashList and hashValues functions (flutter/flutter#151677) 2024-07-18 [email protected] Roll Flutter Engine from 766f7bed7185 to b65c93ea948e (2 revisions) (flutter/flutter#152004) 2024-07-18 [email protected] Update TESTOWNERS (flutter/flutter#151907) 2024-07-18 [email protected] chore: fix test name & add description of tests (flutter/flutter#151959) 2024-07-18 [email protected] Roll Flutter Engine from 564ded4c4742 to 766f7bed7185 (2 revisions) (flutter/flutter#151998) 2024-07-18 [email protected] Fix SelectionArea scrolling conflicts (flutter/flutter#151138) 2024-07-18 [email protected] Fix: BaseTapAndDragGestureRecognizer should reset drag state after losing gesture arena (flutter/flutter#151989) 2024-07-18 [email protected] Roll pub packages (flutter/flutter#151975) 2024-07-18 [email protected] Roll Flutter Engine from 8bcf638eb893 to 564ded4c4742 (2 revisions) (flutter/flutter#151986) 2024-07-18 [email protected] Fix WidgetStateTextStyle's doc (flutter/flutter#151935) 2024-07-18 [email protected] Roll Flutter Engine from d58ba74250ce to 8bcf638eb893 (2 revisions) (flutter/flutter#151977) 2024-07-18 [email protected] Adds 3.22.3 changelog (flutter/flutter#151974) 2024-07-18 [email protected] Roll Packages from d03b1b4 to c7f0526 (8 revisions) (flutter/flutter#151971) 2024-07-18 [email protected] `WidgetState` mapping (flutter/flutter#146043) 2024-07-18 [email protected] Fix AppBar doc to keep diagram next to its description (flutter/flutter#151937) 2024-07-18 [email protected] Small fixes to Image docs: NNBD, and add a cross-reference (flutter/flutter#151938) 2024-07-18 [email protected] Roll Flutter Engine from b043fe447bb3 to d58ba74250ce (1 revision) (flutter/flutter#151964) 2024-07-18 [email protected] Roll pub packages (flutter/flutter#151946) 2024-07-18 [email protected] Roll pub packages (flutter/flutter#151904) 2024-07-18 [email protected] Roll Flutter Engine from e3abca2d8105 to b043fe447bb3 (1 revision) (flutter/flutter#151942) 2024-07-18 [email protected] Roll Flutter Engine from 8073523b4623 to e3abca2d8105 (1 revision) (flutter/flutter#151936) 2024-07-18 [email protected] Roll Flutter Engine from dfe22e3acc19 to 8073523b4623 (1 revision) (flutter/flutter#151934) 2024-07-18 [email protected] Roll Flutter Engine from 184c3f0de6b3 to dfe22e3acc19 (1 revision) (flutter/flutter#151930) 2024-07-18 [email protected] Roll Flutter Engine from 00f0f6b74da7 to 184c3f0de6b3 (1 revision) (flutter/flutter#151928) 2024-07-18 [email protected] Roll Flutter Engine from d194a2f0e5da to 00f0f6b74da7 (1 revision) (flutter/flutter#151927) 2024-07-18 [email protected] Roll Flutter Engine from a5a93bb80bd1 to d194a2f0e5da (3 revisions) (flutter/flutter#151925) 2024-07-17 [email protected] Roll Flutter Engine from e9dc62074c2b to a5a93bb80bd1 (1 revision) (flutter/flutter#151918) 2024-07-17 [email protected] [web] use the new backlog Github project in triage links (flutter/flutter#151920) 2024-07-17 [email protected] Update Flutter-Web-Triage.md (flutter/flutter#151607) 2024-07-17 [email protected] Reland fix InputDecorator hint default text style on M3 (flutter/flutter#150835) 2024-07-17 [email protected] Roll Flutter Engine from 39ee1a549581 to e9dc62074c2b (3 revisions) (flutter/flutter#151915) 2024-07-17 [email protected] Constrain `CupertinoContextMenu` animation to safe area (flutter/flutter#151860) 2024-07-17 [email protected] Create `CarouselView` widget - Part 2 (flutter/flutter#149775) 2024-07-17 [email protected] Roll Flutter Engine from 45b722b661f0 to 39ee1a549581 (3 revisions) (flutter/flutter#151905) 2024-07-17 [email protected] docs: Fix typo in data driven fixes test folder section (flutter/flutter#151836) 2024-07-17 [email protected] Stop running flaky mac tests in presubmit (flutter/flutter#151870) 2024-07-17 [email protected] Roll Flutter Engine from 7e2579634027 to 45b722b661f0 (1 revision) (flutter/flutter#151895) 2024-07-17 [email protected] fix(Flutter Web App): fixes html lang typo (flutter/flutter#151866) 2024-07-17 [email protected] Delete `docs/engine` directory (flutter/flutter#151616) 2024-07-17 [email protected] Make `CupertinoSlidingSegmentedControl` type parameter non-null (flutter/flutter#151803) ...
|
Hi @QuncCccccc Hi, how can I get the index of the currently displayed items? Suppose I pass List? _weights = [6,1], how can I get the index when I scroll to the positions?. Even the index of the position corresponding to 6 would be fine. Is there a way to get it or a formula to calculate it? |
This PR is to create `Carousel.weighted` so the size of each carousel item is based on a list of weights. While scrolling, item sizes are changing dynamically based on the scrolling progress. https://github.com/flutter/flutter/assets/36861262/181472b0-6f8b-48e7-b191-ab5f7c88c0c8
|
can you please add options like this CarouselOptions( |
|
What version of Flutter will have this? I noticed it's not in 3.24.0 |
|
My wild guess is 3.26 stable, but it's already available on the main channel |
|
Hi everyone. The team (usually) does not follow up on comments in closed PRs. Please open a new issue for bugs, see where's my commit for what build tag is on a given PR, or see the help channel on Discord for questions. |

This PR is to create
Carousel.weightedso the size of each carousel item is based on a list of weights. While scrolling, item sizes are changing dynamically based on the scrolling progress.weighted_carousel.mov
Pre-launch Checklist
///).