New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PlatformViewLink, handling creation of the PlatformViewSurface and dispose PlatformViewController #37703
Conversation
9fed1dd
to
75a5a3f
Compare
@@ -729,4 +729,7 @@ abstract class PlatformViewController { | |||
|
|||
/// Dispatches the `event` to the platform view. | |||
void dispatchPointerEvent(PointerEvent event); | |||
|
|||
/// Disposes the resources related to ths platform view from engine. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disposes the platform view.
The [PlatformViewController] is unusable after calling dispose
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@required this.onPlatformViewCreated}):assert(id != null), | ||
assert(onPlatformViewCreated != null); | ||
|
||
/// The auto generated id for the newly created platform view. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The unique identifier for the new platform view.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
/// [PlatformViewController.viewId] should match this id. | ||
final int id; | ||
|
||
/// Notifies when the PlatformView is ready. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Callback invoked after the platform view has been created.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
/// Widget build(BuildContext context) { | ||
/// return PlatformViewLink( | ||
/// createCallback: createFooWebView, | ||
/// surfaceFactory: (BuildContext context, PlatformViewController controller, int id) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess int id
is leftover from previous iteration?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
/// Constructs a [PlatformViewController]. | ||
/// | ||
/// The implementer of a new platform view is responsible to implement this method when constructing a [PlatformViewLink]. | ||
typedef CreatePlatformView = PlatformViewController Function(PlatformViewCreationParams params); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: CreatePlatformViewController
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
/// * [PlatformViewSurface] for details on the widget returned by `surfaceFactory`. | ||
/// * [PlatformViewCreationParams] for how each parameter can be used when implementing `createPlatformView`. | ||
const PlatformViewLink({ | ||
@required PlatformViewSurfaceFactory surfaceFactory, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a key
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
|
||
void _onPlatformViewCreated(int id) { | ||
_platformViewCreated = true; | ||
setState((){}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setState(() {
_platformViewCreated = true;
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
} | ||
|
||
@override | ||
void didChangeDependencies() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we override initState
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, also removed the _initialized flag.
/// } | ||
/// } | ||
/// ``` | ||
class PlatformViewLink extends StatefulWidget { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Document what happens when the widget is rebuild with a different create callback or a different surfaceFactory
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, with the change that made sure PlatformViewSurface
only created once.
await tester.pumpWidget(Container()); | ||
|
||
expect(disposedController.disposed, true); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add an equivalent test to Android view survives widget tree change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated with review comments.
@@ -729,4 +729,7 @@ abstract class PlatformViewController { | |||
|
|||
/// Dispatches the `event` to the platform view. | |||
void dispatchPointerEvent(PointerEvent event); | |||
|
|||
/// Disposes the resources related to ths platform view from engine. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@required this.onPlatformViewCreated}):assert(id != null), | ||
assert(onPlatformViewCreated != null); | ||
|
||
/// The auto generated id for the newly created platform view. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
/// [PlatformViewController.viewId] should match this id. | ||
final int id; | ||
|
||
/// Notifies when the PlatformView is ready. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
|
||
/// Constructs a [PlatformViewController]. | ||
/// | ||
/// The implementer of a new platform view is responsible to implement this method when constructing a [PlatformViewLink]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
/// Constructs a [PlatformViewController]. | ||
/// | ||
/// The implementer of a new platform view is responsible to implement this method when constructing a [PlatformViewLink]. | ||
typedef CreatePlatformView = PlatformViewController Function(PlatformViewCreationParams params); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
/// * [PlatformViewSurface] for details on the widget returned by `surfaceFactory`. | ||
/// * [PlatformViewCreationParams] for how each parameter can be used when implementing `createPlatformView`. | ||
const PlatformViewLink({ | ||
@required PlatformViewSurfaceFactory surfaceFactory, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
} | ||
|
||
@override | ||
void didChangeDependencies() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, also removed the _initialized flag.
|
||
void _onPlatformViewCreated(int id) { | ||
_platformViewCreated = true; | ||
setState((){}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
await tester.pumpWidget(Container()); | ||
|
||
expect(disposedController.disposed, true); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
/// } | ||
/// } | ||
/// ``` | ||
class PlatformViewLink extends StatefulWidget { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, with the change that made sure PlatformViewSurface
only created once.
|
||
const PlatformViewCreationParams._({ | ||
@required this.id, | ||
@required this.onPlatformViewCreated}):assert(id != null), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style nit: ) : assert
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
const PlatformViewCreationParams._({ | ||
@required this.id, | ||
@required this.onPlatformViewCreated}):assert(id != null), | ||
assert(onPlatformViewCreated != null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix indentation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
|
||
/// Callback invoked after the platform view has been created. | ||
/// | ||
/// Must be invoked as soon as the embedded platform view is ready. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it doesn't have to be called "as soon as" the platform view is created.
I'd skip this paragraph.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
/// It is required when constructing a [PlatformViewLink]. | ||
typedef PlatformViewSurfaceFactory = PlatformViewSurface Function(BuildContext context, PlatformViewController controller); | ||
|
||
///Constructs a [PlatformViewController]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: missing space
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
/// The factory to construct a [PlatformViewSurface]. | ||
/// | ||
/// It is required when constructing a [PlatformViewLink]. | ||
typedef PlatformViewSurfaceFactory = PlatformViewSurface Function(BuildContext context, PlatformViewController controller); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably want to make this return a widget.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
/// The factory to construct a [PlatformViewSurface]. | ||
/// | ||
/// It is required when constructing a [PlatformViewLink]. | ||
typedef PlatformViewSurfaceFactory = PlatformViewSurface Function(BuildContext context, PlatformViewController controller); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// A factory for a surface presenting a platform view as part of the widget hierarchy.
///
/// The returned widget should present the platform view associated with `controller`.
///
/// See also:
/// * [PlatformViewSurface], a common widget for presenting platform views.
typedef PlatformViewSurfaceFactory = Widget Function(BuildContext context, PlatformViewController controller);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
final CreatePlatformViewController _createPlatformViewController; | ||
|
||
@override | ||
State<StatefulWidget> createState() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style nit: use =>
if it fits in one line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated. Ready for another review.
|
||
const PlatformViewCreationParams._({ | ||
@required this.id, | ||
@required this.onPlatformViewCreated}):assert(id != null), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
const PlatformViewCreationParams._({ | ||
@required this.id, | ||
@required this.onPlatformViewCreated}):assert(id != null), | ||
assert(onPlatformViewCreated != null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
|
||
/// Callback invoked after the platform view has been created. | ||
/// | ||
/// Must be invoked as soon as the embedded platform view is ready. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
/// The factory to construct a [PlatformViewSurface]. | ||
/// | ||
/// It is required when constructing a [PlatformViewLink]. | ||
typedef PlatformViewSurfaceFactory = PlatformViewSurface Function(BuildContext context, PlatformViewController controller); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
/// The factory to construct a [PlatformViewSurface]. | ||
/// | ||
/// It is required when constructing a [PlatformViewLink]. | ||
typedef PlatformViewSurfaceFactory = PlatformViewSurface Function(BuildContext context, PlatformViewController controller); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
/// It is required when constructing a [PlatformViewLink]. | ||
typedef PlatformViewSurfaceFactory = PlatformViewSurface Function(BuildContext context, PlatformViewController controller); | ||
|
||
///Constructs a [PlatformViewController]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
final CreatePlatformViewController _createPlatformViewController; | ||
|
||
@override | ||
State<StatefulWidget> createState() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Description
Introduce
PlatformViewLink
which in this PR handles the creation and disposal of the platform view.Related Issues
#36779
Tests
I added the following tests:
'PlatformViewController Widget init, should create a SizedBox widget before onPlatformViewCreated and a PlatformViewSurface after'
'PlatformViewController Widget dispose'
Checklist
Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes (
[x]
). This will ensure a smooth and quick review process.///
).flutter analyze --flutter-repo
) does not report any problems on my PR.Breaking Change
Does your PR require Flutter developers to manually update their apps to accommodate your change?