CarouselView.builder constructor

const CarouselView.builder({
  1. Key? key,
  2. EdgeInsets? padding,
  3. Color? backgroundColor,
  4. double? elevation,
  5. ShapeBorder? shape,
  6. Clip? itemClipBehavior,
  7. WidgetStateProperty<Color?>? overlayColor,
  8. bool itemSnapping = false,
  9. double shrinkExtent = 0.0,
  10. CarouselController? controller,
  11. Axis scrollDirection = Axis.horizontal,
  12. bool reverse = false,
  13. ValueChanged<int>? onTap,
  14. bool enableSplash = true,
  15. required double itemExtent,
  16. required NullableIndexedWidgetBuilder? itemBuilder,
  17. int? itemCount,
  18. ValueChanged<int>? onIndexChanged,
  19. bool infinite = false,
})

Creates a scrollable carousel with fixed-sized items created on demand.

This constructor allows lazy loading of carousel items. Only items that are visible (or about to be visible) are built, improving performance when dealing with large numbers of items.

The itemBuilder callback will be called only with indices greater than or equal to zero and less than itemCount.

This example shows how to create a carousel with 1000 items using lazy loading:

To see it in action, copy and run this code snippet on DartPad.

import 'package:material_ui/material_ui.dart';

/// Flutter code sample for [CarouselView.builder].

void main() => runApp(const CarouselBuilderExampleApp());

class CarouselBuilderExampleApp extends StatelessWidget {
  const CarouselBuilderExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(title: const Text('CarouselView.builder Sample')),
        body: const CarouselBuilderExample(),
      ),
    );
  }
}

class CarouselBuilderExample extends StatelessWidget {
  const CarouselBuilderExample({super.key});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: ConstrainedBox(
        constraints: const BoxConstraints(maxHeight: 200),
        child: CarouselView.builder(
          itemExtent: 350,
          itemCount: 1000,
          itemBuilder: (BuildContext context, int index) {
            return ColoredBox(
              color: Colors.primaries[index % Colors.primaries.length],
              child: Center(
                child: Text(
                  'Item $index',
                  style: const TextStyle(
                    color: Colors.white,
                    fontSize: 24,
                    fontWeight: .bold,
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );
  }
}

See also:

Implementation

// TODO(framework): Replace the following block with a @dartpad directive
// when it's supported. https://github.com/dart-lang/dartdoc/issues/4123
/// {@macro material_ui.dartpad_guide}
///
/// {@example /example/lib/carousel/carousel.1.dart#body}
///
/// </callout-box>
///
/// See also:
///
///  * [CarouselView.new], which creates a carousel with explicit children.
///  * [CarouselView.weighted], which creates a carousel with weighted items.
///  * [CarouselView.weightedBuilder], which creates a carousel with weighted
///    items using lazy loading.
const CarouselView.builder({
  super.key,
  this.padding,
  this.backgroundColor,
  this.elevation,
  this.shape,
  this.itemClipBehavior,
  this.overlayColor,
  this.itemSnapping = false,
  this.shrinkExtent = 0.0,
  this.controller,
  this.scrollDirection = Axis.horizontal,
  this.reverse = false,
  this.onTap,
  this.enableSplash = true,
  required double this.itemExtent,
  required this.itemBuilder,
  this.itemCount,
  this.onIndexChanged,
  this.infinite = false,
}) : consumeMaxWeight = true,
     flexWeights = null,
     children = const <Widget>[];