Corporate Training
Request Demo
Click me

Tutorials

Flutter for Desktop

28. Flutter for Desktop (Windows, macOS, Linux)

Flutter is not limited to mobile and web development; you can also use it to build desktop applications for Windows, macOS, and Linux. In this tutorial, we'll explore how to get started with Flutter for desktop development and create a simple desktop app that works on Windows, macOS, and Linux.

Prerequisites

Before you begin, make sure you have the following installed:

  • Flutter: Install Flutter on your machine by following the instructions in the official Flutter documentation: Install Flutter.

  • Desktop Enablement: Ensure that you've enabled desktop development for Flutter. Run the following commands in your terminal:
flutter channel dev
flutter upgrade
flutter config --enable-windows-desktop
flutter config --enable-macos-desktop
flutter config --enable-linux-desktop
 
  • Code Editor: Use a code editor of your choice. Popular options include Visual Studio Code, Android Studio, and IntelliJ IDEA.

Part 1: Create a Flutter Desktop Project

Step 1: Create a New Flutter Desktop Project

Open your terminal and run the following command to create a new Flutter desktop project:
flutter create my_desktop_app
 

Replace 'my_desktop_app' with your preferred project name.

Step 2: Navigate to the Project Directory

Use the 'cd' command to navigate to your project directory:
cd my_desktop_app
 

Step 3: Open the Project in Your Code Editor

Open the project in your preferred code editor.

Part 2: Building a Simple Web App

Step 1: Replace the Default Code

In the 'lib' directory of your project, open the 'main.dart' file and replace the default code with the following:
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Desktop App'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                'Hello, Flutter for Desktop!',
                style: TextStyle(fontSize: 24),
              ),
              SizedBox(height: 20),
              ElevatedButton(
                onPressed: () {
                  // Add button functionality here
                },
                child: Text('Click me!'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
 

This code creates a simple Flutter web app with a button and a text widget.

Step 2: Run the Desktop App

To run your Flutter desktop app, use the following command:
flutter run
 

This command will start your app, and it will work on the desktop platform you're currently using (Windows, macOS, or Linux).

Step 3: Test the Desktop App

You can interact with your desktop app just like you would with any regular desktop application. Click the "Click me!" button to test your app.

Part 3: Build and Distribute the Flutter Desktop App

Step 1: Build the Desktop App

To build your Flutter desktop app for distribution, use the following command:
flutter build 
 
Replace '<platform>' with the platform you want to build for: 'windows', 'macos', or 'linux'. For example:
flutter build windows
 

This command generates optimized executable files in the 'build/<platform>/' directory.

Step 2: Distribute the Executable

You can distribute the executable file generated in the 'build/<platform>/' directory as you would with any native desktop application. Here are some common methods:

  • Windows: Distribute the '.exe' file.
  • macOS: Create a DMG file or distribute the '.app' bundle.
  • Linux: Distribute the binary file for the appropriate Linux distribution.

Step 3: Additional Platform-Specific Considerations

For platform-specific deployment and distribution details, refer to the official Flutter documentation:

Conclusion:

You've successfully created a Flutter desktop app, tested it locally, and learned how to build and distribute it for Windows, macOS, and Linux. Flutter for desktop development allows you to build cross-platform desktop applications with a single codebase, expanding the reach of your applications to desktop users. You can now explore more advanced features and build full-fledged desktop applications with Flutter.