Flutter Add desktop support to an existing Flutter app

13 min read

To add desktop support to an existing Flutter app, follow these steps:

  1. Ensure that your Flutter SDK is up-to-date and properly installed on your computer.
  2. Create a new folder for your desktop implementation within your project folder.
  3. In your project's pubspec.yaml file, add the following dependencies:
flutter:
  plugin:
    platforms:
      windows:
        pluginClass: WindowPlugin
      macos:
        pluginClass: MacOsPlugin
  1. Open the terminal or command prompt, navigate to your project directory, and run the following command:
flutter create .

This command will create a new desktop implementation of your Flutter app.
5. In your desktop implementation's main.dart file, add the following import statement at the top of the file:

import 'package:flutter/material.dart';
  1. Replace the contents of the main() method in main.dart with the following code:
void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}
  1. Create a new class called MyApp that extends StatelessWidget:
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      home: MyHomePage(),
    );
  }
}
  1. Create a new class called MyHomePage that extends StatefulWidget:
class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('My App'),
      ),
      body: Center(
        child: Text(
          'Hello, World!',
          style: TextStyle(fontSize: 24),
        ),
      ),
    );
  }
}
  1. Run your app using the following command:
flutter run -d windows

This command will launch your app in a Windows environment. To launch your app for macOS, use the following command instead:

flutter run -d macos

Congratulations, you have successfully added desktop support to your existing Flutter app!