Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Flutter for Web Development

27. Flutter for Web Development

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

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.

  • Flutter Web: Ensure that you've enabled Flutter web by running the following command in your terminal:
flutter channel beta
flutter upgrade
flutter config --enable-web
 
  • 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 Web Project

Step 1: Create a New Flutter Web Project

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

Replace 'my_web_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_web_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 Web App'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                'Hello, Flutter for Web!',
                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: Add Web-Specific Dependencies

In your 'pubspec.yaml' file, make sure to add the necessary dependencies for web development under 'dependencies':
dependencies:
  flutter:
    sdk: flutter
  flutter_web: any # Add this line for web support
  flutter_web_ui: any # Add this line for web support
 
Save the file, and then run the following command to get the dependencies:
flutter pub get
 

Step 3: Run the Web App

To run your Flutter web app, use the following command:
flutter run -d web
 

This command will start a development server and open your web app in a web browser. You can access it at 'http://localhost:5000' by default.

Step 4: Test the Web App

You can interact with your web app in the browser just like you would with a regular web application. Click the "Click me!" button to test your app.

Part 3: Build and Deploy the Flutter Web App

Step 1: Build the Web App

To build your Flutter web app for production, use the following command:
flutter build web
 

This command generates optimized files in the 'build/web' directory.

Step 2: Deploy to Hosting Service

You can deploy your Flutter web app to various hosting services, such as Firebase Hosting, GitHub Pages, Netlify, or any web hosting provider of your choice. Here's a basic example of deploying to Firebase Hosting:

  • Install the Firebase CLI by running 'npm install -g firebase-tools'.
  • Initialize Firebase in your project directory:
firebase init
 

Follow the prompts to set up Firebase Hosting.

  • Deploy your app to Firebase Hosting:
firebase deploy
 
  • Firebase will provide you with a hosting URL where your Flutter web app is now live.

Conclusion:

You've successfully created a Flutter web app, tested it locally, and deployed it to a hosting service. Flutter for web development opens up exciting possibilities for building cross-platform applications that work seamlessly on both web and mobile. You can now explore more advanced features and build full-fledged web applications with Flutter.