PlatformView: recreate surface if the controller changes (#40280)
Currently the surface of a platform view is only created only one when the state of PlatformViewLink is created. When the PlatformViewLink widget changes, the PlatformViewController in the corresponding state is also updated. Just the surface is not updated even though it depends on the controller. This PR changes this behavior to recreate the surface whenever the controller is updated.
This commit is contained in:
@@ -846,6 +846,9 @@ class _PlatformViewLinkState extends State<PlatformViewLink> {
|
||||
|
||||
if (widget.viewType != oldWidget.viewType) {
|
||||
_controller?.dispose();
|
||||
// The _surface has to be recreated as its controller is disposed.
|
||||
// Setting _surface to null will trigger its creation in build().
|
||||
_surface = null;
|
||||
|
||||
// We are about to create a new platform view.
|
||||
_platformViewCreated = false;
|
||||
|
||||
@@ -2059,6 +2059,7 @@ void main() {
|
||||
testWidgets('PlatformViewLink re-initializes when view type changes', (WidgetTester tester) async {
|
||||
final int currentViewId = platformViewsRegistry.getNextPlatformViewId();
|
||||
final List<int> ids = <int>[];
|
||||
final List<int> surfaceViewIds = <int>[];
|
||||
final List<String> viewTypes = <String>[];
|
||||
|
||||
PlatformViewLink createPlatformViewLink(String viewType) {
|
||||
@@ -2072,6 +2073,7 @@ void main() {
|
||||
return controller;
|
||||
},
|
||||
surfaceFactory: (BuildContext context, PlatformViewController controller) {
|
||||
surfaceViewIds.add(controller.viewId);
|
||||
return PlatformViewSurface(
|
||||
gestureRecognizers: const <Factory<OneSequenceGestureRecognizer>>{},
|
||||
controller: controller,
|
||||
@@ -2107,6 +2109,13 @@ void main() {
|
||||
]),
|
||||
);
|
||||
|
||||
expect(
|
||||
surfaceViewIds,
|
||||
unorderedEquals(<int>[
|
||||
currentViewId+1, currentViewId+2,
|
||||
]),
|
||||
);
|
||||
|
||||
expect(
|
||||
viewTypes,
|
||||
unorderedEquals(<String>[
|
||||
|
||||
Reference in New Issue
Block a user