Skip to content

feat: add CarouselView control - #38

Merged
AdamMusa merged 2 commits into
codex/engine-initializationfrom
feat/carousel-view
Oct 29, 2026
Merged

AdamMusa merged 2 commits into
codex/engine-initializationfrom
feat/carousel-view

Conversation

@AdamMusa

@AdamMusa AdamMusa commented Oct 28, 2026 •

Copy link
Copy Markdown
Owner

Adds the CarouselView control (Work Package F, item F2, P2).

Ruby counterpart: AdamMusa/ruflet#51

Rendering

It follows the three-file adaptive pattern, plus one design-neutral model file:

  • adaptive_carousel_view.dart dispatches through PlatformControlRenderer inside LayoutControl. It renders an ErrorControl when:
    • neither item_extent nor flex_weights is set (the message suggests both);
    • flex_weights contains a non-positive weight;
    • an axis is unbounded. A carousel is a scroll view, so a horizontal carousel without a finite height gets "CarouselView needs a bounded height" with the hint "Set height or expand". The same check applies to the main axis.
  • carousel_view_model.dart is design-neutral. It holds the shared keyword parsing, initial_item and index clamping, the item_click callback, and the parser for the animate_to_item invoke arguments. It accepts camelCase or snake_case curve names.
  • Material (material_carousel_view.dart) uses CarouselView, or CarouselView.weighted when flex_weights is given (flex_weights wins over item_extent). It is driven by a CarouselController(initialItem:), and animate_to_item calls CarouselController.animateToItem.
  • Cupertino (cupertino_carousel_view.dart) is hand-built from widgets-layer and Cupertino pieces only, with no Material, per D-7.
    • With item_extent, it is a fixed-extent ListView.builder with BouncingScrollPhysics. With item_snapping, a small per-item snapping physics settles flings on whole items, like PageScrollPhysics with a page size of one item.
    • As on Material, an item is never larger than the viewport.
    • Each item is Padding(padding, default 4) around a PhysicalShape:
      • its shape is shape, or a 20 px rounded rectangle by default;
      • it is filled with bgcolor, or secondarySystemBackground by default;
      • it also applies elevation and item_clip_behavior.
    • On press the item dims to 0.6 opacity, like a CupertinoButton. If overlay_color is given, its pressed colour is painted over the item instead. enable_splash: false turns this feedback off.
    • Taps go through a GestureDetector, attached only when on_item_click is set.
    • flex_weights is approximated by a PageView. Its viewportFraction is the first weight divided by the sum, with padEnds: false and pageSnapping set from item_snapping. This approximation is documented in the SPEC.

Wire keys and events

Ruby keyword Material Cupertino
controls children list items
item_extent itemExtent ListView.itemExtent (clamped to viewport)
flex_weights CarouselView.weighted(flexWeights:) PageView(viewportFraction: first/sum)
consume_max_weight consumeMaxWeight (n/a: approximation)
shrink_extent shrinkExtent (n/a: items keep full extent)
item_snapping itemSnapping per-item snap physics / pageSnapping
horizontal, reverse scrollDirection, reverse same
padding padding (default 4) item padding (default 4)
bgcolor, elevation, shape, item_clip_behavior Material item surface PhysicalShape
overlay_color, enable_splash InkWell overlay / splash pressed overlay / opacity feedback
initial_item CarouselController(initialItem:) initial scroll offset / page
  • Event: on_item_click fires item_click with the tapped index (int). The tap handler is only set when the Ruby handler exists.
  • Method: Ruby animate_to_item(index, duration: 300, curve: "ease") sends the animate_to_item invoke. Each renderer registers its own addInvokeMethodListener and returns null for other names. Out-of-range indexes clamp.
  • Flutter 3.41.2 has no CarouselView.onIndexChanged, so per the SPEC there is no on_change event for the visible item.

Tests

  • New test/widgets/ruby_carousel_view_test.dart, 22 tests on iOS and Android. They check:
    • items are exactly item_extent wide, at full height, and laid out in order;
    • the default 4 px padding;
    • tapping item 1 fires item_click 1, and nothing fires without a handler;
    • animate_to_item(2) moves item 2 to the leading edge, and index 99 clamps to the last item;
    • initial_item starts on that item;
    • item_snapping settles on a whole item after a drag;
    • flex_weights [2,1,1] makes the leading item half the viewport;
    • a vertical carousel stacks items;
    • bgcolor, elevation, shape, item_clip_behavior and reverse reach the renderer;
    • on iOS, the default surface is a 20 px rounded card, a pressed item dims to 0.6 opacity, and no Material widgets render;
    • the error cases above, including an unbounded height.
  • New matrix fixture CarouselView (item_extent: 200), covered on all 5 platforms.
  • verify_pr.sh with Flutter 3.41.2: +722: All tests passed! (baseline 695). flutter analyze lib test reports No issues found!. The wire-key audit shows 0 new findings: every CarouselView key is read and item_click is fired.

Render Ruby carousel_view with Flutter's M3 CarouselView (or CarouselView.weighted) on Material and a hand-built Cupertino carousel on Apple platforms: a fixed-extent bouncing ListView with optional per-item snapping, rounded PhysicalShape items with press dimming, and a PageView approximation for flex_weights. item_click reports the tapped index and animate_to_item scrolls to an item.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant