When I thought about the content for Design Your City, I faced a dilemma.
I had an idea for 4 tabs.
– “Learn” tab: content, should be picked up by search engines.
– “Discover” and “Design”: CAD tools
– “Account”: log in/out for the CAD tools
My requirements were like this:
(Note: the website has slightly expanded now, with 6 tabs)
I knew from the official doc, and from my own experimentation with Flutter For Web on a previous project, that Flutter For Web is very well suited to make CAD tools, but not for SEO.
I really cared about the learning section of the website, but it made no sense to have it if it wasn’t going to have good SEO.
So I had no choice but to roll up my sleeves and do a couple of POCs to find out how it could all hang together.
My setup
After trial and error, I ended up with the following:
– I use plain JS for the tabs for the website.
– Everything is hosted on Firebase: the website is hosted on the main domain (I linked my domain to Firebase) and the web app is hosted on a Firebase subdomain.
– I have one Flutter project, which is an app with 3 tabs (“Discover”, “Design”, “Account”). When I develop the app, I have 3 tabs in the project, so I can navigate. BUT those tabs aren’t shown when I load it from the website.
– I use a single iFrame in my website index.html file. I use an event to tell the Flutter app which tab the website wants. This is that same event that also allows me to hide the tabs in the Flutter web app. So it’s the same instance of the web app running in the 3 tabs.
The website code
I found a great JS tutorial to set up tabs on a website. I use almost the same set up as in that tutorial.
The only difference from the tutorial is I have a div for each web tab (3 of them now on Design Your City: “Be Inspired”, “Learn”, and “About Us”), and a separate div for the Flutter web app. A separate div enables each web content tab to retain its content.
The 3 tabs that show the web app (“Discover”, “Design”, and “Account”) display the same div. The div has a simple iFrame, pointing to my Firebase subdomain where the Flutter app is deployed. The Flutter app retains the content for each tab, using a PageController.
To communicate with the Flutter web app, I use an event. This is run in a method openTab, which I trigger in the a tag, like this:
|
1 |
<a href="/learn" data-link class="tablinks" onclick="openTab('learn')">Learn</a> |
The code in openTab:
|
1 2 3 4 5 |
document.getElementById('content').contentWindow.postMessage({ sender: "web", message: tabName }, "*"); |
content is the id of my iFrame.
Now, that’s the web side sorted. Let’s move on to the Flutter web app part.
The Flutter web app code
My main widget is PagesScreen. In it, I use a PageController.
Let’s define the pages (basically, a list of widgets):
|
1 2 3 4 5 6 |
List<Widget> _pages = const [ DiscoverHomeScreen(), DesignHomeScreen(), AccountHomeScreen(), ]; |
It’s a StatefulWidget, and in the initState method, I listen for the web event (and I dispose of the listener in dispose).
By default, tabs are shown in web app. But, when I receive a web event, tabs are disabled.
Note: In the code below, you will notice getInstance –> this is simply my injection code. For this app, I use my own injection setup, but you could use whatever you want here, this blog post isn’t about arch or state.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
class PagesScreen extends StatefulWidget { const PagesScreen({super.key}); @override State<PagesScreen> createState() => _PagesScreenState(); } class _PagesScreenState extends State<PagesScreen> { int _pageIndex = 0; late PageController _pageController; bool _showTabs = true; @override void initState() { _pageController = PageController(initialPage: _pageIndex, keepPage: true); getInstance<WebEventListener>().startListening(_listen); super.initState(); } @override void dispose() { getInstance<WebEventListener>().stopListening(); super.dispose(); } void _listen(String? message) { if (message != null) { setState(() { _showTabs = false; }); getInstance<PagesBloc>().add(GoToPage( type: PagesType.values[getPagesTypeFromString(message).pageIndex()])); } } @override Widget build(BuildContext context) { // TODO } } |
WebEventListener is an abstract class as below:
|
1 2 3 4 5 6 7 8 |
typedef OnWebEventReceived = Function(String? message); abstract class WebEventListener { void startListening(OnWebEventReceived callback); void stopListening(); } |
My implementation class is as below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
// ignore: avoid_web_libraries_in_flutter import 'dart:html' as html; import 'package:flutter/foundation.dart'; import 'package:flutter_dyc/base/web/domain/web_events_listener.dart'; class WebEventsListenerImpl implements WebEventListener { late OnWebEventReceived _callback; void _listen(html.Event event) { try { var data = (event as html.MessageEvent).data; debugPrint("html event data $data"); if (data['message'] != null) { _callback(data['message']); } } catch (e, str) { debugPrint('Error parsing html event $e $str'); } } @override void startListening(OnWebEventReceived callback) { _callback = callback; html.window.addEventListener('message', _listen, true); } @override void stopListening() { html.window.removeEventListener('message', _listen, true); } } |
With that setup, I have tabs when I run the app on its own, but not when it is in the iFrame, as the website has tabs already.
Finally, I use a PageView to show the correct page. It uses a Bloc (PagesBloc) to show the correct page. This blog post isn’t about arch or state management, you can use whatever you like for that.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
@override Widget build(BuildContext context) { return BlocListener<PagesBloc, PagesState>( listenWhen: (oldState, newState) { return newState.type != oldState.type; }, listener: (context, state) { setState(() { _pageIndex = state.type.index; _pageController.jumpToPage(_pageIndex); }); }, child: Scaffold( bottomNavigationBar: _showTabs ? (MediaQuery.of(context).size.width < _navigationRailThreshold ? BottomNavigationBar(...) : null) : null, body: Row( mainAxisSize: MainAxisSize.max, children: [ if (MediaQuery.of(context).size.width >= _navigationRailThreshold && _showTabs) NavigationRail(...), Expanded( child: PageView( physics: const NeverScrollableScrollPhysics(), controller: _pageController, children: _pages, ), ), ], ), ), ); } |
All working….
Until I tried to run a test!
(Yes, I love tests – Unleash The Full Power of Flutter Widget Tests)
Tests don’t like dart:html
I got an error message new to me:
|
1 |
Error: Dart library 'dart:html' is not available on this platform. |
So, what to do?
Well, thankfully, I don’t need dart:html for the functionality itself, it is only used for listening to events from the website.
At first, I tried creating a second implementation class for WebEventListener but I got the same error. Even though I wasn’t using the class above (I was injecting a different class, which basically was doing nothing), it still tried to build that class I wasn’t using.
So, I’ve got a hack I don’t like, which is I comment code out. That means I need to change code when I deploy to Firebase, but I use the code below for development.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
// ignore: avoid_web_libraries_in_flutter // TODO - uncomment to build //import 'dart:html' as html; //import 'package:flutter/foundation.dart'; import 'package:flutter_dyc/base/web/domain/web_events_listener.dart'; class WebEventsListenerImpl implements WebEventListener { late OnWebEventReceived _callback; // TODO - delete to build @override void startListening(OnWebEventReceived callback) {} // TODO - delete to build @override void stopListening() {} // TODO - uncomment to build /** void _listen(html.Event event) { try { var data = (event as html.MessageEvent).data; debugPrint("html event data $data"); if (data['message'] != null) { _callback(data['message']); } } catch (e, str) { debugPrint('Error parsing html event $e $str'); } } @override void startListening(OnWebEventReceived callback) { _callback = callback; html.window.addEventListener('message', _listen, true); } @override void stopListening() { html.window.removeEventListener('message', _listen, true); }**/ } |
Do I like this? Hell no! But I absolutely want tests and dart:html functionality isn’t required for the web app to work, so I live with this hack.
Note: if someone knows of a better way, please let me know! I tried to run tests with flutter test --platform chrome but I got many errors, different ones, and so I decided to not bother.
As I’m working on Design Your City, I will write more blog posts about Flutter for Web. But, in a nutshell, I can say: “Thank you to Flutter team for enabling me to make web apps.”

Mobile app developer with 14 years experience. Started with the Android SDK in 2010 and switched to Flutter in 2017. Past apps range from start ups to large tech (Google) and non tech (British Airways) companies, in various sectors (transport, commercial operations, e-commerce).
You must be logged in to post a comment.