Fix changing DraggableScrollableSheet controller (#111445)
This commit is contained in:
parent
eae9bba77b
commit
0322b57796
@ -663,6 +663,10 @@ class _DraggableScrollableSheetState extends State<DraggableScrollableSheet> {
|
|||||||
@override
|
@override
|
||||||
void didUpdateWidget(covariant DraggableScrollableSheet oldWidget) {
|
void didUpdateWidget(covariant DraggableScrollableSheet oldWidget) {
|
||||||
super.didUpdateWidget(oldWidget);
|
super.didUpdateWidget(oldWidget);
|
||||||
|
if (widget.controller != oldWidget.controller) {
|
||||||
|
oldWidget.controller?._detach();
|
||||||
|
widget.controller?._attach(_scrollController);
|
||||||
|
}
|
||||||
_replaceExtent(oldWidget);
|
_replaceExtent(oldWidget);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -715,7 +719,9 @@ class _DraggableScrollableSheetState extends State<DraggableScrollableSheet> {
|
|||||||
_scrollController.extent = _extent;
|
_scrollController.extent = _extent;
|
||||||
// If an external facing controller was provided, let it know that the
|
// If an external facing controller was provided, let it know that the
|
||||||
// extent has been replaced.
|
// extent has been replaced.
|
||||||
|
if (widget.controller == oldWidget.controller) {
|
||||||
widget.controller?._onExtentReplaced(previousExtent);
|
widget.controller?._onExtentReplaced(previousExtent);
|
||||||
|
}
|
||||||
if (widget.snap
|
if (widget.snap
|
||||||
&& (widget.snap != oldWidget.snap || widget.snapSizes != oldWidget.snapSizes)
|
&& (widget.snap != oldWidget.snap || widget.snapSizes != oldWidget.snapSizes)
|
||||||
&& _scrollController.hasClients
|
&& _scrollController.hasClients
|
||||||
|
@ -1502,4 +1502,48 @@ void main() {
|
|||||||
// DraggableScrollableSheet has rebuilt, so expect the builder to be called.
|
// DraggableScrollableSheet has rebuilt, so expect the builder to be called.
|
||||||
expect(buildCount, 2);
|
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);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user