Skip to content
Tutorials Center
My learning

Topics

Lesson 49 of 73 ยท Platform integration

Flutter on Web and Desktop

Run one Flutter codebase on the web, Windows, macOS and Linux: renderers, WebAssembly, URL strategy, window sizing, mouse and keyboard input, limits and SEO.

The same Dart code that runs on a phone can also run in a browser tab and as a native desktop app. That is the promise of "one codebase", and it mostly holds, but a desktop window and a web page are not a phone. They have a mouse, a keyboard, resizable windows, URLs and search engines. An app that ignores those feels like a stretched phone app.

This lesson explains how Flutter renders on each target, what to change for web and desktop, and when Flutter is the wrong tool for a web project.

Real vs stand-in. Commands and Dart code here are real and read-only: the site cannot build Flutter. The live examples are JavaScript stand-ins for the decision logic: picking a layout from a window width, mapping keyboard shortcuts, and URL path handling.

Where Flutter runs

Mobile Android and iOS.
Web Chrome, Safari, Firefox, Edge.
Desktop Windows, macOS, Linux.
1 / 2

Enabling and running a target

Check which targets your SDK supports and add platform folders to an existing project:

Terminal
flutter devices                         # lists chrome, windows, macos, linux, phones...
flutter create --platforms=web,windows,macos,linux .   # adds web/, windows/, ...
flutter run -d chrome
flutter run -d windows

Building for Windows needs Visual Studio with the C++ workload, macOS needs Xcode, and Linux needs a few system packages (clang, cmake, GTK). flutter doctor (Setting up) tells you what is missing. You can only build a target on its own OS, except web, which builds anywhere.

Flutter on the web

A Flutter web app does not turn widgets into HTML. It ships a rendering engine and paints everything on a canvas, just like on mobile. That is why the pixels match across browsers, and also why the page is heavier than a normal website.

Renderers and WebAssembly

  • CanvasKit (the default): Skia compiled to WebAssembly and drawing through WebGL. Highest fidelity and speed; the download is a few megabytes for the engine.
  • Skwasm (WebAssembly build): the engine compiled for use with flutter build web --wasm, where the Dart code itself is compiled to WebAssembly (WasmGC). It is faster, can render off the main thread, and is available in browsers that support WasmGC; the build falls back to the JavaScript output where it can't run.
  • The older HTML renderer, which mapped to DOM elements, has been removed from current SDKs, so don't rely on guides that mention it.
Terminal
flutter build web --release            # JavaScript output in build/web
flutter build web --wasm               # WebAssembly build, with fallback

The output is static files (index.html, main.dart.js, assets, flutter_bootstrap.js). Host it anywhere: Firebase Hosting, Netlify, GitHub Pages, S3 or your own server. Configure the server to return index.html for unknown paths so deep links work. If you host under a sub-path, build with --base-href /app/.

URL strategy

By default a Flutter web app uses a hash strategy: https://site.com/#/products/7. The hash keeps everything working on any static host. To get clean paths (/products/7), switch to the path strategy before runApp:

DART
import 'package:flutter_web_plugins/url_strategy.dart';

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

Then the server must serve index.html for every path. A router that understands URLs, such as go_router (Declarative routing), turns the address into the right screen and supports the browser's back and forward buttons.

JS Stand-in: path to screen, and the fallback rule

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Web limitations and SEO

This is the honest part. Because the page is a canvas:

  • Search engines see little. A Flutter web page starts as an almost empty HTML document, and the content appears after the app loads. Google can run JavaScript, but indexing a canvas app is unreliable, and there is no server-side rendering. Meta tags per page need extra work.
  • First load is heavy. The engine and app are several megabytes, with a startup delay before anything shows. Static sites do not have that cost.
  • Browser features differ. Text selection, right-click menus, find-in-page, translation and password managers may need extra handling (SelectionArea, AutofillGroup). Accessibility on the web depends on the semantics tree being enabled.
  • Some packages and APIs are missing: dart:io, dart:ffi and many plugins do not work. Guard code with kIsWeb.

Flutter web is a great fit for application-like experiences behind a login: dashboards, admin tools, internal apps, interactive tools and PWAs that share code with a mobile app. It is a poor fit for content websites, blogs, shops that depend on organic search, or landing pages. For those, use a normal website (HTML, or a framework with server rendering) and link to the app.

Flutter on desktop

