From af79b4c7a3a425814eabc8df32708b1426643f5c Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Mon, 21 Aug 2023 23:13:22 +0300 Subject: [PATCH] Update `ExpansionPanel` example for the updated `expansionCallback` callback (#132837) fixes [ExpansionPanelList can't expand/collapse on the latest stable/master ](https://github.com/flutter/flutter/issues/132759) https://github.com/flutter/flutter/pull/128082 updated the `expansionCallback` and also the `ExpansionPanel` sample in the Flutter gallery but not the API example. This PR fixes the API example and adds tests. --- dev/bots/check_code_samples.dart | 1 - .../expansion_panel_list.0.dart | 2 +- .../expansion_panel_list.0_test.dart | 75 +++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 examples/api/test/material/expansion_panel/expansion_panel_list.0_test.dart diff --git a/dev/bots/check_code_samples.dart b/dev/bots/check_code_samples.dart index 0ffd56e510..792fe54224 100644 --- a/dev/bots/check_code_samples.dart +++ b/dev/bots/check_code_samples.dart @@ -306,7 +306,6 @@ final Set _knownMissingTests = { 'examples/api/test/material/icon_button/icon_button.3_test.dart', 'examples/api/test/material/icon_button/icon_button.0_test.dart', 'examples/api/test/material/icon_button/icon_button.1_test.dart', - 'examples/api/test/material/expansion_panel/expansion_panel_list.0_test.dart', 'examples/api/test/material/expansion_panel/expansion_panel_list.expansion_panel_list_radio.0_test.dart', 'examples/api/test/material/input_decorator/input_decoration.1_test.dart', 'examples/api/test/material/input_decorator/input_decoration.prefix_icon_constraints.0_test.dart', diff --git a/examples/api/lib/material/expansion_panel/expansion_panel_list.0.dart b/examples/api/lib/material/expansion_panel/expansion_panel_list.0.dart index 740250e4f6..6fb02ad074 100644 --- a/examples/api/lib/material/expansion_panel/expansion_panel_list.0.dart +++ b/examples/api/lib/material/expansion_panel/expansion_panel_list.0.dart @@ -67,7 +67,7 @@ class _ExpansionPanelListExampleState extends State { return ExpansionPanelList( expansionCallback: (int index, bool isExpanded) { setState(() { - _data[index].isExpanded = !isExpanded; + _data[index].isExpanded = isExpanded; }); }, children: _data.map((Item item) { diff --git a/examples/api/test/material/expansion_panel/expansion_panel_list.0_test.dart b/examples/api/test/material/expansion_panel/expansion_panel_list.0_test.dart new file mode 100644 index 0000000000..fc64f447c9 --- /dev/null +++ b/examples/api/test/material/expansion_panel/expansion_panel_list.0_test.dart @@ -0,0 +1,75 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/material.dart'; +import 'package:flutter_api_samples/material/expansion_panel/expansion_panel_list.0.dart' as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('ExpansionPanel can be expanded', (WidgetTester tester) async { + await tester.pumpWidget( + const example.ExpansionPanelListExampleApp(), + ); + + // Verify the first tile is collapsed. + expect(tester.widget(find.byType(ExpandIcon).first).isExpanded, false); + + // Tap to expand the first tile. + await tester.tap(find.byType(ExpandIcon).first); + await tester.pumpAndSettle(); + + // Verify that the first tile is expanded. + expect(tester.widget(find.byType(ExpandIcon).first).isExpanded, true); + }); + + testWidgets('Tap to delete a ExpansionPanel', (WidgetTester tester) async { + const int index = 3; + + await tester.pumpWidget( + const example.ExpansionPanelListExampleApp(), + ); + + expect(find.widgetWithText(ListTile, 'Panel $index'), findsOneWidget); + expect(tester.widget(find.byType(ExpandIcon).at(index)).isExpanded, false); + + // Tap to expand the tile at index 3. + await tester.tap(find.byType(ExpandIcon).at(index)); + await tester.pumpAndSettle(); + + expect(tester.widget(find.byType(ExpandIcon).at(index)).isExpanded, true); + + // Tap to delete the tile at index 3. + await tester.tap(find.byIcon(Icons.delete).at(index)); + await tester.pumpAndSettle(); + + // Verify that the tile at index 3 is deleted. + expect(find.widgetWithText(ListTile, 'Panel $index'), findsNothing); + }); + + testWidgets('ExpansionPanelList is scrollable', (WidgetTester tester) async { + await tester.pumpWidget( + const example.ExpansionPanelListExampleApp(), + ); + + expect(find.byType(SingleChildScrollView), findsOneWidget); + + // Expand all the tiles. + for (int i = 0; i < 8; i++) { + await tester.tap(find.byType(ExpandIcon).at(i)); + } + await tester.pumpAndSettle(); + + // Check panel 3 tile position. + Offset tilePosition = tester.getBottomLeft(find.widgetWithText(ListTile, 'Panel 3')); + expect(tilePosition.dy, 656.0); + + // Scroll up. + await tester.drag(find.byType(SingleChildScrollView), const Offset(0, -300)); + await tester.pumpAndSettle(); + + // Verify panel 3 tile position is updated after scrolling. + tilePosition = tester.getBottomLeft(find.widgetWithText(ListTile, 'Panel 3')); + expect(tilePosition.dy, 376.0); + }); +}