How to Create a Blurred Transparent Background in Your Flutter macOS Project

Sezer Ufuk Yavuz
4 min readJul 29, 2023

Flutter is a powerful framework for building cross-platform applications, including macOS desktop projects. However, implementing certain features, like a blurred transparent background, might seem challenging at first. In this article, we’ll guide you through the steps to achieve this effect by combining Flutter and macOS-specific code. Before we proceed, let’s briefly discuss the intricacies of working with Flutter on desktop projects and why understanding Swift integration is beneficial.

  1. The Challenges of Building Flutter Desktop Projects: Building Flutter applications for desktop platforms, such as macOS, comes with unique challenges. Unlike mobile devices, desktop applications have different user interface paradigms, screen sizes, and input methods. This means you’ll need to adapt your Flutter app to work seamlessly on desktop environments. However, once you grasp the differences and embrace platform-specific integrations, you can create powerful and responsive desktop applications.
  2. Leveraging Swift Integration in Flutter Projects: When working on macOS-specific features in your Flutter project, you might need to incorporate native code. Flutter allows you to use platform channels, enabling communication between Dart (Flutter) and native code (Swift/Objective-C on iOS/macOS). Understanding how to use Swift on the macOS side of your Flutter project can unlock a world of possibilities for adding platform-specific functionalities.

Let’s dive into the steps to create a blurred transparent background for your Flutter macOS project:

Step 1: Open Your Flutter Project Begin by opening your existing Flutter project, which should already be set up for macOS deployment.

Step 2: Open the Project in Xcode From your macOS file system, open your Flutter project in Xcode. This will allow us to edit the macOS-specific code required for the blurred background effect.

Step 3: Edit MainFlutterWindow.swift In Xcode, locate the file named “MainFlutterWindow.swift.” This file is essential for configuring the macOS window for your Flutter app. Open it and make the following modifications:


class MainFlutterWindow: NSWindow {

override func awakeFromNib() {

let flutterViewController = FlutterViewController.init()

//Add these code to your MainFlutterWindow.swift file
flutterViewController.backgroundColor = .clear



let visualEffectView = NSVisualEffectView()
visualEffectView.material = .underWindowBackground // The material of the view. This sets the blur effect.
visualEffectView.blendingMode = .behindWindow // The blending mode. This makes the blur effect appear behind the window.
visualEffectView.state = .active // The state. This makes the blur effect active.
flutterViewController.view.addSubview(visualEffectView, positioned: .below, relativeTo: flutterViewController.view)
visualEffectView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
visualEffectView.topAnchor.constraint(equalTo: flutterViewController.view.topAnchor),
visualEffectView.bottomAnchor.constraint(equalTo: flutterViewController.view.bottomAnchor),
visualEffectView.leadingAnchor.constraint(equalTo: flutterViewController.view.leadingAnchor),
visualEffectView.trailingAnchor.constraint(equalTo:flutterViewController.view.trailingAnchor),
])

let windowFrame = self.frame
self.contentViewController = flutterViewController
self.setFrame(windowFrame, display: true)
RegisterGeneratedPlugins(registry: flutterViewController)


super.awakeFromNib()
}

override public func order(_ place: NSWindow.OrderingMode, relativeTo otherWin: Int) {
super.order(place, relativeTo: otherWin)
hiddenWindowAtLaunch()
}
}

Step 4: Modify main.dart In your Flutter code (main.dart), you need to apply a transparent background to your app and overlay it with a blurred effect. Add the following lines:

   return const MacosApp(
color: Colors.transparent,
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.transparent,
body: App(),
),
);
    return MacosWindow(
backgroundColor: Colors.transparent,)

Step 5: Done! You’ve successfully created a blurred transparent background for your Flutter macOS project! By following these steps, you can now create visually stunning desktop applications that blend Flutter’s cross-platform capabilities with the native look and feel of macOS.

Conclusion: In this article, we explored how to implement a blurred transparent background in your Flutter macOS project. While building desktop applications in Flutter may present some challenges, integrating platform-specific Swift code can enhance your app’s capabilities and user experience. With the power of Flutter and macOS integration, you can create versatile applications that shine on desktop platforms. Now, you have the tools to add a touch of elegance and sophistication to your Flutter macOS apps with a blurred transparent background effect. Happy coding!

--

--

Sezer Ufuk Yavuz

Flutter & Swift dev | Specializing in LLM applications for daily ease on macOS  | Founder of ThinkBuddy, enhancing the macOS experience: http://thinkbuddy.io