Desktop apps compile to native code (ahead of time), so they start fast and have no browser overhead. You get file system access (dart:io), plugins such as path_provider, file_picker and url_launcher, and system integration through packages: window_manager for window size, title and position, tray_manager for the tray icon, and flutter_acrylic for translucent windows.

DART
import 'package:window_manager/window_manager.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await windowManager.ensureInitialized();
  await windowManager.waitUntilReadyToShow(
    const WindowOptions(
      size: Size(1100, 720),
      minimumSize: Size(640, 480),
      center: true,
      title: 'Notes',
    ),
    () async {
      await windowManager.show();
      await windowManager.focus();
    },
  );
  runApp(const NotesApp());
}

Set a minimum size, because a user can drag the window to any size and your layout must survive it. Use responsive layout rules from Responsive and adaptive layouts: a navigation rail or a sidebar on wide windows instead of a bottom bar.

Mouse and keyboard

Desktop and web users expect hover feedback, right-click menus, scrolling with the wheel, text selection and shortcuts.

DART
// Hover and cursor
MouseRegion(
  cursor: SystemMouseCursors.click,
  onEnter: (_) => setState(() => _hovered = true),
  onExit: (_) => setState(() => _hovered = false),
  child: card,
)

// Keyboard shortcuts bound to intents and actions
Shortcuts(
  shortcuts: const {
    SingleActivator(LogicalKeyboardKey.keyS, control: true): SaveIntent(),
    SingleActivator(LogicalKeyboardKey.keyS, meta: true): SaveIntent(),   // macOS
  },
  child: Actions(
    actions: {SaveIntent: CallbackAction<SaveIntent>(onInvoke: (_) => save())},
    child: Focus(autofocus: true, child: editor),
  ),
)

Shortcuts maps key combinations to intents; Actions maps intents to code, so the same action can also be triggered from a menu. macOS uses the Command key (meta) where Windows and Linux use Control. Other helpers: PlatformMenuBar for the native menu bar on macOS, SelectionArea for selectable text, Scrollbar (visible on desktop), and FocusTraversalGroup for Tab navigation.

JS Stand-in: shortcut map and adaptive layout

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Packaging desktop apps

flutter build windows (or macos, linux) writes a release bundle. For distribution you package it: an MSIX or installer for Windows, a signed and notarized .app or .dmg for macOS, and a Snap, AppImage or .deb for Linux. macOS and Windows warn users about unsigned apps, so plan for code-signing certificates. The release steps for stores are similar in spirit to those in the publishing chapter.

Writing code that works everywhere

  • Test a wide range of window sizes early; a phone-only layout looks broken on a 27-inch monitor. Constrain content with ConstrainedBox and a max width.
  • Use defaultTargetPlatform and kIsWeb (from package:flutter/foundation.dart) for branching, and never import dart:io in code that must compile for the web; use conditional imports or package:universal_io.
  • Check pub.dev's platform badges before adding a plugin; remember that platform channels need a native half per target.
  • Do not rely on touch-only gestures: add hover, right-click and keyboard alternatives.
  • Run flutter test plus a smoke run on each target in CI.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How does Flutter web draw an app?

    Flutter ships its engine (CanvasKit or Skwasm) and paints pixels, which keeps looks consistent but makes pages heavier and less SEO-friendly.
  2. 2. What does usePathUrlStrategy() change?

    Path strategy gives clean URLs, but static hosting must fall back to index.html so deep links load.
  3. 3. Which kind of web project is Flutter best suited for?

    Application-like experiences benefit from shared code; content sites need indexable, lightweight HTML.
  4. 4. Which key does a macOS 'Save' shortcut usually use instead of Control?

    macOS conventions use Command, so list both SingleActivator combinations.
  5. 5. Why set a minimum window size on desktop?

    A resizable window makes tiny sizes possible; minimumSize keeps the UI usable.

Summary

  • Flutter targets web, Windows, macOS and Linux from one codebase; desktop builds need the matching OS and toolchain.
  • Web apps paint on a canvas (CanvasKit, or the WebAssembly build), so they are consistent but heavy and weak for SEO: use Flutter web for apps, not content sites.
  • Choose hash or path URL strategy and make the server fall back to index.html.
  • Desktop needs window sizing (window_manager), a minimum size, hover, right-click, scrolling and keyboard shortcuts.
  • Branch with kIsWeb and defaultTargetPlatform, check plugin platform support, and test on every target.

Next: Playing Video and Audio