Add enableSplash parameter to CarouselView (#155214)
This PR fixes #154701 by adding a new parameter to the CarouselView widget that allows developers to control which parts of the carousel are clickable. Currently, an InkWell covers the entire carousel, preventing child widgets from being clickable even when children widget have their clickable widgets. The new `enableSplash` parameter allows developers to disable this default behavior of `InkWell` without breaking any existing code. If `enableSplash` is `false`, the `onTap` of the `CarouselView` will be disabled. And developer can define their own click behavior likes animation, the clickable widget and so on.
This commit is contained in:
@@ -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<int> 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<int>? 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<CarouselView> {
|
||||
return null;
|
||||
});
|
||||
|
||||
Widget contents = widget.children[index];
|
||||
if (widget.enableSplash) {
|
||||
contents = Stack(
|
||||
fit: StackFit.expand,
|
||||
children: <Widget>[
|
||||
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<CarouselView> {
|
||||
color: effectiveBackgroundColor,
|
||||
elevation: effectiveElevation,
|
||||
shape: effectiveShape,
|
||||
child: Stack(
|
||||
fit: StackFit.expand,
|
||||
children: <Widget>[
|
||||
widget.children[index],
|
||||
Material(
|
||||
color: Colors.transparent,
|
||||
child: InkWell(
|
||||
onTap: () {
|
||||
widget.onTap?.call(index);
|
||||
},
|
||||
overlayColor: effectiveOverlayColor,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: contents,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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<Widget>.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<Widget>.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<Widget>.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<Widget>.generate(3, (int index) {
|
||||
return Column(
|
||||
children: <Widget>[
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user