Skip to content

Commit 8af3c74

Browse files
Add WidgetStatesController support to ExpansionTile (#181238)
This PR adds support for providing a `WidgetStatesController` to `ExpansionTile` and forwards it to the backing `ListTile`. This allows callers to observe and control hovered, pressed, and focused states of the tile header. The change includes updated documentation with a snippet example and adds test coverage for both provided and null `statesController` cases. Fixes #180161 ## Pre-launch Checklist - [x] I read the Contributor Guide and followed the process outlined there for submitting PRs. - [x] I read the Tree Hygiene wiki page, which explains my responsibilities. - [x] I read and followed the Flutter Style Guide, including Features we expect every widget to implement. - [x] I signed the CLA. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making. - [ ] I followed the breaking change policy and added Data Driven Fixes where supported. - [ ] All existing and new tests are passing.
1 parent 5329e94 commit 8af3c74

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

‎packages/flutter/lib/src/material/expansion_tile.dart‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ class ExpansionTile extends StatefulWidget {
152152
this.enabled = true,
153153
this.expansionAnimationStyle,
154154
this.internalAddSemanticForOnTap = false,
155+
this.statesController,
155156
}) : assert(
156157
expandedCrossAxisAlignment != CrossAxisAlignment.baseline,
157158
'CrossAxisAlignment.baseline is not supported since the expanded children '
@@ -484,6 +485,16 @@ class ExpansionTile extends StatefulWidget {
484485
// the default value to true.
485486
final bool internalAddSemanticForOnTap;
486487

488+
/// The controller that notifies when the widget's [WidgetState]s change.
489+
///
490+
/// This allows listening to and controlling states such as
491+
/// [WidgetState.hovered], [WidgetState.focused], [WidgetState.pressed],
492+
/// and [WidgetState.disabled] for the tile's header.
493+
///
494+
/// If null, the backing [ListTile] will create and manage its own
495+
/// [WidgetStatesController].
496+
final WidgetStatesController? statesController;
497+
487498
@override
488499
State<ExpansionTile> createState() => _ExpansionTileState();
489500
}
@@ -626,6 +637,7 @@ class _ExpansionTileState extends State<ExpansionTile> {
626637
: null,
627638
minTileHeight: widget.minTileHeight,
628639
internalAddSemanticForOnTap: widget.internalAddSemanticForOnTap,
640+
statesController: widget.statesController,
629641
),
630642
);
631643

‎packages/flutter/test/material/expansion_tile_test.dart‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,4 +2158,31 @@ void main() {
21582158
variant: const TargetPlatformVariant(<TargetPlatform>{TargetPlatform.android}),
21592159
);
21602160
});
2161+
2162+
testWidgets('ExpansionTile forwards statesController to ListTile', (tester) async {
2163+
final controller = WidgetStatesController();
2164+
addTearDown(controller.dispose);
2165+
2166+
await tester.pumpWidget(
2167+
MaterialApp(
2168+
home: Material(
2169+
child: ExpansionTile(title: const Text('Tile'), statesController: controller),
2170+
),
2171+
),
2172+
);
2173+
2174+
final ListTile listTile = tester.widget<ListTile>(find.byType(ListTile));
2175+
expect(listTile.statesController, controller);
2176+
});
2177+
2178+
testWidgets('ExpansionTile forwards null statesController to ListTile', (tester) async {
2179+
await tester.pumpWidget(
2180+
const MaterialApp(
2181+
home: Material(child: ExpansionTile(title: Text('Tile'))),
2182+
),
2183+
);
2184+
2185+
final ListTile listTile = tester.widget<ListTile>(find.byType(ListTile));
2186+
expect(listTile.statesController, isNull);
2187+
});
21612188
}

0 commit comments

Comments
 (0)