Unverified Commit fe503dc4 authored by Amir Hardon's avatar Amir Hardon Committed by GitHub

Add creation parameters to UiKitView. (#23800)

parent 072cd948
...@@ -56,8 +56,8 @@ class AndroidView extends StatefulWidget { ...@@ -56,8 +56,8 @@ class AndroidView extends StatefulWidget {
/// ///
/// {@template flutter.widgets.platformViews.constructorParams} /// {@template flutter.widgets.platformViews.constructorParams}
/// The `viewType` and `hitTestBehavior` parameters must not be null. /// The `viewType` and `hitTestBehavior` parameters must not be null.
/// {@endtemplate}
/// If `creationParams` is not null then `creationParamsCodec` must not be null. /// If `creationParams` is not null then `creationParamsCodec` must not be null.
/// {@endtemplate}
AndroidView({ // ignore: prefer_const_constructors_in_immutables AndroidView({ // ignore: prefer_const_constructors_in_immutables
// TODO(aam): Remove lint ignore above once https://dartbug.com/34297 is fixed // TODO(aam): Remove lint ignore above once https://dartbug.com/34297 is fixed
Key key, Key key,
...@@ -193,8 +193,11 @@ class UiKitView extends StatefulWidget { ...@@ -193,8 +193,11 @@ class UiKitView extends StatefulWidget {
this.onPlatformViewCreated, this.onPlatformViewCreated,
this.hitTestBehavior = PlatformViewHitTestBehavior.opaque, this.hitTestBehavior = PlatformViewHitTestBehavior.opaque,
this.layoutDirection, this.layoutDirection,
this.creationParams,
this.creationParamsCodec,
}) : assert(viewType != null), }) : assert(viewType != null),
assert(hitTestBehavior != null), assert(hitTestBehavior != null),
assert(creationParams == null || creationParamsCodec != null),
super(key: key); super(key: key);
// TODO(amirh): reference the iOS API doc once avaliable. // TODO(amirh): reference the iOS API doc once avaliable.
...@@ -212,6 +215,19 @@ class UiKitView extends StatefulWidget { ...@@ -212,6 +215,19 @@ class UiKitView extends StatefulWidget {
/// {@macro flutter.widgets.platformViews.directionParam} /// {@macro flutter.widgets.platformViews.directionParam}
final TextDirection layoutDirection; final TextDirection layoutDirection;
/// Passed as the `arguments` argument of [-[FlutterPlatformViewFactory createWithFrame:viewIdentifier:arguments:]](/objcdoc/Protocols/FlutterPlatformViewFactory.html#/c:objc(pl)FlutterPlatformViewFactory(im)createWithFrame:viewIdentifier:arguments:)
///
/// This can be used by plugins to pass constructor parameters to the embedded Android view.
final dynamic creationParams;
/// The codec used to encode `creationParams` before sending it to the
/// platform side. It should match the codec returned by [-[FlutterPlatformViewFactory createArgsCodec:]](/objcdoc/Protocols/FlutterPlatformViewFactory.html#/c:objc(pl)FlutterPlatformViewFactory(im)createArgsCodec)
///
/// This is typically one of: [StandardMessageCodec], [JSONMessageCodec], [StringCodec], or [BinaryCodec].
///
/// This must not be null if [creationParams] is not null.
final MessageCodec<dynamic> creationParamsCodec;
@override @override
State<UiKitView> createState() => _UiKitViewState(); State<UiKitView> createState() => _UiKitViewState();
} }
...@@ -379,6 +395,8 @@ class _UiKitViewState extends State<UiKitView> { ...@@ -379,6 +395,8 @@ class _UiKitViewState extends State<UiKitView> {
id: _id, id: _id,
viewType: widget.viewType, viewType: widget.viewType,
layoutDirection: _layoutDirection, layoutDirection: _layoutDirection,
creationParams: widget.creationParams,
creationParamsCodec: widget.creationParamsCodec,
); );
if (!mounted) { if (!mounted) {
controller.dispose(); controller.dispose();
......
...@@ -179,6 +179,7 @@ class FakeIosPlatformViewsController { ...@@ -179,6 +179,7 @@ class FakeIosPlatformViewsController {
final Map<dynamic, dynamic> args = call.arguments; final Map<dynamic, dynamic> args = call.arguments;
final int id = args['id']; final int id = args['id'];
final String viewType = args['viewType']; final String viewType = args['viewType'];
final Uint8List creationParams = args['params'];
if (_views.containsKey(id)) { if (_views.containsKey(id)) {
throw PlatformException( throw PlatformException(
...@@ -194,7 +195,7 @@ class FakeIosPlatformViewsController { ...@@ -194,7 +195,7 @@ class FakeIosPlatformViewsController {
); );
} }
_views[id] = FakeUiKitView(id, viewType); _views[id] = FakeUiKitView(id, viewType, creationParams);
return Future<int>.sync(() => null); return Future<int>.sync(() => null);
} }
...@@ -272,10 +273,11 @@ class FakeAndroidMotionEvent { ...@@ -272,10 +273,11 @@ class FakeAndroidMotionEvent {
} }
class FakeUiKitView { class FakeUiKitView {
FakeUiKitView(this.id, this.type); FakeUiKitView(this.id, this.type, [this.creationParams]);
final int id; final int id;
final String type; final String type;
final Uint8List creationParams;
@override @override
bool operator ==(dynamic other) { bool operator ==(dynamic other) {
...@@ -283,7 +285,8 @@ class FakeUiKitView { ...@@ -283,7 +285,8 @@ class FakeUiKitView {
return false; return false;
final FakeUiKitView typedOther = other; final FakeUiKitView typedOther = other;
return id == typedOther.id && return id == typedOther.id &&
type == typedOther.type; type == typedOther.type &&
creationParams == typedOther.creationParams;
} }
@override @override
...@@ -291,6 +294,6 @@ class FakeUiKitView { ...@@ -291,6 +294,6 @@ class FakeUiKitView {
@override @override
String toString() { String toString() {
return 'FakeIosPlatformView(id: $id, type: $type)'; return 'FakeUiKitView(id: $id, type: $type, creationParams: $creationParams)';
} }
} }
...@@ -961,5 +961,43 @@ void main() { ...@@ -961,5 +961,43 @@ void main() {
); );
}); });
testWidgets('Create UIView with params', (WidgetTester tester) async {
final int currentViewId = platformViewsRegistry.getNextPlatformViewId();
final FakeIosPlatformViewsController viewsController = FakeIosPlatformViewsController();
viewsController.registerViewType('webview');
await tester.pumpWidget(
Center(
child: SizedBox(
width: 200.0,
height: 100.0,
child: UiKitView(
viewType: 'webview',
layoutDirection: TextDirection.ltr,
creationParams: 'creation parameters',
creationParamsCodec: const StringCodec(),
),
),
),
);
final FakeUiKitView fakeView = viewsController.views.first;
final Uint8List rawCreationParams = fakeView.creationParams;
final ByteData byteData = ByteData.view(
rawCreationParams.buffer,
rawCreationParams.offsetInBytes,
rawCreationParams.lengthInBytes
);
final dynamic actualParams = const StringCodec().decodeMessage(byteData);
expect(actualParams, 'creation parameters');
expect(
viewsController.views,
unorderedEquals(<FakeUiKitView>[
FakeUiKitView(currentViewId + 1, 'webview', fakeView.creationParams)
]),
);
});
}); });
} }
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