Unverified Commit 0322b577 authored by Callum Moffat's avatar Callum Moffat Committed by GitHub

Fix changing DraggableScrollableSheet controller (#111445)

parent eae9bba7
......@@ -663,6 +663,10 @@ class _DraggableScrollableSheetState extends State<DraggableScrollableSheet> {
@override
void didUpdateWidget(covariant DraggableScrollableSheet oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.controller != oldWidget.controller) {
oldWidget.controller?._detach();
widget.controller?._attach(_scrollController);
}
_replaceExtent(oldWidget);
}
......@@ -715,7 +719,9 @@ class _DraggableScrollableSheetState extends State<DraggableScrollableSheet> {
_scrollController.extent = _extent;
// If an external facing controller was provided, let it know that the
// extent has been replaced.
widget.controller?._onExtentReplaced(previousExtent);
if (widget.controller == oldWidget.controller) {
widget.controller?._onExtentReplaced(previousExtent);
}
if (widget.snap
&& (widget.snap != oldWidget.snap || widget.snapSizes != oldWidget.snapSizes)
&& _scrollController.hasClients
......
......@@ -1502,4 +1502,48 @@ void main() {
// DraggableScrollableSheet has rebuilt, so expect the builder to be called.
expect(buildCount, 2);
});
testWidgets('DraggableScrollableSheet controller can be changed', (WidgetTester tester) async {
final DraggableScrollableController controller1 = DraggableScrollableController();
final DraggableScrollableController controller2 = DraggableScrollableController();
DraggableScrollableController controller = controller1;
await tester.pumpWidget(MaterialApp(
home: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) => Scaffold(
body: DraggableScrollableSheet(
initialChildSize: 0.25,
snap: true,
snapSizes: const <double>[0.25, 0.5, 1.0],
controller: controller,
builder: (BuildContext context, ScrollController scrollController) {
return ListView(
controller: scrollController,
children: <Widget>[
ElevatedButton(
onPressed: () => setState(() {
controller = controller2;
}),
child: const Text('Switch controller'),
),
Container(
height: 10000,
color: Colors.blue,
),
],
);
},
),
),
),
));
expect(controller1.isAttached, true);
expect(controller2.isAttached, false);
await tester.tap(find.text('Switch controller'));
await tester.pump();
expect(controller1.isAttached, false);
expect(controller2.isAttached, true);
});
}
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