Embed Flutter CodePen projects everywhere! šŸ¤Æ

Mariano Zorrilla
Flutter Community
Published in
4 min readApr 28, 2020

WAIT! Did you knew this?! Did anyone knew this?! Did CodePen.IO knew about this?!

Okā€¦ lets backtrack a little bit. You can now make Flutter Web projects using CodePen

thatā€™s so cool! and I can use it to create amazing clone app like this WhatsApp one

Thatā€™s amazing! But then I noticed something. CodePen has different ā€œView Modesā€ā€¦ Ok, we have ā€œPenā€ (regular view, code side by side), ā€œFullā€ (full screen with CodePen bar on top) and ā€œDebugā€ (full screen)ā€¦ Why is this relevant? Weā€™ll get back with this in a second.

Embedded CodePen projects

At the bottom right of your CodePen project you have this magic button called ā€œEmbedā€

Once you get there, you have different ā€œembedā€ modes:

  • WordPress Shortcodes
  • Perfil Embed
  • iframe
  • HTML

Not only thatā€¦ you can directly embed CodePen projects like here by posting the link directly

Okā€¦ weā€™re getting somewhere. My first test was trying to embed a Flutter CodePen project inside a Website.

Flutter CodePen Embedded

thatā€™s not bad at allā€¦ but I want a full screen design, I donā€™t want any of those CodePen logosā€¦ Donā€™t get me wrong, but I want to show my examples like if it was an actual app running on web.

So, I came out with this solution, using that iframe with the ā€œDebugā€ preview URL (yep, this is where that preview goes handy). The result? The following:

I can use my Flutter CodePen project embedded on ANY WEBSITE, literally! That means you can live showcase your projects on your WordPress blogs, Blogger or even your own hand made ones.

But thenā€¦ something tick on me! What if I can embed a Flutter CodePen project on Flutter CodePen project?! šŸ˜³

You know, like a Flutter Inception šŸ¤ÆšŸ¤ÆšŸ¤Æ

But, how to do it?! šŸ¤·ā€ā™‚ļø

What youā€™re looking for is called ā€œplatformViewRegistryā€ this method is part of dart:ui library and part of the main library AKA you can use is on CodePen

With this following snippet, you can use it with a HtmlElementView part of dart:html library

// ignore: camel_case_types
class platformViewRegistry {
static registerViewFactory(String viewId, dynamic cb) {
// ignore:undefined_prefixed_name
ui.platformViewRegistry.registerViewFactory(viewId, cb);
}
}

My solution came really simple, all you need to do is pass a few parameters around and youā€™re fine:

final IFrameElement _iFrameElement = IFrameElement();
Widget _iFrameWidget;
void loadIframe() {
_iFrameElement.height = '100%';
_iFrameElement.width = '100%';
_iFrameElement.src = 'https://codepen.io/mkiisoft/debug/BaoWVrQ';
_iFrameElement.style.border = 'none';
_iFrameWidget = HtmlElementView(key: UniqueKey(), viewType: 'iframeElement');
platformViewRegistry.registerViewFactory('iframeElement', (int viewId) => _iFrameElement);
}

Then, you use ā€œ_iFrameWidgetā€ like any other widget! You can place it inside a SizedBox or similar to have constrained sizes (if you need it) and that it!

But wait! You can do something really cool, you can use a PNG image of a Phone, place your iframe with your Flutter CodePen project inside and make it looks like a real (and usable) mockup. Something like this:

and you can actually try it yourself here! šŸ¤“šŸ’»

This is something amazing! I havenā€™t had this much fun making apps in my life! Flutter takes your imagination into a whole different and new way.

Why donā€™t you give it a try? Hereā€™s how to start your own CodePen project

--

--

Mariano Zorrilla
Flutter Community

GDE Flutter ā€” Tech Lead ā€” Engineer Manager | Flutter | Android @ Venmo