diff --git a/dev/integration_tests/flutter_gallery/lib/demo/material/expansion_panels_demo.dart b/dev/integration_tests/flutter_gallery/lib/demo/material/expansion_panels_demo.dart index dcd2e687b0..86e0ca50e8 100644 --- a/dev/integration_tests/flutter_gallery/lib/demo/material/expansion_panels_demo.dart +++ b/dev/integration_tests/flutter_gallery/lib/demo/material/expansion_panels_demo.dart @@ -345,7 +345,7 @@ class _ExpansionPanelsDemoState extends State { child: ExpansionPanelList( expansionCallback: (int index, bool isExpanded) { setState(() { - _demoItems[index].isExpanded = !isExpanded; + _demoItems[index].isExpanded = isExpanded; }); }, children: _demoItems.map((DemoItem item) { diff --git a/packages/flutter/lib/src/material/expansion_panel.dart b/packages/flutter/lib/src/material/expansion_panel.dart index e994ae85c9..409bb1c96c 100644 --- a/packages/flutter/lib/src/material/expansion_panel.dart +++ b/packages/flutter/lib/src/material/expansion_panel.dart @@ -313,9 +313,7 @@ class _ExpansionPanelListState extends State { return widget.children[index].isExpanded; } - void _handlePressed(bool isExpanded, int index) { - widget.expansionCallback?.call(index, isExpanded); - + void _handlePressed(bool isExpanded, int index) { if (widget._allowOnlyOnePanelOpen) { final ExpansionPanelRadio pressedChild = widget.children[index] as ExpansionPanelRadio; @@ -334,6 +332,8 @@ class _ExpansionPanelListState extends State { _currentOpenPanel = isExpanded ? null : pressedChild; }); } + // !isExpanded is passed because, when _handlePressed, the state of the panel to expand is not yet expanded. + widget.expansionCallback?.call(index, !isExpanded); } ExpansionPanelRadio? searchPanelByValue(List panels, Object? value) { diff --git a/packages/flutter/test/material/expansion_panel_test.dart b/packages/flutter/test/material/expansion_panel_test.dart index 1adbae5b3a..b699647ff5 100644 --- a/packages/flutter/test/material/expansion_panel_test.dart +++ b/packages/flutter/test/material/expansion_panel_test.dart @@ -144,7 +144,7 @@ void main() { expect(find.byType(ExpandIcon), findsOneWidget); await tester.tap(find.byType(ExpandIcon)); expect(capturedIndex, 0); - expect(capturedIsExpanded, isFalse); + expect(capturedIsExpanded, isTrue); box = tester.renderObject(find.byType(ExpansionPanelList)); expect(box.size.height, equals(oldHeight)); @@ -556,7 +556,7 @@ void main() { // Callback is invoked once with appropriate arguments expect(callbackHistory.length, equals(1)); expect(callbackHistory.last['index'], equals(1)); - expect(callbackHistory.last['isExpanded'], equals(false)); + expect(callbackHistory.last['isExpanded'], equals(true)); // Close the same panel await tester.tap(find.byType(ExpandIcon).at(1)); @@ -565,7 +565,7 @@ void main() { // Callback is invoked once with appropriate arguments expect(callbackHistory.length, equals(2)); expect(callbackHistory.last['index'], equals(1)); - expect(callbackHistory.last['isExpanded'], equals(true)); + expect(callbackHistory.last['isExpanded'], equals(false)); }); testWidgets('Radio mode calls expansionCallback twice if other panel open prior', (WidgetTester tester) async { @@ -630,7 +630,7 @@ void main() { expect(callbackHistory.length, equals(1)); callbackResults = callbackHistory[callbackHistory.length - 1]; expect(callbackResults['index'], equals(1)); - expect(callbackResults['isExpanded'], equals(false)); + expect(callbackResults['isExpanded'], equals(true)); // Close a different panel await tester.tap(find.byType(ExpandIcon).at(2)); @@ -639,13 +639,108 @@ void main() { // Callback is invoked the first time with correct arguments expect(callbackHistory.length, equals(3)); callbackResults = callbackHistory[callbackHistory.length - 2]; - expect(callbackResults['index'], equals(2)); + expect(callbackResults['index'], equals(1)); expect(callbackResults['isExpanded'], equals(false)); // Callback is invoked the second time with correct arguments callbackResults = callbackHistory[callbackHistory.length - 1]; - expect(callbackResults['index'], equals(1)); - expect(callbackResults['isExpanded'], equals(false)); + expect(callbackResults['index'], equals(2)); + expect(callbackResults['isExpanded'], equals(true)); + }); + + testWidgets('ExpansionPanelList.radio callback displays true or false based on the visibility of a list item', (WidgetTester tester) async { + late int lastExpanded; + bool topElementExpanded = false; + bool bottomElementExpanded = false; + + final List demoItemsRadio = [ + // topElement + ExpansionPanelRadio( + headerBuilder: (BuildContext context, bool isExpanded) { + return Text(isExpanded ? 'B' : 'A'); + }, + body: const SizedBox(height: 100.0), + value: 0, + ), + // bottomElement + ExpansionPanelRadio( + headerBuilder: (BuildContext context, bool isExpanded) { + return Text(isExpanded ? 'D' : 'C'); + }, + body: const SizedBox(height: 100.0), + value: 1, + ), + ]; + + final ExpansionPanelList expansionListRadio = ExpansionPanelList.radio( + children: demoItemsRadio, + expansionCallback: (int index, bool isExpanded) + { + lastExpanded = index; + if (index == 0) + { + topElementExpanded = isExpanded; + bottomElementExpanded = false; + } + else + { + topElementExpanded = false; + bottomElementExpanded = isExpanded; + } + } + ); + + await tester.pumpWidget( + MaterialApp( + home: SingleChildScrollView( + child: expansionListRadio, + ), + ), + ); + + // Initializes with all panels closed. + expect(find.text('A'), findsOneWidget); + expect(find.text('B'), findsNothing); + expect(find.text('C'), findsOneWidget); + expect(find.text('D'), findsNothing); + + await tester.tap(find.byType(ExpandIcon).at(0)); + await tester.pump(const Duration(milliseconds: 200)); + await tester.pumpAndSettle(); + + // Now the first panel is open. + expect(find.text('A'), findsNothing); + expect(find.text('B'), findsOneWidget); + expect(find.text('C'), findsOneWidget); + expect(find.text('D'), findsNothing); + + expect(lastExpanded,0); + expect(topElementExpanded,true); + + await tester.tap(find.byType(ExpandIcon).at(1)); + await tester.pump(const Duration(milliseconds: 200)); + await tester.pumpAndSettle(); + + // Open the other panel and ensure the first is now closed. + expect(lastExpanded,1); + expect(bottomElementExpanded,true); + expect(topElementExpanded,false); + expect(find.text('D'), findsOneWidget); + expect(find.text('A'), findsOneWidget); + + await tester.tap(find.byType(ExpandIcon).at(1)); + await tester.pump(const Duration(milliseconds: 200)); + await tester.pumpAndSettle(); + + // Close the item that was expanded should now be false. + expect(lastExpanded,1); + expect(bottomElementExpanded,false); + + // All panels should be closed. + expect(find.text('A'), findsOneWidget); + expect(find.text('B'), findsNothing); + expect(find.text('C'), findsOneWidget); + expect(find.text('D'), findsNothing); }); testWidgets(