Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[web] Pass creation params to the platform view factory #128146

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

Merged
merged 2 commits into from
Jun 15, 2023

Conversation

mdebbar
Copy link
Contributor

@mdebbar mdebbar commented Jun 2, 2023

This concludes step 1 of the HtmlElementView improvements. It's now possible to pass creation params to platform view factories directly from HtmlElementView.

Here's a sample app using a single factory to render platform views in different colors:

Code sample
import 'dart:js_interop';
import 'dart:ui_web' as ui_web;
import 'package:flutter/material.dart';
import 'package:web/web.dart' as web;

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
  return MaterialApp(
    title: 'Platform View Demo',
    home: Scaffold(
      appBar: AppBar(
        title: Text('Platform View Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            BoxWrapper('red'),
            BoxWrapper(null),
            BoxWrapper('blue'),
          ],
        ),
      ),
    ),
  );
}
}

bool isRegistered = false;

class BoxWrapper extends StatelessWidget {
const BoxWrapper(this.cssColor);

final String? cssColor;

void register() {
  if (isRegistered) return;
  isRegistered = true;

  ui_web.platformViewRegistry.registerViewFactory('my-platform-view', (
    id, {
    Object? params,
  }) {
    params as String?;

    final element = web.document.createElement('div'.toJS) as web.HTMLElement;
    element.textContent = 'Platform View'.toJS;
    element.style
      ..lineHeight = '100px'.toJS
      ..fontSize = '24px'.toJS
      ..backgroundColor = (params ?? 'pink').toJS
      ..textAlign = 'center'.toJS;
    return element;
  });
}

@override
Widget build(BuildContext context) {
  register();
  return SizedBox(
    width: 200,
    height: 100,
    child: Card(
      child: HtmlElementView(
        viewType: 'my-platform-view',
        creationParams: cssColor,
      ),
    ),
  );
}
}

image

Depends on flutter/engine#42255

Part of #127030

@mdebbar mdebbar requested a review from ditman June 2, 2023 21:22
@flutter-dashboard flutter-dashboard bot added the framework flutter/packages/flutter repository. See also f: labels. label Jun 2, 2023
Copy link
Member

@ditman ditman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this might be a breaking change if people have extends HtmlElementView, I guess the customer tests will tell!

This is great, thanks for the PR!!

@@ -345,6 +345,7 @@ class HtmlElementView extends StatelessWidget {
super.key,
required this.viewType,
this.onPlatformViewCreated,
this.creationParams,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new parameter needs a little bit of documentation on the docblock, because it's published here.

(Also, does the docblock represent how the widget works? the contents are a little bit weird)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new parameter needs a little bit of documentation on the docblock, because it's published here.

Do you have some ideas of what we can add to the docs?

(Also, does the docblock represent how the widget works? the contents are a little bit weird)

Not sure what you mean. What I'm trying to do in the docs is explain the connection between this and the view factory since that's the important part for users to understand. Everything else that happens inside this widget (e.g. the controller) is an implementation detail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind, my rant is more about the documentation of the class HtmlElementView, your documentation for the params object seems OK to me.

@mdebbar
Copy link
Contributor Author

mdebbar commented Jun 5, 2023

I wonder if this might be a breaking change if people have extends HtmlElementView, I guess the customer tests will tell!

Looks like everything is green except Google testing. The failures seem to be related to app life cycle which has nothing to do with my changes. The roll hasn't happened in many days and that might be causing these failures.

Speaking of extends HtmlElementView, is it a common pattern for widgets to extend each other?

@iamzhibo
Copy link

iamzhibo commented Jun 6, 2023

May I ask if you will have problems using the lower version of HtmlElementView?Below ios13 system

@mdebbar mdebbar mentioned this pull request Jun 6, 2023
4 tasks
Copy link
Member

@ditman ditman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flutter/engine#42255 seems to have already landed into the framework here: 39adf1b

This should be landable!

@ditman
Copy link
Member

ditman commented Jun 15, 2023

May I ask if you will have problems using the lower version of HtmlElementView?Below ios13 system

@iamzhibo The new parameter is optional, and this is web-only, I don't think this has anything to do with the version of iOS. What's your concern?

@mdebbar mdebbar added the autosubmit Merge PR when tree becomes green via auto submit App label Jun 15, 2023
@auto-submit auto-submit bot merged commit b0188cd into flutter:master Jun 15, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 15, 2023
auto-submit bot pushed a commit to flutter/packages that referenced this pull request Jun 15, 2023
flutter/flutter@95be76a...b0188cd

2023-06-15 [email protected] [web] Pass creation params to the platform view factory (flutter/flutter#128146)
2023-06-15 [email protected] [flutter_tools] cache flutter sdk version to disk (flutter/flutter#124558)
2023-06-14 [email protected] Fix inconsistently suffixed macOS flavored bundle directory (flutter/flutter#127997)
2023-06-14 [email protected] Update golden tests for material (flutter/flutter#128839)
2023-06-14 [email protected] Update getChildrenSummaryTree to handle Diagnosticable as input. (flutter/flutter#128833)
2023-06-14 [email protected] Improve the error message for non-normalized constraints (flutter/flutter#127906)
2023-06-14 [email protected] ContextAction.isEnabled needs a context (flutter/flutter#127721)
2023-06-14 [email protected] Remove temporary default case for PointerSignalKind (flutter/flutter#128900)
2023-06-14 [email protected] Respect allowlisted count of leaks. (flutter/flutter#128823)
2023-06-14 [email protected] Unpin flutter_plugin_android_lifecycle (flutter/flutter#128898)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://bugs.chromium.org/p/skia/issues/entry?template=Autoroller+Bug

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 16, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 17, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 17, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants