diff --git a/packages/flutter/lib/src/material/carousel.dart b/packages/flutter/lib/src/material/carousel.dart index a73c354490..1e60e7e0ae 100644 --- a/packages/flutter/lib/src/material/carousel.dart +++ b/packages/flutter/lib/src/material/carousel.dart @@ -130,6 +130,7 @@ class CarouselView extends StatefulWidget { this.scrollDirection = Axis.horizontal, this.reverse = false, this.onTap, + this.enableSplash = true, required double this.itemExtent, required this.children, }) : consumeMaxWeight = true, @@ -190,6 +191,7 @@ class CarouselView extends StatefulWidget { this.reverse = false, this.consumeMaxWeight = true, this.onTap, + this.enableSplash = true, required List this.flexWeights, required this.children, }) : itemExtent = null; @@ -290,6 +292,16 @@ class CarouselView extends StatefulWidget { /// Called when one of the [children] is tapped. final ValueChanged? onTap; + /// Determines whether an [InkWell] will cover each Carousel item. + /// + /// If true, tapping an item will create an ink splash + /// as defined by the [ThemeData.splashFactory]. + /// + /// Setting this to false allows the [children] to respond to user gestures. + /// + /// Defaults to true. + final bool enableSplash; + /// The extent the children are forced to have in the main axis. /// /// The item extent should not exceed the available space that the carousel view @@ -401,6 +413,28 @@ class _CarouselViewState extends State { return null; }); + Widget contents = widget.children[index]; + if (widget.enableSplash) { + contents = Stack( + fit: StackFit.expand, + children: [ + contents, + Material( + color: Colors.transparent, + child: InkWell( + onTap: () => widget.onTap?.call(index), + overlayColor: effectiveOverlayColor, + ), + ), + ], + ); + } else if (widget.onTap != null) { + contents = GestureDetector( + onTap: () => widget.onTap!(index), + child: contents, + ); + } + return Padding( padding: effectivePadding, child: Material( @@ -408,21 +442,7 @@ class _CarouselViewState extends State { color: effectiveBackgroundColor, elevation: effectiveElevation, shape: effectiveShape, - child: Stack( - fit: StackFit.expand, - children: [ - widget.children[index], - Material( - color: Colors.transparent, - child: InkWell( - onTap: () { - widget.onTap?.call(index); - }, - overlayColor: effectiveOverlayColor, - ), - ), - ], - ), + child: contents, ), ); } diff --git a/packages/flutter/test/material/carousel_test.dart b/packages/flutter/test/material/carousel_test.dart index 4bab1b1577..a4c1348a06 100644 --- a/packages/flutter/test/material/carousel_test.dart +++ b/packages/flutter/test/material/carousel_test.dart @@ -1236,6 +1236,143 @@ void main() { // This should be less than 330.0 because the item is shrunk; width is 800.0 - 330.0 - 330.0 expect(tester.getRect(getItem(2)).width, 140.0); }); + + testWidgets('CarouselView onTap is clickable', (WidgetTester tester) async { + int tappedIndex = -1; + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: CarouselView( + itemExtent: 350, + onTap: (int index) { + tappedIndex = index; + }, + children: List.generate(3, (int index) { + return Center( + child: Text('Item $index'), + ); + }), + ), + ), + ), + ); + + await tester.pumpAndSettle(); + + final Finder carouselItem = find.text('Item 1'); + await tester.tap(carouselItem, warnIfMissed: false); + await tester.pumpAndSettle(); + + // Verify that the onTap callback was called with the correct index. + expect(tappedIndex, 1); + + // Tap another item. + final Finder anotherCarouselItem = find.text('Item 2'); + await tester.tap(anotherCarouselItem, warnIfMissed: false); + await tester.pumpAndSettle(); + + // Verify that the onTap callback was called with the new index. + expect(tappedIndex, 2); + }); + + testWidgets('CarouselView with enableSplash true - children are not directly interactive', (WidgetTester tester) async { + bool buttonPressed = false; + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: CarouselView( + itemExtent: 350, + children: List.generate(3, (int index) { + return Center( + child: ElevatedButton( + onPressed: () => buttonPressed = true, + child: Text('Button $index'), + ), + ); + }), + ), + ), + ) + ); + await tester.pumpAndSettle(); + + await tester.tap(find.text('Button 1'), warnIfMissed: false); + expect(buttonPressed, isFalse); + }); + + testWidgets('CarouselView with enableSplash false - children are directly interactive', (WidgetTester tester) async { + bool buttonPressed = false; + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: CarouselView( + itemExtent: 350, + enableSplash: false, + children: List.generate(3, (int index) { + return Center( + child: ElevatedButton( + onPressed: () => buttonPressed = true, + child: Text('Button $index'), + ), + ); + }), + ), + ), + ) + ); + await tester.pumpAndSettle(); + + await tester.tap(find.text('Button 1')); + expect(buttonPressed, isTrue); + }); + + + testWidgets('CarouselView with enableSplash false - container is clickable without triggering children onTap', (WidgetTester tester) async { + int tappedIndex = -1; + bool buttonPressed = false; + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: CarouselView( + itemExtent: 350, + enableSplash: false, + onTap: (int index) { + tappedIndex = index; + }, + children: List.generate(3, (int index) { + return Column( + children: [ + Text('Item $index'), + ElevatedButton( + onPressed: () => buttonPressed = true, + child: Text('Button $index'), + ), + ], + ); + }), + ), + ), + ) + ); + await tester.pumpAndSettle(); + + final Finder carouselItem = find.text('Item 1'); + await tester.tap(carouselItem, warnIfMissed: false); + await tester.pumpAndSettle(); + + expect(tappedIndex, 1); + expect(buttonPressed,false); + + final Finder anotherCarouselItem = find.text('Item 2'); + await tester.tap(anotherCarouselItem, warnIfMissed: false); + await tester.pumpAndSettle(); + + expect(tappedIndex, 2); + expect(buttonPressed,false); + + await tester.tap(find.text('Button 1'), warnIfMissed: false); + expect(buttonPressed, isTrue); + }); } Finder getItem(int index) {