Commit d7a4a54b authored by Adam Barth's avatar Adam Barth Committed by GitHub

TwoLevelSublist should have an onOpenChanged callback (#4848)

Also, fix an animation leak found by the test.

Fixes #4619
parent 9e111d18
......@@ -53,10 +53,17 @@ class TwoLevelListItem extends StatelessWidget {
}
class TwoLevelSublist extends StatefulWidget {
TwoLevelSublist({ Key key, this.leading, this.title, this.children }) : super(key: key);
TwoLevelSublist({
Key key,
this.leading,
this.title,
this.onOpenChanged,
this.children
}) : super(key: key);
final Widget leading;
final Widget title;
final ValueChanged<bool> onOpenChanged;
final List<Widget> children;
@override
......@@ -90,6 +97,12 @@ class _TwoLevelSublistState extends State<TwoLevelSublist> {
_controller.value = 1.0;
}
@override
void dispose() {
_controller.stop();
super.dispose();
}
void _handleOnTap() {
setState(() {
_isExpanded = !_isExpanded;
......@@ -99,6 +112,8 @@ class _TwoLevelSublistState extends State<TwoLevelSublist> {
_controller.reverse();
PageStorage.of(context)?.writeState(context, _isExpanded);
});
if (config.onOpenChanged != null)
config.onOpenChanged(_isExpanded);
}
Widget buildList(BuildContext context, Widget child) {
......
......@@ -66,4 +66,41 @@ void main() {
expect(getY(bottomKey) - getY(sublistKey), greaterThan(getHeight(topKey)));
expect(getY(bottomKey) - getY(sublistKey), greaterThan(getHeight(bottomKey)));
});
testWidgets('onOpenChanged callback', (WidgetTester tester) async {
bool didChangeOpen;
final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
'/': (_) {
return new Material(
child: new Viewport(
child: new TwoLevelList(
children: <Widget>[
new TwoLevelSublist(
title: new Text('Sublist'),
onOpenChanged: (bool opened) {
didChangeOpen = opened;
},
children: <Widget>[
new TwoLevelListItem(title: new Text('0')),
new TwoLevelListItem(title: new Text('1'))
]
),
]
)
)
);
}
};
await tester.pumpWidget(new MaterialApp(routes: routes));
expect(didChangeOpen, isNull);
await tester.tap(find.text('Sublist'));
expect(didChangeOpen, isTrue);
await tester.pump();
await tester.pump(const Duration(seconds: 1));
await tester.tap(find.text('Sublist'));
expect(didChangeOpen, isFalse);
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment