CarouselView class

A Material Design carousel widget.

The CarouselView presents a scrollable list of items, each of which can dynamically change size based on the chosen layout.

Material Design 3 introduced 4 carousel layouts:

  • Multi-browse: This layout shows at least one large, medium, and small carousel item at a time. This layout is supported by CarouselView.weighted.
  • Uncontained (default): This layout show items that scroll to the edge of the container. This layout is supported by CarouselView.
  • Hero: This layout shows at least one large and one small item at a time. This layout is supported by CarouselView.weighted.
  • Full-screen: This layout shows one edge-to-edge large item at a time and scrolls vertically. The full-screen layout can be supported by both constructors.

The default constructor implements the uncontained layout model. It shows items that scroll to the edge of the container, behaving similarly to a ListView where all children are a uniform size. CarouselView.weighted enables dynamic item sizing. Each item is assigned a weight that determines the portion of the viewport it occupies. This constructor helps to 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 CarouselView.weighted is used, then set the flexWeights to only have one integer in the array.

This code snippet shows how to get a vertical full-screen carousel by using itemExtent in CarouselView.

This code snippet below shows how to achieve the same vertical full-screen carousel by using flexWeights in CarouselView.weighted.

link
Scaffold(
  body: CarouselView(
    scrollDirection: Axis.vertical,
    itemExtent: double.infinity,
    children: List<Widget>.generate(10, (int index) {
      return Center(child: Text('Item $index'));
    }),
  ),
),

// ...

Scaffold(
  body: CarouselView.weighted(
    scrollDirection: Axis.vertical,
    flexWeights: const <int>[1], // Or any positive integers as long as the length of the array is 1.
    children: List<Widget>.generate(10, (int index) {
      return Center(child: Text('Item $index'));
    }),
  ),
),

In CarouselView.weighted, weights are relative proportions. For example, if the layout weights is [3, 2, 1], it means the first visible item occupies 3/6 of the viewport; the second visible item occupies 2/6 of the viewport; the last visible item occupies 1/6 of the viewport. As the carousel scrolls, the size of the latter one gradually changes to the size of the former one. As a result, when the first visible item is completely off-screen, the following items will follow the same layout as before. Using CarouselView.weighted helps build the multi-browse, hero, center-aligned hero and full-screen layouts, as indicated in Carousel sepcs.

The CarouselController is used to control the CarouselController.initialItem, which determines the first fully expanded item when the CarouselView or CarouselView.weighted is initially displayed. This is straightforward for CarouselView because each item in the view has fixed size. In CarouselView.weighted, for instance, if the layout weights are [1, 2, 3, 2, 1] and the initial item is 4 (the fourth item), the view will display items 2, 3, 4, 5, and 6 with weights 1, 2, 3, 2 and 1 respectively.

The CarouselView.itemExtent property must be non-null and defines the base size of items. While items typically maintain this size, the first and last visible items may be slightly compressed during scrolling. The shrinkExtent property controls the minimum allowable size for these compressed items.

Here is an example to show different carousel layouts that CarouselView and CarouselView.weighted can build.
link

To create a local project with this code sample, run:
flutter create --sample=material.CarouselView.2 mysample

See also:

  • CarouselController, which controls the first fully visible item in the view.
  • PageView, which is a scrollable list that works page by page.
Inheritance

Constructors

CarouselView({Key? key, EdgeInsets? padding, Color? backgroundColor, double? elevation, ShapeBorder? shape, WidgetStateProperty<Color?>? overlayColor, bool itemSnapping = false, double shrinkExtent = 0.0, CarouselController? controller, Axis scrollDirection = Axis.horizontal, bool reverse = false, ValueChanged<int>? onTap, bool enableSplash = true, required double itemExtent, required List<Widget> children})
Creates a Material Design carousel.
const
CarouselView.weighted({Key? key, EdgeInsets? padding, Color? backgroundColor, double? elevation, ShapeBorder? shape, WidgetStateProperty<Color?>? overlayColor, bool itemSnapping = false, double shrinkExtent = 0.0, CarouselController? controller, Axis scrollDirection = Axis.horizontal, bool reverse = false, bool consumeMaxWeight = true, ValueChanged<int>? onTap, bool enableSplash = true, required List<int> flexWeights, required List<Widget> children})
Creates a scrollable list where the size of each child widget is dynamically determined by the provided flexWeights.
const

Properties

backgroundColor Color?
The background color for each carousel item.
final
children List<Widget>
The child widgets for the carousel.
final
consumeMaxWeight bool
Whether the collapsed items are allowed to expand to the max size.
final
controller CarouselController?
An object that can be used to control the position to which this scroll view is scrolled.
final
elevation double?
The z-coordinate of each carousel item.
final
enableSplash bool
Determines whether an InkWell will cover each Carousel item.
final
flexWeights List<int>?
The weights that each visible child should occupy in the viewport.
final
hashCode int
The hash code for this object.
no setterinherited
itemExtent double?
The extent the children are forced to have in the main axis.
final
itemSnapping bool
Whether the carousel should keep scrolling to the next/previous items to maintain the original layout.
final
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
onTap ValueChanged<int>?
Called when one of the children is tapped.
final
overlayColor WidgetStateProperty<Color?>?
The highlight color to indicate the carousel items are in pressed, hovered or focused states.
final
padding EdgeInsets?
The amount of space to surround each carousel item with.
final
reverse bool
Whether the carousel list scrolls in the reading direction.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
scrollDirection Axis
The Axis along which the scroll view's offset increases with each item.
final
shape ShapeBorder?
The shape of each carousel item's Material.
final
shrinkExtent double
The minimum allowable extent (size) in the main axis for carousel items during scrolling transitions.
final

Methods

createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
inherited
createState() State<CarouselView>
Creates the mutable state for this widget at a given location in the tree.
override
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A short, textual description of this widget.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited