Building a web application with Flutter

Caching In PHP
How to Cache Data in PHP
30th May 2023
Mojo: The Programming Language for AI That Is Up To 35000x Faster Than Python
3rd June 2023
Show all

Building a web application with Flutter

Flutter for web, This page covers the following steps for getting started with web support:

  • Configure the flutter tool for web support.
  • Create a new project with web support.
  • Run a new project with web support.
  • Build an app with web support.
  • Add web support to an existing project.

Flutter for web, Requirements

To create a Flutter app with web support, you need the following software:

  • Flutter SDK. See the Flutter SDK installation instructions.
  • Chrome; debugging a web app requires the Chrome browser.
  • Optional: An IDE that supports Flutter. You can install Visual Studio Code, Android Studio, IntelliJ IDEA. Also install the Flutter and Dart plugins to enable language support and tools for refactoring, running, debugging, and reloading your web app within an editor. See setting up an editor for more details.

For more information, see the web FAQ.

Create a new project with web support

You can use the following steps to create a new project with web support.

Set up

Run the following commands to use the latest version of the Flutter SDK:content_copy

$ flutter channel stable
$ flutter upgrade

 Warning: Running flutter channel stable replaces your current version of Flutter with the stable version and can take time if your connection is slow. After this, running flutter upgrade upgrades your install to the latest stable. Returning to another channel (beta or master) requires calling flutter channel <channel> explicitly.

If Chrome is installed, the flutter devices command outputs a Chrome device that opens the Chrome browser with your app running, and a Web Server that provides the URL serving the app.content_copy

$ flutter devices
1 connected device:

Chrome (web) • chrome • web-javascript • Google Chrome 88.0.4324.150

In your IDE, you should see Chrome (web) in the device pulldown.

Create and run

Creating a new project with web support is no different than creating a new Flutter project for other platforms.

IDE

Create a new app in your IDE and it automatically creates iOS, Android, desktop, and web versions of your app. From the device pulldown, select Chrome (web) and run your app to see it launch in Chrome.

Command line

To create a new app that includes web support (in addition to mobile support), run the following commands, substituting my_app with the name of your project:content_copy

$ flutter create my_app
$ cd my_app

To serve your app from localhost in Chrome, enter the following from the top of the package:content_copy

$ flutter run -d chrome

 Note: If there aren’t any other connected devices, the -d chrome is optional.

The flutter run command launches the application using the development compiler in a Chrome browser.

 Warning: Hot reload is not supported in a web browser Currently, Flutter supports hot restart, but not hot reload in a web browser.

Build

Run the following command to generate a release build:content_copy

$ flutter build web

A release build uses dart2js (instead of the development compiler) to produce a single JavaScript file main.dart.js. You can create a release build using release mode (flutter run --release) or by using flutter build web. This populates a build/web directory with built files, including an assets directory, which need to be served together.

You can also include --web-renderer html or --web-renderer canvaskit to select between the HTML or CanvasKit renderers, respectively. For more information, see Web renderers.

For more information, see Build and release a web app.

Add web support to an existing app

To add web support to an existing project created using a previous version of Flutter, run the following command from your project’s top-level directory:content_copy

$ flutter create --platforms web .

What you’ll build

You’ll implement a simple web app that displays a sign in screen. The screen contains three text fields: first name, last name, and username. As the user fills out the fields, a progress bar animates along the top of the sign in area. When all three fields are filled in, the progress bar displays in green along the full width of the sign in area, and the Sign up button becomes enabled. Clicking the Sign up button causes a welcome screen to animate in from the bottom of the screen.

The animated GIF shows how the app works at the completion of this lab.

What you’ll learn

  • How to write a Flutter app that looks natural on the web.
  • Basic structure of a Flutter app.
  • How to implement a Tween animation.
  • How to implement a stateful widget.
  • How to use the debugger to set breakpoints.

While developing, run your web app in Chrome so you can debug with Dart DevTools.

Step 0: Get the starter web app

You’ll start with a simple web app that we provide for you.

  1. Enable web development.
    At the command line, perform the following command to make sure that you have Flutter installed correctly.content_copy$ flutter doctor Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel master, 3.4.0-19.0.pre.254, on macOS 12.6 21G115 darwin-arm64, locale en) [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0) [✓] Xcode - develop for iOS and macOS (Xcode 14.0) [✓] Chrome - develop for the web [✓] Android Studio (version 2021.2) [✓] VS Code (version 1.71.1) [✓] Connected device (4 available) [✓] HTTP Host Availability • No issues found! If you see “flutter: command not found”, then make sure that you have installed the Flutter SDK and that it’s in your path.It’s okay if the Android toolchain, Android Studio, and the Xcode tools aren’t installed, since the app is intended for the web only. If you later want this app to work on mobile, you’ll need to do additional installation and setup.
  2. List the devices.
    To ensure that web is installed, list the devices available. You should see something like the following:content_copy$ flutter devices 4 connected devices: sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64 • Android 13 (API 33) (emulator) iPhone 14 Pro Max (mobile) • 45A72BE1-2D4E-4202-9BB3-D6AE2601BEF8 • ios • com.apple.CoreSimulator.SimRuntime.iOS-16-0 (simulator) macOS (desktop) • macos • darwin-arm64 • macOS 12.6 21G115 darwin-arm64 Chrome (web) • chrome • web-javascript • Google Chrome 105.0.5195.125 The Chrome device automatically starts Chrome and enables the use of the Flutter DevTools tooling.

Observations

  • The entire code for this example lives in the lib/main.dart file.
  • If you know Java, the Dart language should feel very familiar.
  • All of the app’s UI is created in Dart code. For more information, see Introduction to declarative UI.
  • The app’s UI adheres to Material Design, a visual design language that runs on any device or platform. You can customize the Material Design widgets, but if you prefer something else, Flutter also offers the Cupertino widget library, which implements the current iOS design language. Or you can create your own custom widget library.
  • In Flutter, almost everything is a Widget. Even the app itself is a widget. The app’s UI can be described as a widget tree.

for more information XpertLab

similar Blogs Click