loading words...

May 08, 2019 16:27:55

Flutter for the Web

by @valentino | 1209 words | 36๐Ÿ”ฅ | 334๐Ÿ’Œ

Valentino Urbano

Current day streak: 36๐Ÿ”ฅ
Total posts: 334๐Ÿ’Œ
Total words: 161600 (646 pages ๐Ÿ“„)

Setup

In this tutorial we are going to show you how to build a Flutter application for the web. Note that during the preview of Flutter for the Web hot reload is only available in Chrome.

Install the package webdev globally to enable the preview of Flutter for the Web by running:

```

flutter packages pub global activate webdev

```

Add webdev to you path so you can call it from the Terminal.

```

//if you're on Mac add this line at the end of the file

export PATH="/Users/$USER/flutter/.pub-cache/bin:$PATH"#flutter web

//if you're on Window

%APPDATA%\Pub\Cache\bin

```

Open your Terminal and type `source .bash-profile` to reload your system PATH with the Flutter folder we just added. Think of your PATH as a switch for the Terminal to know where to locate all the various packages in your filesystem.

Now you should be able to use `webdev` by simply typing it in the Terminal.

Installing Dart

If you are not able to use the webdev command it means that Dart is not installed on your system. Flutter comes with its own copy of Dart, but it is not made available outside Flutter and it is used only by Flutter. To be able to use dart from the command line you need to install Dart yourself.

You can follow our guide on how to Install Dart on Mac OSX. If you're using Windows you can follow the official documentation.

At the end of the installation process running `webdev` should print the following:

```

A tool to develop Dart web projects.

Usage: webdev <command> [arguments]

Global options:

-h, --help Print this usage information.

--version Prints the version of webdev.

Available commands:

build Run builders to build a package.

help Display help information for webdev.

serve Run a local web development server and a file system watcher that rebuilds on changes.

Run "webdev help <command>" for more information about a command.

```

If something still doesn't work try troubleshooting it.

Creating a new Web App with Flutter

Open Visual Studio Code and update both the Dart and Flutter extensions. You need to have at least version 3.0.0 of both extensions (not to be confused with the Flutter or Dart versions). Reload the window to make sure the editor applies the changes to the 2 packages. Open the command palette ( โ‡งโŒ˜P ) and type "Flutter web". Select "Flutter: New Web Project":


If instead you are trying to run Flutter for the web for an existing application make sure to update the dependencies of your project by running:

```

flutter packages upgrade

```

webdev has only 2 commands for now, one to run a debug build and the other to build the prod version `webdev serve` and `webdev build` respectively. You can also run the app in production without saving the build to disk by running `webdev serve -r`

To run the development version of the site run `webdev serve`. You will be greeted with the default Flutter widget:


Now we can simply edit the "main.dart" file inside lib to make it similar to the default iOS and Android application by replacing its content with:

```

import 'package:flutter_web/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

// This widget is the root of your application.

@override

Widget build(BuildContext context) {

return MaterialApp(

title: 'Flutter Demo',

theme: ThemeData(

// This is the theme of your application.

//

// Try running your application with "flutter run". You'll see the

// application has a blue toolbar. Then, without quitting the app, try

// changing the primarySwatch below to Colors.green and then invoke

// "hot reload" (press "r" in the console where you ran "flutter run",

// or simply save your changes to "hot reload" in a Flutter IDE).

// Notice that the counter didn't reset back to zero; the application

// is not restarted.

primarySwatch: Colors.blue,

),

home: MyHomePage(title: 'Flutter Demo Home Page'),

);

}

}

class MyHomePage extends StatefulWidget {

MyHomePage({Key key, this.title}) : super(key: key);

// This widget is the home page of your application. It is stateful, meaning

// that it has a State object (defined below) that contains fields that affect

// how it looks.

// This class is the configuration for the state. It holds the values (in this

// case the title) provided by the parent (in this case the App widget) and

// used by the build method of the State. Fields in a Widget subclass are

// always marked "final".

final String title;

@override

_MyHomePageState createState() => _MyHomePageState();

}

class _MyHomePageState extends State<MyHomePage> {

int _counter = 0;

void _incrementCounter() {

setState(() {

// This call to setState tells the Flutter framework that something has

// changed in this State, which causes it to rerun the build method below

// so that the display can reflect the updated values. If we changed

// _counter without calling setState(), then the build method would not be

// called again, and so nothing would appear to happen.

_counter++;

});

}

@override

Widget build(BuildContext context) {

// This method is rerun every time setState is called, for instance as done

// by the _incrementCounter method above.

//

// The Flutter framework has been optimized to make rerunning build methods

// fast, so that you can just rebuild anything that needs updating rather

// than having to individually change instances of widgets.

return Scaffold(

appBar: AppBar(

// Here we take the value from the MyHomePage object that was created by

// the App.build method, and use it to set our appbar title.

title: Text(widget.title),

),

body: Center(

// Center is a layout widget. It takes a single child and positions it

// in the middle of the parent.

child: Column(

// Column is also layout widget. It takes a list of children and

// arranges them vertically. By default, it sizes itself to fit its

// children horizontally, and tries to be as tall as its parent.

//

// Invoke "debug painting" (press "p" in the console, choose the

// "Toggle Debug Paint" action from the Flutter Inspector in Android

// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)

// to see the wireframe for each widget.

//

// Column has various properties to control how it sizes itself and

// how it positions its children. Here we use mainAxisAlignment to

// center the children vertically; the main axis here is the vertical

// axis because Columns are vertical (the cross axis would be

// horizontal).

mainAxisAlignment: MainAxisAlignment.center,

children: <Widget>[

Text(

'You have pushed the button this many times:',

),

Text(

'$_counter',

style: Theme.of(context).textTheme.display1,

),

],

),

),

floatingActionButton: FloatingActionButton(

onPressed: _incrementCounter,

tooltip: 'Increment',

child: Text("+"),//<-- no Icon

), // This trailing comma makes auto-formatting nicer for build methods.

);

}

}

```

Note that we're using `child: Text("+"),` instead of the icon. The icon is (for now) only included in the iOS and Android version of Material. The rest of the code is identical to the mobile version.


You can download the demo from Github.

Troubleshooting


If you still have problems try following these steps:

Dart works, but webdev does not

If dart works and webdev does not the problem is most likely to be found in your PATH.

1. Try running it using its full path (for me it was "./Users/USERNAME/flutter/.pub-cache/bin/webdev") to confirm that it is indeed a problem with the PATH configuration.

2. Open your bash_profile and check that the bin folder has been correctly added to your path.

3. Reload the Terminal by running `source .bash-profile`.

4. Try restarting your machine.

Dart doesn't work

Check Install Dart on Mac OS troubleshooting instructions.


Originally published at learningflutter.net

contact: email - twitter / Terms / Privacy