Skip to content

Commit 380c8b4

Browse files
Add ExpansibleController.toggle method. (flutter#181320)
Closes flutter#181319 ### Description - Adds `ExpansibleController.toggle` method - Adds test to verify that `ExpansibleController.toggle` works as expected ## 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, or this PR is [test-exempt]. - [X] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [X] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Victor Sanni <victorsanniay@gmail.com>
1 parent 57911ef commit 380c8b4

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

packages/flutter/lib/src/widgets/expansible.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,21 @@ class ExpansibleController extends ChangeNotifier {
126126
_setExpansionState(false);
127127
}
128128

129+
/// Convenience method for toggling the current [isExpanded] status.
130+
///
131+
/// Calling this method may cause the [Expansible] to rebuild, so it may not
132+
/// be called from a build method.
133+
///
134+
/// Calling this method will notify registered listeners of this controller
135+
/// that the expansion state has changed.
136+
///
137+
/// See also:
138+
///
139+
/// * [expand], which expands the [Expansible].
140+
/// * [collapse], which collapses the [Expansible].
141+
/// * [isExpanded] to check whether the [Expansible] is expanded.
142+
void toggle() => isExpanded ? collapse() : expand();
143+
129144
/// Finds the [ExpansibleController] for the closest [Expansible] instance
130145
/// that encloses the given context.
131146
///

packages/flutter/test/widgets/expansible_test.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,4 +511,34 @@ void main() {
511511
controller.expand();
512512
await tester.pump();
513513
});
514+
515+
testWidgets('Controller can be toggled', (WidgetTester tester) async {
516+
final controller = ExpansibleController();
517+
addTearDown(controller.dispose);
518+
519+
await tester.pumpWidget(
520+
MaterialApp(
521+
home: Expansible(
522+
controller: controller,
523+
bodyBuilder: (BuildContext context, Animation<double> animation) => const Text('Body'),
524+
headerBuilder: (BuildContext context, Animation<double> animation) =>
525+
GestureDetector(onTap: controller.toggle, child: const Text('Header')),
526+
),
527+
),
528+
);
529+
530+
expect(find.text('Body'), findsNothing);
531+
controller.toggle();
532+
await tester.pumpAndSettle();
533+
expect(find.text('Body'), findsOneWidget);
534+
535+
controller.toggle();
536+
await tester.pumpAndSettle();
537+
expect(find.text('Body'), findsNothing);
538+
539+
expect(find.text('Body'), findsNothing);
540+
controller.toggle();
541+
await tester.pumpAndSettle();
542+
expect(find.text('Body'), findsOneWidget);
543+
});
514544
}

0 commit comments

Comments
 (0)